七大排序算法(冒泡,选择,插入,二分法排序,希尔,快速,合并,堆排序)的java实现

冒泡排序

思路:就是每次将最大或最小的元素放到数组的最后,so easy!时间复杂度为(O(n^2))
[java]  view plain  copy
  1. public class BubbleSort {  
  2.     public static void bubbleSort(int[] a) {  
  3.         for (int j = 1; j < a.length; j++) {  
  4.             for (int i = 0; i < a.length - j; i++) {  
  5.                 if (a[i] > a[i + 1]) {  
  6.                     int temp = a[i];  
  7.                     a[i] = a[i + 1];  
  8.                     a[i + 1] = temp;  
  9.                 }  
  10.             }  
  11.         }  
  12.     }  
  13.     public static void main(String[] args) {  
  14.         int a[] = { 524572424572424572423723043231241157,  
  15.                 3831 };  
  16.         bubbleSort(a);  
  17.         for(int i = 0; i < a.length; i++){  
  18.             System.out.print(a[i]+" ");  
  19.         }  
  20.     }  
  21. }  

选择排序

思路:类似于冒泡,每次将后面最小的元素放在前面。时间复杂度为((O(n^2)))
[java]  view plain  copy
  1. public class SelectSort {  
  2.     public static void selectSort(int[] a) {  
  3.         int min;  
  4.         for (int i = 0; i < a.length - 1; i++) {  
  5.             min = i;  
  6.             for (int j = min + 1; j < a.length; j++) {  
  7.                 if (a[min] > a[j]) {  
  8.                     int temp = a[min];  
  9.                     a[min] = a[j];  
  10.                     a[j] = temp;  
  11.                 }  
  12.             }  
  13.         }  
  14.     }  
  15.   
  16.     public static void main(String[] args) {  
  17.         int a[] = { 5245724245724245724237,  
  18.                 230432312411573831 };  
  19.         selectSort(a);  
  20.         for (int i = 0; i < a.length; i++) {  
  21.             System.out.print(a[i] + " ");  
  22.         }  
  23.     }  
  24. }  

插入排序

思路:从第二个元素开始,和之前的已排好序的字数组的元素比较,找到合适的位置,然后后面的元素向后移动一位,再将该元素插入到前面合适的位置。时间复杂度为(O(n^2))
[java]  view plain  copy
  1. public class InsertSort {  
  2.     public static void insertSort(int[] a) {  
  3.         for (int i = 1; i < a.length; i++) {  
  4.             int key = a[i];  
  5.             int j = i - 1;  
  6.             while (j >= 0 && a[j] > key) {  
  7.                 a[j+1] = a[j];  
  8.                 j--;  
  9.             }  
  10.             a[j+1] = key;  
  11.         }  
  12.     }  
  13.       
  14.     public static void main(String[] args) {  
  15.         int a[] = { 5245724245724245724237,  
  16.                 230432312411573831 };  
  17.         insertSort(a);  
  18.         for (int i = 0; i < a.length; i++) {  
  19.             System.out.print(a[i] + " ");  
  20.         }  
  21.     }  
  22. }  

二分法排序

思路:插入排序的多此一举,居然先用二分法查找插入位置(多此一举),然后再所有插入位置(二分法查出来的)后面的元素全部后移,太蛋疼了,插入法直接边找边后移多容易啊,这个事蛋疼的做法,下午想了一下做出来。(14、08、3)
[java]  view plain  copy
  1. package sort;  
  2.   
  3. public class BinarySort {  
  4.     public static int binarySerch(int[] arr, int start, int end, int value) {  
  5.         int mid = -1;  
  6.         while (start <= end) {  
  7.             mid = (start + end) / 2;  
  8.             if (arr[mid] < value)  
  9.                 start = mid + 1;  
  10.             else if (arr[mid] > value)  
  11.                 end = mid - 1;  
  12.             else  
  13.                 break;  
  14.         }  
  15.         if (arr[mid] < value)  
  16.             return mid + 1;  
  17.         else if (value < arr[mid])  
  18.             return mid;  
  19.   
  20.         return mid + 1;  
  21.   
  22.     }  
  23.   
  24.     public static void binarySort(int[] arr, int start, int end) {  
  25.         for (int i = start + 1; i <= end; i++) {  
  26.             int value = arr[i];  
  27.             int insertLoc = binarySerch(arr, start, i - 1, value) ;  
  28.             for (int j = i; j > insertLoc; j--) {  
  29.                 arr[j] = arr[j - 1];  
  30.             }  
  31.             arr[insertLoc] = value;  
  32.         }  
  33.     }  
  34.   
  35.     public static void main(String[] args) {  
  36.         int[] arr = { 3508791264 };  
  37.         binarySort(arr, 0, arr.length - 1);  
  38.         for (int i = 0; i < arr.length; i++) {  
  39.             System.out.print(arr[i] + " ");  
  40.         }  
  41.     }  
  42. }  


希尔排序

思路:类似于插入排序,只是每次所取的步长为(数组的长度 /  2 / i)。时间复杂度为(n*log n)。
[java]  view plain  copy
  1. public class ShellSort {  
  2.     public static void shellSort(int[] a) {  
  3.         for (int gap = a.length / 2; gap > 0; gap /= 2)  
  4.             for (int i = gap; i < a.length; i++) {  
  5.                 int key = a[i];  
  6.                 int j = i - gap;  
  7.                 while (j >= 0 && a[j] > key) {  
  8.                     a[j + gap] = a[j];  
  9.                     j -= gap;  
  10.                 }  
  11.                 a[j + gap] = key;  
  12.             }  
  13.   
  14.     }  
  15.   
  16.     public static void main(String[] args) {  
  17.         int a[] = { 5245724245724245724237,  
  18.                 230432312411573831 };  
  19.         shellSort(a);  
  20.         for (int i = 0; i < a.length; i++) {  
  21.             System.out.print(a[i] + " ");  
  22.         }  
  23.     }  
  24. }  

快速排序

思路:关键在于求出partition()函数。我给出了两种方法:1、从前到后。2、从前到中间,从后到中间。时间复杂度为(n * log n)最坏情况为
OK!show your my codes!
[java]  view plain  copy
  1. public class QuickSort {  
  2.       
  3.     /*public static int partition(int[] a, int p, int r) { 
  4.         int x = a[r]; 
  5.         int i = p - 1; 
  6.         for (int j = p; j < r; j++) { 
  7.             if (a[j] <= x) {//如果a[j]<x就将后面a[i]后面a[i+1](值大于x)与a[j](值<a[j])进行交换 
  8.                 i++; 
  9.                  
  10.                 int temp = a[i]; 
  11.                 a[i] = a[j]; 
  12.                 a[j] = temp; 
  13.                  
  14.             } 
  15.         } 
  16.         i++; 
  17.         int temp = a[i]; 
  18.         a[i] = a[r]; 
  19.         a[r] = temp; 
  20.         return i; 
  21.     }*/  
  22.        
  23.     public static int partition(int a[], int p, int r) {  
  24.         int x = a[p];  
  25.         int i = p;  
  26.         int j = r ;  
  27.         while (i < j) {  
  28.             while (a[j] >= x && i<j)  
  29.                 j--;  
  30.             a[i] = a[j];//把小于X的那个数拿到前面的a【i】中  
  31.             while (a[i] <= x && i<j)  
  32.                 i++;  
  33.             a[j] = a[i];//把大于X的那个数拿到后面的a【j】中  
  34.         }  
  35.         a[j] = x;//将X拿到分割处  
  36.         return j;  
  37.     }  
  38.   
  39.     public static void quickSort(int[] a, int p, int r) {  
  40.         if (p < r) {  
  41.             int q = partition(a, p, r);  
  42.             quickSort(a, p, q-1);  
  43.             quickSort(a, q + 1, r);  
  44.         }  
  45.     }  
  46.   
  47.     public static void main(String[] args) {  
  48.         int a[] = { 524572424572424572423723043231241157,  
  49.                 3831 };  
  50.         quickSort(a, 0, a.length-1);  
  51.         for (int i = 0; i < a.length; i++) {  
  52.             System.out.print(a[i] + " ");  
  53.         }  
  54.   
  55.     }  
  56. }  

合并排序

思路:用分治的思想,将数组分至最小,再合并即可,不懂自己google吧!时间复杂度为(n * log n )是一个稳定的排序算法。
[java]  view plain  copy
  1. public class MergeSort {  
  2.     public static void merge(int A[], int p, int q, int r) {  
  3.         int[] L = new int[q - p + 1];  
  4.         int[] R = new int[r - q];  
  5.         for (int i = p, j = 0; i <= q; i++, j++) {  
  6.             L[j] = A[i];  
  7.         }  
  8.         for (int i = q + 1, j = 0; i <= r; i++, j++) {  
  9.             R[j] = A[i];  
  10.         }  
  11.         int pos = p;  
  12.         int i = 0, j = 0;  
  13.         for (; i < L.length && j < R.length;) {  
  14.             if (L[i] <= R[j]) {  
  15.                 A[pos++] = L[i++];  
  16.             } else {  
  17.                 A[pos++] = R[j++];  
  18.             }  
  19.         }  
  20.         if (i < L.length) {  
  21.             for (; i < L.length;)  
  22.                 A[pos++] = L[i++];  
  23.         } else if (j < R.length) {  
  24.             for (; j < R.length;)  
  25.                 A[pos++] = R[j++];  
  26.         }  
  27.     }  
  28.   
  29.     public static void mergeSort(int[] A, int p, int r) {  
  30.         if (p < r) {  
  31.             int q = (p + r) / 2;  
  32.             mergeSort(A, p, q);  
  33.             mergeSort(A, q + 1, r);  
  34.             merge(A, p, q, r);  
  35.         }  
  36.     }  
  37.   
  38.     public static void main(String[] args) {  
  39.         int A[] = { 524572424572424572423723043231241157,  
  40.                 3831 };  
  41.         mergeSort(A, 0, A.length - 1);  
  42.         for (int i = 0; i < A.length; i++) {  
  43.             System.out.print(A[i] + " ");  
  44.         }  
  45.     }  
  46. }  

堆排序

思路:建立一个堆,大顶堆或者小顶堆都可以。每次将堆顶元素放到数组最后,然后堆的规模减小一个,再维持第一个元素的大顶堆性质。时间复杂度为(n * log n)。
[java]  view plain  copy
  1. public class HeapSort {  
  2.     //求双亲位置  
  3.     static int parent(int i) {  
  4.         return i / 2;  
  5.     }  
  6.     //求左孩子位置  
  7.     static int left(int i) {  
  8.         return 2 * i;  
  9.     }  
  10.     //求右孩子位置  
  11.     static int right(int i) {  
  12.         return 2 * i + 1;  
  13.     }  
  14.     //维持大顶堆的性质  
  15.     static void maxHelpify(int[] A, int i) {  
  16.         int l = left(i);  
  17.         int r = right(i);  
  18.         int largest = 0;  
  19.         if (l <= A[0] && A[l] > A[i])  
  20.             largest = l;  
  21.         else  
  22.             largest = i;  
  23.         if (r <= A[0] && A[r] > A[largest])  
  24.             largest = r;  
  25.         if (largest != i) {  
  26.             int temp = A[largest];  
  27.             A[largest] = A[i];  
  28.             A[i] = temp;  
  29.             maxHelpify(A, largest);  
  30.         }  
  31.     }  
  32.     //建立大顶堆  
  33.     static void buildMaxHeap(int[] A){  
  34.         for(int i=(A.length-1)/2; i>0; i--){  
  35.             maxHelpify(A, i);  
  36.         }  
  37.     }  
  38.     //堆排序  
  39.     public static void heapSort(int[] A){  
  40.         buildMaxHeap(A);//建立大顶堆  
  41.         //每次把堆顶的数放到数组最后,然后把堆的大小减1,再次维持第一个数的大顶堆性质  
  42.         for(int i = A.length - 1; i>=2; i--){  
  43.             int temp = A[1];  
  44.             A[1] = A[i];  
  45.             A[i] = temp;  
  46.             A[0]--;  
  47.             maxHelpify(A, 1);  
  48.         }  
  49.     }  
  50.     public static void main(String[] args) {  
  51.         int A[] = new int[3];  
  52.         A[0] = A.length-1;//a[0]存放堆的大小  
  53.         for(int i = 1; i < A.length; i++){  
  54.             A[i] = (int) (Math.random()*10);  
  55.         }  
  56.         heapSort(A);  
  57.         for (int i = 1; i < A.length; i++) {  
  58.             System.out.print(A[i] + " ");  
  59.         }  
  60.     }  
  61. }  


其他:还有一种计数排序。

比较简单:就是将数组下标作为元素的value,特殊情况下使用。如排序N个人的年龄就可以用计数排序。将年龄看作数组的下标,定义一个大小为100的数组,将年龄与之比较,如果年龄==下标就将,该下标的value+1.时间复杂度为(N)。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值