.选择排序
基本思想:选择排序的基本思想是遍历数组的过程中,以 i 代表当前需要排序的序号,则需要在剩余的 [i…n-1] 中找出其中的最小值,然后将找到的最小值与 i 指向的值进行交换。因为每一趟确定元素的过程中都会有一个选择最大值的子流程,所以人们形象地称之为选择排序。
选择排序的时间复杂度和空间复杂度分别为 O(n2 ) 和 O(1) 。
例子:
public class SelectSort { public static void main(String []args) { int num[]={4,6,3,2,7,8,2,6,0,7}; new SelectSort().selectSort(num); } public void selectSort(int []num) { int temp; for(int i=0;i<num.length-1;i++) { int k=i; for(int j=i+1;j<num.length;j++) { if(num[k]>num[j]) k=j; } if(k!=i){ temp=num[k]; num[k]=num[i]; num[i]=temp; } } for (int i=0;i<num.length;i++) { System.out.print(num[i]+" "); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
输出:
0 2 2 3 4 6 6 7 7 8
2.冒泡排序
基本思想:临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换,这样一趟过去后,最大或最小的数字被交换到了最后一位,然后再从头开始进行两两比较交换,直到倒数第二位时结束
冒泡排序最坏情况的时间复杂度是O(n²)
存在不足:就是本来位于前面的较小数被交换到后面
例子:
public class BubbleSort { public static void main(String []args) { int num[]={4,6,3,2,7,8,2,6,0,7}; new BubbleSort().bubbleSort(num); } public void bubbleSort(int [] num) { int temp; for(int i=0;i<num.length-1;i++) { for(int j=i+1;j<num.length;j++) { if(num[i]<num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } } } for (int i=0;i<num.length;i++) { System.out.print(num[i]+" "); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
3.插入排序
基本思想:插入排序的基本思想是在遍历数组的过程中,假设在序号 i 之前的元素即 [0..i-1] 都已经排好序,本趟需要找到 i 对应的元素 x 的正确位置 k ,并且在寻找这个位置 k 的过程中逐个将比较过的元素往后移一位,为元素 x “腾位置”,最后将 k 对应的元素值赋为 x
一般情况下,插入排序的时间复杂度和空间复杂度分别为 O(n2 ) 和 O(1)。
例子:
public class InsertSort { public static void main(String []args) { int num[]={4,6,3,2,7,8,2,6,0,7,9,3,4,3,4,5,6,4,3,8,0,1,8}; new InsertSort.insertSort(num); } public void insertSort(int num[]) {int temp,j; for(int i=1;i<num.length;i++) { temp = num[i]; for ( j = i-1; j >=0 &&temp< num[j]; j--) num[j+1]=num[j]; num[j+1]=temp; } for(int n:num) System.out.print(n+" "); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
输出:
0 0 1 2 2 3 3 3 3 4 4 4 4 5 6 6 6 7 7 8 8 8 9
4.归并排序
基本思想:
归并排序采用的是递归来实现,属于“分而治之”,将目标数组从中间一分为二,之后分别对这两个数组进行排序,排序完毕之后再将排好序的两个数组“归并”到一起,归并排序最重要的也就是这个“归并”的过程
归并的过程中需要额外的跟需要归并的两个数组长度一致的空间
public class MergeSort { public static void main(String []args) { int []numbers={4,6,3,7,2,8,2,9,4,6,3,7,1}; for(int n:numbers) System.out.print(n+" "); System.out.println(); new MergeSort().mergeSort(numbers,0,numbers.length-1); for(int n:numbers) System.out.print(n+" "); } private void mergeSort(int []numbers,int left,int right) { int middle=(left+right)/2; if(left<right) { mergeSort(numbers,0,middle); mergeSort(numbers,middle+1,right); merge(numbers,left,middle,right); } } private void merge(int[] numbers, int left, int middle, int right) { int []temp=new int[numbers.length]; int k=left; int m=left; int n=middle+1; while(m<=middle&&n<=right) { if(numbers[m]<numbers[n]) temp[k++]=numbers[m++]; else temp[k++]=numbers[n++]; } while(m<=middle) temp[k++]=numbers[m++]; while(n<=right) temp[k++]=numbers[n++]; while(left<=right) numbers[left]=temp[left++]; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
输出:
4 6 3 7 2 8 2 9 4 6 3 7 1
1 2 2 3 3 4 4 6 6 7 7 8 9
5.快速排序
基本思想:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可以分别对这两部分记录继续进行排序,已达到整个序列有序的目的,本质就是,找一个基位(枢轴,分水岭,作用是左边的都比它小,右边的都比它大.可随机,取名base,首先从序列最右边开始找比base小的,如果小,换位置,从而base移到刚才右边(比较时比base小)的位置(记为临时的high位),这样base右边的都比base大。然后,从序列的最左边开始找比base大的,如果大,换位置,从而base移动到刚才左边(比较时比base大)的位置(记为临时的low位),这样base左边的都比base小,循环以上两步,直到 low == heigh, 这使才真正的找到了枢轴,分水岭. 返回这个位置,分水岭左边和右边的序列,分别再来递归。
例子:
public class QuickSort { public static void main(String[] args) { int []numbers={4,6,3,7,2,8,2,9,4,6,3,7,1}; for(int n:numbers) System.out.print(n+" "); System.out.println(); new QuickSort().sortQuick(numbers); for(int n:numbers) System.out.print(n+" "); } public int[] sortQuick(int[] array) { return quickSort(array, 0, array.length - 1); } private int[] quickSort(int[] arr, int low, int heigh) { if (low < heigh) { int division = partition(arr, low, heigh); quickSort(arr, low, division - 1); quickSort(arr, division + 1, heigh); } return arr; } private int partition(int[] arr, int low, int heigh) { int base = arr[low]; while (low < heigh) { while (low < heigh && arr[heigh] >= base) { heigh--; } swap(arr, heigh, low); while (low < heigh && arr[low] <= base) { low++; } swap(arr, heigh, low); } return low; } private void swap(int[] arr, int a, int b) { int temp; temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
6.希尔排序
基本思想: 希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题。希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 gap 来划分,比如数组 [1, 2, 3, 4, 5, 6, 7, 8] ,如果以 gap = 2 来划分,可以分为 [1, 3, 5, 7] 和 [2, 4, 6, 8] 两个数组(对应的,如 gap = 3 , 则划分的数组为: [1, 4, 7] 、 [2, 5, 8] 、 [3, 6] )然后分别对划分出来的数组进行插入排序,待各个子数组排序完毕之后再减小 gap 值重复进行之前的步骤,直至 gap = 1 ,即对整个数组进行插入排序,此时的数组已经基本上快排好序了,所以需要移动的元素会很小很小,解决了插入排序在处理大规模数组时较多移动次数的问题,希尔排序是插入排序的改进版,在数据量大的时候对效率的提升帮助很大,数据量小的时候建议直接使用插入排序就好了。
例子:
public class ShellSort { public static void main(String[] args) { int []numbers={4,6,3,7,2,8,2,9,4,6,3,7,1}; for(int n:numbers) System.out.print(n+" "); System.out.println(); new ShellSort().sortShell(numbers); for(int n:numbers) System.out.print(n+" "); } public int[] sortShell(int[] array) { int step = array.length / 2; while (step >= 1) { for (int i = step; i < array.length; i++) { int temp = array[i]; int j = 0; for (j = i - step; j >= 0 && temp < array[j]; j -= step) { array[j + step] = array[j]; } array[j + step] = temp; } step /= 2; } return array; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
7.堆排序
基本思想:本质就是先构造一个大顶堆,parent比children大,root节点就是最大的节点 把最大的节点(root)与尾节点(最后一个节点,比较小)位置互换,剩下最后的尾节点,现在最大,其余的,从第一个元素开始到尾节点前一位,构造大顶堆递归。
例子:
public class HeapSort { public static void main(String[] args) { int []numbers={4,6,3,7,2,8,2,9,4,6,3,7,1}; for(int n:numbers) System.out.print(n+" "); System.out.println(); new HeapSort().sortHeap(numbers); for(int n:numbers) System.out.print(n+" "); } public int[] sortHeap(int[] array) { buildHeap(array); int n = array.length; int i = 0; for (i = n - 1; i >= 1; i--) { swap(array, 0, i); heapify(array, 0, i); } return array; } private void buildHeap(int[] array) { int n = array.length; for (int i = n / 2 - 1; i >= 0; i--) heapify(array, i, n); } private void heapify(int[] A, int idx, int max) { int left = 2 * idx + 1; int right = 2 * idx + 2; int largest = 0; if (left < max && A[left] > A[idx]) largest = left; else largest = idx; if (right < max && A[right] > A[largest]) largest = right; if (largest != idx) { swap(A, largest, idx); heapify(A, largest, max); } } public static void heapAdjust(int[] array, int s, int m) { array[0] = array[s]; for (int j = 2 * s; j <= m; j *= 2) { if (j < m && array[j] < array[j + 1]) { j++; } if (!(array[0] < array[j])) { break; } array[s] = array[j]; s = j; } array[s] = array[0]; } private void swap(int[] arr, int a, int b) { int temp; temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; }}