面试需要掌握的排序 Quick Sort and Merge Sort 快速排序和归并排序 [Java]
2013-09-12 02:27
621 查看
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.
另外partition还可以解决一类面试题(第k大的数),例如:
1. 最小的k个数,时间复杂度是O(N)
2. 数组中次数超过一半的数,时间复杂度是O(N)
解法都是不断的缩小partition的范围,直到partition返回值为k时停止,即找到了第k大的数。时间复杂度O(N)需要数学公式推理,记住答案吧少年。
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];
}
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];
}
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】
- 交换排序—快速排序(Quick Sort)原理以及Java实现
- java面试需要掌握的知识点
- Java基础知识强化55:经典排序之归并排序(MergeSort)
- 排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序
- 插入排序、选择排序、归并排序、堆排序、快速排序的JAVA实现
- JAVA 面试基础试题(大家可以参考一下学Java时需要掌握的基础知识)
- 最简单之Java实现冒泡排序、选择排序、插入排序、希尔排序、归并排序和快速排序(转载请注明出处)
- 【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】
- 高级java工程师面试需要掌握的点
- java面试需要掌握知识点
- java面试需要掌握知识点
- 排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序
- java中各种常用排序实现(直接插入排序、直接选择排序、堆排序、冒泡排序、快速排序和归并排序)
- java面试需要掌握知识点
- 用java.util.Collections中的sort方法对两个类的对象进行排序
- list---java List 排序 Collections.sort() 对 List 排序
- 简单插入排序(Insertion Sort)——插入类排序法(Java实现)
- 用Java集合中的Collections.sort方法对list排序的两种方法
- sort之选择排序(java基础)