面试需要掌握的排序 Quick Sort and Merge Sort 快速排序和归并排序 [Java]

来源:互联网 发布:vs mysql 编辑:程序博客网 时间:2024/05/21 11:17

if N (the number of items to be sorted) is 10,000, then N^2 is 100,000,000, while N*logN is only 40,000. If sorting this many items required 40 seconds with the mergesort, it would take almost 28 hours for the insertion sort.

Part 1 Quick Sort 快速排序

快速排序的算法没有什么可以多说的。但有一点需要提及就是partition函数的重要性。partition的应用场景很多,例如把文件分成大于1G的和小于1G的,把班里同学分成身高高于170cm的和小于170cm的等等。
另外partition还可以解决一类面试题(第k大的数),例如:
1. 最小的k个数,时间复杂度是O(N)
2. 数组中次数超过一半的数,时间复杂度是O(N)
解法都是不断的缩小partition的范围,直到partition返回值为k时停止,即找到了第k大的数。时间复杂度O(N)需要数学公式推理,记住答案吧少年。

public void quicksort(int[] data, int left, int right) {    if (left >= right)        return;    else {        int partition = partition(data, left, right);        quicksort(data, left, partition - 1);        quicksort(data, partition, right);    } }int partition(int[] data, int left, int right){      int i = left, j = right;      int tmp;      int pivot = data[(left + right) / 2];           while (i <= j) {            while (data[i] < pivot)                  i++;            while (data[j] > pivot)                  j--;            if (i <= j) {                  tmp = data[i];                  data[i] = data[j];                  data[j] = tmp;                  i++;                  j--;            }      };           return i;}

Part 2 Merge Sort 归并排序

Merge sort can be highly useful in situations where quick sort isimpractical 归并排序可以在许多大数据的情况下应用,并且时间复杂度是 O(N*logN)

The downside of the mergesort is that it requires an additional array in memory, equal in size to the one being sorted. 需要额外大小等同的内存

/**

 * @param integer data array * @param start index * @param length of the given array */public static void mergesort(int[ ] data, int first, int  n){int n1; // Size of the first half of the array int n2; // Size of the second half of the array if (n > 1) { // Compute sizes of the two halves n1 = n / 2; n2 = n - n1; mergesort(data, first, n1); // Sort data[first] through data[first+n1-1] mergesort(data, first + n1, n2); // Sort data[first+n1] to the end // Merge the two sorted halves. merge(data, first, n1, n2); } }/** * @param integer data array * @param start index * @param length of the first half sorted array * @param length of the second half sorted array */private static void merge(int[ ] data, int first, int n1, int n2) { int[ ] temp = new int[n1+n2]; // Allocate the temporary array int copied = 0; // Number of elements copied from data to temp int copied1 = 0; // Number copied from the first half of data int copied2 = 0; // Number copied from the second half of data int i; // Array index to copy from temp back into data // Merge elements, copying from two halves of data to the temporary array. while ((copied1 < n1) && (copied2 < n2)) { if (data[first + copied1] < data[first + n1 + copied2]) temp[copied++] = data[first + (copied1++)]; else temp[copied++] = data[first + n1 + (copied2++)]; } // Copy any remaining entries in the left and right subarrays. while (copied1 < n1) temp[copied++] = data[first + (copied1++)]; while (copied2 < n2) temp[copied++] = data[first + n1 + (copied2++)]; // Copy from temp back to the data array. for (i = 0; i < n1+n2; i++) data[first + i] = temp[i]; }
	
				
		
原创粉丝点击