七大常见排序算法总结(Java语言)

排序方法平均情况最好情况最坏情况辅助空间稳定性
冒泡排序 O(n^2) O(n) O(n^2) O(1) 稳定
简单选择排序 O(n^2) O(n^2) O(n^2) O(1) 稳定
直接插入排序 O(n^2) O(n) O(n^2) O(1) 稳定
希尔排序 O(nlogn)~O(n^2) O(n^1.3) O(n^2) O(1) 不稳定
堆排序 O(nlogn) O(nlogn) O(nlogn) O(1) 不稳定
归并排序 O(nlogn) O(nlogn) O(nlogn) O(n) 稳定
快速排序 O(nlogn) O(nlogn) O(n^2) O(logn)~O(n) 不稳定

 事先准备一个库,里面有数据产生方法和交换方法(男票反复强调code style很重要!!!)

目录结构

·Utils

  ·DataPreparationUtil

  ·Swap

·Test

 ·BubbleSort

    ·SelectSort

    ·InsertSort

    ·HeapSort

    ·MergeSort

    ·QuickSort

    ·Shellsort

 

接下来介绍9个函数的代码

1.DataPreparationUtil

 1 package Utils;
 2 
 3 import java.util.Random;
 4 import java.util.stream.IntStream;
 5 
 6 public class DataPreparationUtil {
 7 
 8     public static Integer[] PreparationData(int nums,int range) {
 9         Random random = new Random();
10         return IntStream.rangeClosed(0, nums)
11                 .boxed()
12                 .map(index->random.nextInt(range))
13                 .toArray(Integer[]::new);
14     }
15 
16 }

2.Swap

1 package Utils;
2 
3 public class Swap {
4     public static void swap(Integer[] arr,int i,int j) {
5         int temp = arr[i];
6         arr[i] = arr[j];
7         arr[j] = temp;
8     }
9 }

3.BubbleSort

 1 package test;
 2 
 3 import java.util.Arrays;
 4 
 5 import Utils.DataPreparationUtil;
 6 import Utils.Swap;
 7 
 8 public class BubbleSort {
 9 
10     public static void main(String[] args) {
11         Integer[] arr = DataPreparationUtil.PreparationData(8, 20);
12         System.out.println("before sort:" + Arrays.toString(arr));
13         
14         for(int i = 0;i < arr.length-1;i++) {
15             for(int j = 0;j < arr.length-1-i;j++) {
16                 if(arr[j] > arr[j+1]) {
17                     Swap.swap(arr, j, j+1);
18                 }
19             }
20         }
21         System.out.println("after sort:" + Arrays.toString(arr));
22     }
23 
24 }

4.SelectSort

 1 package test;
 2 
 3 import java.util.Arrays;
 4 
 5 public class SelectSort {
 6 
 7     public static void main(String[] args) {
 8         Integer[] arr = Utils.DataPreparationUtil.PreparationData(8, 20);
 9         System.out.println("before sort:" + Arrays.toString(arr));
10         
11         for(int i = 0;i < arr.length-1;i++) {
12             for(int j = i+1;j < arr.length;j++) {
13                 if(arr[i] > arr[j]) {
14                     Utils.Swap.swap(arr, i, j);
15                 }
16             }
17         }
18         System.out.println("after sort:" + Arrays.toString(arr));
19     }
20 
21 }

5.InsertSort

 1 package test;
 2 
 3 import java.util.Arrays;
 4 
 5 public class InsertSort {
 6 
 7     public static void main(String[] args) {
 8         Integer[] arr = Utils.DataPreparationUtil.PreparationData(8, 20);
 9         System.out.println("before sort:" + Arrays.toString(arr));
10         
11         for(int i = 1;i < arr.length;i++) {
12             int j = i;
13             while(j>0 && arr[j] < arr[j-1]) {
14                 Utils.Swap.swap(arr, j-1, j);
15                 j--;
16             }
17         }
18         
19         System.out.println("after sort:" + Arrays.toString(arr));
20     }
21 
22 }

6.HeapSort

 1 package test;
 2 
 3 import java.util.Arrays;
 4 
 5 public class HeapSort {
 6 
 7     public static void main(String[] args) {
 8         Integer[] arr = Utils.DataPreparationUtil.PreparationData(10, 20);
 9         System.out.println("before sort:" + Arrays.toString(arr));
10 
11         sort(arr);
12         System.out.println("after sort: "+ Arrays.toString(arr) );
13     }
14 
15     private static void sort(Integer[] arr) {
16 
17         for(int i=arr.length/2-1;i>=0;i--) {
18             AdjustHeap(arr,i,arr.length);
19         }
20         for(int j=arr.length-1;j>0;j--) {
21             Utils.Swap.swap(arr, 0,j);
22             AdjustHeap(arr,0,j);
23         }
24     }
25 
26     private static void AdjustHeap(Integer[] arr, int i, int length) {
27         int temp = arr[i];
28         for(int k=i*2+1;k<length;k=k*2+1) {
29             if(k+1<length&&arr[k]<arr[k+1]) {
30                 k++;
31             }
32             
33             if(arr[k]>temp) {
34                 arr[i]=arr[k];
35                 i=k;
36             } else {
37                 break;
38             }
39         }
40         arr[i] = temp;
41     }
42 
43 }

7.MergeSort

 1 package test;
 2 
 3 import java.util.Arrays;
 4 
 5 public class MergeSort {
 6 
 7     public static void main(String[] args) {
 8         Integer[] arr = Utils.DataPreparationUtil.PreparationData(8, 20);
 9         System.out.println("before sort: "+ Arrays.toString(arr));
10         
11         sort(arr,0,arr.length-1);
12         System.out.println("after sort: "+ Arrays.toString(arr));
13 
14     }
15     private static void sort(Integer[] arr, int left, int right) {
16         if(left>=right) return;
17         int mid = (left+right)/2;
18         sort(arr,left,mid);
19         sort(arr,mid+1,right);
20         merge(arr,left,right);
21     }
22     private static void merge(Integer[] arr, int left, int right) {
23         int mid = (left+right)/2;
24         int[] temp = new int[arr.length];
25         int i=left;
26         int j=mid+1;
27         int t=0;
28         
29         while(i<=mid&&j<=right) {
30             if(arr[i] < arr[j]) {
31                 temp[t++]=arr[i++];
32             } else {
33                 temp[t++]=arr[j++];
34             }
35         }
36         
37         while(i<=mid) {
38             temp[t++]=arr[i++];
39         }
40         while(j<=right) {
41             temp[t++]=arr[j++];
42         }
43         
44         t=0;
45         while(left<=right) {
46             arr[left++] = temp[t++];
47         }
48     }    
49 
50 }

8.QuickSort

 

 1 package test;
 2 
 3 import java.util.Arrays;
 4 import java.util.Random;
 5 
 6 public class QuickSort {
 7 
 8     public static void main(String[] args) {
 9         Integer[] arr = Utils.DataPreparationUtil.PreparationData(8, 20);
10         System.out.println("before sort: " + Arrays.toString(arr));
11         
12         sort(arr,0,arr.length-1);
13         System.out.println("after sort: " + Arrays.toString(arr));
14     }
15 
16     private static void sort(Integer[] arr, int min, int max) {
17         if(arr==null||min>=max) return;
18         Random random = new Random();
19         int pivot = random.nextInt(max) % (max-min+1) + min;
20         
21         Utils.Swap.swap(arr, pivot, max);
22         int temp = min-1;
23         for (int i = min; i < max; i++) {
24             if(arr[i] <= arr[max]) {
25                 temp++;
26                 Utils.Swap.swap(arr,temp,i);
27             }
28         }
29         temp++;
30         Utils.Swap.swap(arr, temp, max);
31         sort(arr,min,temp-1);
32         sort(arr,temp+1,max);
33     }
34 }

9.Shellsort

 1 package test;
 2 
 3 import java.util.Arrays;
 4 
 5 public class Shellsort {
 6 
 7     public static void main(String[] args) {
 8         Integer[] arr = Utils.DataPreparationUtil.PreparationData(8, 10);
 9         System.out.println("before sort: " + Arrays.toString(arr));
10         
11         sort(arr);
12         System.out.println("after sort: " + Arrays.toString(arr));
13     }
14 
15     private static void sort(Integer[] arr) {
16         for(int gap=arr.length/2;gap>=1;gap=gap/2) {
17             for(int i=gap;i<arr.length;i++) {
18                 int j=i;
19                 while(j-gap>=0&&j>=0&&arr[j]<arr[j-gap]) {
20                     Utils.Swap.swap(arr, j-gap, j);
21                     j-=gap;
22                 }
23             }
24             
25         }
26     }
27 
28 }

 

posted on 2017-10-15 21:05  小嘤嘤  阅读(296)  评论(0编辑  收藏  举报

导航