简单排序算法

冒泡排序:

[java]  view plain  copy
 print ?
  1. import java.util.Arrays;  
  2.   
  3.   
  4. public class Bubbling  
  5.   
  6.       
  7.       
  8.     public int[] result(int[] array){   //int型可以换成是Object,但数组元素必须实现compareto()方法。  
  9.         if(array.length==0){  
  10.             return array;  
  11.         }else 
  12.             int length,temp;  
  13.             length array.length;  
  14.               
  15.             for(int i=1;i
  16.                 for(int a=0;a
  17.                     if(array[a]>array[a+1]){  
  18.                         temp =array[a];  
  19.                         array[a]=array[a+1];  
  20.                         array[a+1]=temp;  
  21.                      
  22.                  
  23.              
  24.               
  25.               
  26.          
  27.           
  28.         return array;  
  29.      
  30.     public static void main(String[] args)  
  31.         int[] =new int[5];  
  32.         a[0]=1 
  33.         a[1]=5 
  34.         a[2]=3 
  35.         a[3]=0 
  36.         a[4]=6 
  37.         Bubbling bubbling new Bubbling();  
  38.         a=bubbling.result(a);  
  39.         System.out.println(Arrays.toString(a));  
  40.      

 

 

快速排序:

[java]  view plain  copy
 print ?
  1. public class QuickSort   
  2.  
  3.   
  4.       
  5.     public static void main(String[] args)   
  6.      
  7.         quicksort qs new quicksort();  
  8.         int data[] {44,22,2,32,54,22,88,77,99,11};  
  9.         qs.data data;  
  10.         qs.sort(0qs.data.length-1);  
  11.         qs.display();  
  12.      
  13.   
  14.  
  15.   
  16.   
  17. class quicksort  
  18.  
  19.     public int data[];  
  20.       
  21.     private int partition(int sortArray[],int low,int hight)//划分  
  22.      
  23.         int key sortArray[low];  
  24.           
  25.         while(low
  26.          
  27.             while(low=key)  
  28.                 hight--;  
  29.             sortArray[low] sortArray[hight];  
  30.               
  31.             while(low<=key)  
  32.                 low++;  
  33.             sortArray[hight] sortArray[low];  
  34.          
  35.         sortArray[low] key;  
  36.         return low;  
  37.      
  38.       
  39.     public void sort(int low,int hight)  
  40.      
  41.         if(low
  42.          
  43.             int result partition(data,low,hight);//把整体分成独立的两部分,返回轴值位置  
  44.             sort(low,result-1);//左侧重复  
  45.             sort(result+1,hight);//右侧重复  
  46.          
  47.           
  48.      
  49.       
  50.     public void display()  
  51.      
  52.         for(int i=0;i
  53.          
  54.             System.out.print(data[i]);  
  55.             System.out.print(");  
  56.          
  57.      
  58.  



插入排序:

[java]  view plain  copy
 print ?
  1. import java.util.Arrays;  
  2.   
  3.   
  4. public class InsertionSort  
  5.   
  6.       
  7.     public int[] getResult(int[] objects){  
  8.         int temp,in,out;  
  9.         for(out=1;out
  10.             temp =objects[out];  
  11.             in =out;  
  12.             while(in>0&&objects[in-1]>temp){  
  13.                 objects[in]=objects[in-1];  
  14.                 --in;  
  15.              
  16.             objects[in]=temp;  
  17.          
  18.         return objects;  
  19.      
  20.     public static void main(String[] args)  
  21.         int[] a={1,3,2,4,0};  
  22.         InsertionSort insertionSort =new InsertionSort();  
  23.         a=insertionSort.getResult(a);  
  24.         System.out.println(Arrays.toString(a));  
  25.           
  26.      
  27.   
  28.  


希尔排序:

[java]  view plain  copy
 print ?
  1. import java.util.Arrays;  
  2.   
  3.   
  4. public class Shell_Sort  
  5.   
  6.       
  7.       
  8.     public int[] shellSort(int[] theArray)     
  9.         
  10.         int inner=0,outer=0 
  11.         int nElems=theArray.length;  
  12.         long temp=0    
  13.         int h=1                   //find initial value of     
  14.         while(h<=nElems/3         //1,4,13,40,121,...     
  15.             h=h*3+1                 
  16.           
  17.         while(h>0    
  18.             
  19.             //当间隔h>1时,进行小部分插入排序;当间隔h=1时,进行整体插入排序  
  20.         for(outer=h;outer
  21.                 
  22.                 temp=theArray[outer];     
  23.                 inner=outer;         
  24.               while(inner>h-1&&theArray[inner-h]>=temp)     
  25.                     
  26.                 theArray[inner]=theArray[inner-h];     
  27.                 inner-=h;     
  28.                     
  29.             theArray[inner]=(inttemp;     
  30.                  
  31.             h=(h-1)/3 // 间隔从大减小,一直减小到1,此时因为间隔为1,就相当于是做整体的插入排序  
  32.           
  33.           
  34.         return theArray;     
  35.          
  36.          
  37.     
  38.   
  39.     public static void main(String[] args)  
  40.         int[] a={1,4,2,7,3,12,44,21,55,32,11};  
  41.         Shell_Sort g=new Shell_Sort();  
  42.         a=g.shellSort(a);  
  43.         System.out.println(Arrays.toString(a));  
  44.           
  45.      
  46.   
  47.  


 

选择排序:

[java]  view plain  copy
 print ?
  1. import java.util.Arrays;  
  2.   
  3.   
  4. public  class SelectSort  
  5.   
  6.       
  7.     public int[] getResult(int[] objects){  
  8.         int in,out,min,temp;  
  9.           
  10.         //如果数组为null,直接返回  
  11.         if(objects==null||objects.length==0){  
  12.             return objects;  
  13.          
  14.           
  15.           
  16.         for(out=0;out1;out++){  
  17.             min =out;  
  18.             //寻找最小值  
  19.             for(in=out+1;in
  20.                 if(objects[min]>objects[in]){  
  21.                     min in;  
  22.                  
  23.              
  24.               
  25.             //与out交换位置  
  26.             temp =objects[out];  
  27.             objects[out]=objects[min];  
  28.             objects[min]=temp;  
  29.               
  30.          
  31.         return objects;  
  32.      
  33.     public static void main(String[] args)  
  34.         SelectSort ss new SelectSort();  
  35.         int[] ={2,5,4,1};  
  36.         a=ss.getResult(a);  
  37.         System.out.println(Arrays.toString(a));  
  38.      
  39.   
  40. }
简单排序算法是指冒泡排序、选择排序和插入排序这三种基本的排序算法。这些算法都是通过比较和交换数组中的元素来实现排序的。以下是这三种算法的简单介绍和Java代码实现: 1. 冒泡排序算法:重复地遍历要排序的数列,比较相邻的两个元素,如果顺序错误就交换它们的位置,直到没有需要交换的元素为止。时间复杂度为O(n^2)。 Java代码实现: ``` public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } ``` 2. 选择排序算法:遍历数组,找到最小的元素,将其放到数组的起始位置,然后再从剩余的元素中找到最小的元素,放到已排序部分的末尾。时间复杂度为O(n^2)。 Java代码实现: ``` public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } ``` 3. 插入排序算法:将数组分成已排序和未排序两部分,遍历未排序部分的元素,将其插入到已排序部分的合适位置。时间复杂度为O(n^2)。 Java代码实现: ``` public static void insertionSort(int[] arr) { int n = arr.length; for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值