今天主要复习一下各种排序算法,以后机试可能会有用。
1.冒泡排序
冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。
1.1算法描述
步骤1: 比较相邻的元素。如果第一个比第二个大,就交换它们两个;
步骤2: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数;
步骤3: 针对所有的元素重复以上的步骤,除了最后一个;
步骤4: 重复步骤1~3,直到排序完成。
1.2代码
1 2 3 4 5 6 7 8 9 10 11 12 13
| int* bubbleSort(int* numArray,int numLen){ int i,j,temp; for(i = 0;i<numLen;i++){ for(j = 0;j<numLen-i-1;j++){ if(numArray[j]>numArray[j+1]){ temp = numArray[j+1]; numArray[j+1]=numArray[j]; numArray[j]=temp; } } } return numArray; }
|
2.选择排序
选择排序 是表现最稳定的排序算法之一 ,因为无论什么数据进去都是O(n2)的时间复杂度 ,所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。理论上讲,选择排序可能也是平时排序一般人想到的最多的排序方法。
选择排序(Selection-sort) 是一种简单直观的排序算法。它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
2.1算法描述
n个记录的直接选择排序可经过n-1趟直接选择排序得到有序结果。具体算法描述如下:
步骤1:初始状态:无序区为R[1…n],有序区为空;
步骤2:第i趟排序(i=1,2,3…n-1)开始时,当前有序区和无序区分别为R[1…i-1]和R(i…n)。该趟排序从当前无序区中-选出关键字最小的记录 R[k],将它与无序区的第1个记录R交换,使R[1…i]和R[i+1…n)分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区;
步骤3:n-1趟结束,数组有序化了
2.2代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| int* selectionSort(int* numArray,int numLen){ int i,j,minIndex,temp; for(i = 0;i<numLen;i++){ minIndex = i; for(j = i;j<numLen;j++){ if(numArray[j]<numArray[minIndex]){ minIndex = j; } } temp = numArray[minIndex]; numArray[minIndex]=numArray[i]; numArray[i]=temp; } return numArray; }
|
3.插入排序
插入排序(Insertion-Sort) 的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。
3.1算法描述
一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:
步骤1: 从第一个元素开始,该元素可以认为已经被排序;
步骤2: 取出下一个元素,在已经排序的元素序列中从后向前扫描;
步骤3: 如果该元素(已排序)大于新元素,将该元素移到下一位置;
步骤4: 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置;
步骤5: 将新元素插入到该位置后;
步骤6: 重复步骤2~5
3.2代码
1 2 3 4 5 6 7 8 9 10 11 12 13
| int* insertionSort(int* numArray,int numLen){ int i,j,preIndex,temp; for(i = 0;i<numLen-1;i++){ temp = numArray[i+1]; preIndex = i; while(preIndex>=0&&temp<numArray[preIndex]){ numArray[preIndex+1]=numArray[preIndex]; preIndex--; } numArray[preIndex+1] = temp; } return numArray; }
|
4.希尔排序
希尔排序是希尔(Donald Shell) 于1959年提出的一种排序算法。希尔排序也是一种插入排序,它是简单插入排序经过改进之后的一个更高效的版本,也称为缩小增量排序,同时该算法是冲破O(n2)的第一批算法之一。它与插入排序的不同之处在于,它会优先比较距离较远的元素。希尔排序又叫缩小增量排序。
希尔排序是把记录按下表的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。
4.1算法描述
我们来看下希尔排序的基本步骤,在此我们选择增量gap=length/2,缩小增量继续以gap = gap/2的方式,这种增量选择我们可以用一个序列来表示,{n/2,(n/2)/2…1},称为增量序列。希尔排序的增量序列的选择与证明是个数学难题,我们选择的这个增量序列是比较常用的,也是希尔建议的增量,称为希尔增量,但其实这个增量序列不是最优的。此处我们做示例使用希尔增量。
先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,具体算法描述:
步骤1:选择一个增量序列t1,t2,…,tk,其中ti>tj,tk=1;
步骤2:按增量序列个数k,对序列进行k 趟排序;
步骤3:每趟排序,根据对应的增量ti,将待排序列分割成若干长度为m 的子序列,分别对各子表进行直接插入排序。仅增量因子为1 时,整个序列作为一个表来处理,表长度即为整个序列的长度。
4.2代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| int* shellSort(int* numArray,int numLen){ int temp, gap = numLen / 2,i,preIndex; while (gap > 0) { for (i = gap; i < numLen; i++) { temp = numArray[i]; preIndex = i - gap; while (preIndex >= 0 && numArray[preIndex] > temp) { numArray[preIndex + gap] = numArray[preIndex]; preIndex -= gap; } numArray[preIndex + gap] = temp; } gap /= 2; } return numArray; }
|
5.归并排序
和选择排序一样,归并排序的性能不受输入数据的影响,但表现比选择排序好的多,因为始终都是O(n log n)的时间复杂度。代价是需要额外的内存空间。
归并排序 是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。归并排序是一种稳定的排序方法。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并
5.1算法描述
步骤1:把长度为n的输入序列分成两个长度为n/2的子序列;
步骤2:对这两个子序列分别采用归并排序;
步骤3:将两个排序好的子序列合并成一个最终的排序序列。
5.2代码
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
| void Merge(int sourceArr[],int tempArr[], int startIndex, int midIndex, int endIndex) { int i = startIndex, j=midIndex+1, k = startIndex; while(i!=midIndex+1 && j!=endIndex+1) { if(sourceArr[i] > sourceArr[j]) tempArr[k++] = sourceArr[j++]; else tempArr[k++] = sourceArr[i++]; } while(i != midIndex+1) tempArr[k++] = sourceArr[i++]; while(j != endIndex+1) tempArr[k++] = sourceArr[j++]; for(i=startIndex; i<=endIndex; i++) sourceArr[i] = tempArr[i]; }
void MergeSort(int sourceArr[], int tempArr[], int startIndex, int endIndex) { int midIndex; if(startIndex < endIndex) { midIndex = startIndex + (endIndex-startIndex) / 2; MergeSort(sourceArr, tempArr, startIndex, midIndex); MergeSort(sourceArr, tempArr, midIndex+1, endIndex); Merge(sourceArr, tempArr, startIndex, midIndex, endIndex); } }
|
6.快速排序
快速排序 的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。
6.1算法描述
快速排序使用分治法来把一个串(list)分为两个子串(sub-lists)。具体算法描述如下:
步骤1:从数列中挑出一个元素,称为 “基准”(pivot );
步骤2:重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;
步骤3:递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。
6.2代码
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 52 53
| import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class quickSort { public static int partSort(int[] nums, int left, int right){ int key = left, tmp; while (left < right){ while (left < right && nums[right] >= nums[key]){ right--; } while (left < right && nums[left] <= nums[key]){ left++; } tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; } tmp = nums[key]; nums[key] = nums[left]; nums [left] = tmp; return left; } public static void sort(int[] nums, int left, int right){ if(right - left <= 0){ return; } int key = partSort(nums,left,right); sort(nums, left, key-1); sort(nums,key+1, right); } public static void main(String[] args) { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String in = null; try { in = input.readLine(); } catch (IOException e) { throw new RuntimeException(e); } String[] inArr = in.split(" "); int cnt = 0; int[] nums = new int[inArr.length]; for(String s : inArr){ nums[cnt] = Integer.parseInt(s); cnt++; } sort(nums,0,nums.length-1); for(int num:nums){ System.out.print(num); System.out.print(" "); } } }
|
7.计数排序
7.1算法描述
时间复杂度O(k),空间复杂度O(n + k),k是最大值与最小值差值
- 根据待排序集合中最大元素和最小元素的差值范围,申请额外空间;
- 遍历待排序集合,将每一个元素出现的次数记录到元素值对应的额外空间内;
- 对额外空间内数据进行计算,得出每一个元素的正确位置;
- 将待排序集合每一个元素移动到计算得出的正确位置上。