| Time | ||||||
|---|---|---|---|---|---|---|
| Sort | Average | Best | Worst | Space | Stability | Remarks |
| Bubble sort | O(n^2) | O(n^2) | O(n^2) | Constant | Stable | Always use a modified bubble sort |
| Modified Bubble sort | O(n^2) | O(n) | O(n^2) | Constant | Stable | Stops after reaching a sorted array |
| Selection Sort | O(n^2) | O(n^2) | O(n^2) | Constant | Stable | Even a perfectly sorted input requires scanning the entire array |
| Insertion Sort | O(n^2) | O(n) | O(n^2) | Constant | Stable | In the best case (already sorted), every insert requires constant time |
| Heap Sort | O(n*log(n)) | O(n*log(n)) | O(n*log(n)) | Constant | Instable | By using input array as storage for the heap, it is possible to achieve constant space |
| Merge Sort | O(n*log(n)) | O(n*log(n)) | O(n*log(n)) | Depends | Stable | On arrays, merge sort requires O(n) space; on linked lists, merge sort requires constant space |
| Quicksort | O(n*log(n)) | O(n*log(n)) | O(n^2) | Constant | Stable | Randomly picking a pivot value (or shuffling the array prior to sorting) can help avoid worst case scenarios such as a perfectly sorted array. |



|
66
|
55
|
88
|
11
|
44
|
22
|
99
|
33
|
77 |
| 33 |
55
|
88
|
11
|
44
|
22
|
99
|
66
|
77 |
|
33
|
55
|
88
|
11
|
44
|
22
|
99
|
66
|
77 |
|
33
|
55
|
66
|
11
|
44
|
22
|
99
|
88 | 77 |
|
33
|
55
|
66
|
11
|
44
|
22
|
99
|
88
|
77 |
|
33
|
55
|
22
|
11
|
44
|
66
|
99
|
88
|
77 |
|
33
|
55
|
22
|
11
|
44
|
66 |
99
|
88
|
77 |
|
33
|
55
|
22
|
11
|
44
|
66
|
99
|
88
|
77 |
// 快速排序
private static void quickSort(int[] array, int start, int end) {
if (start >= end) {
return;
}
int i = start;
int j = end;
boolean isrun = true;
// 思路一的逻辑代码
while (i != j) {
if (array[i] > array[j]) {
swap(array, i, j);
isrun = (isrun == true) ? false : true;
}
if (isrun) {
j--;
} else {
i++;
}
time++;
}
print(array);
i--;
j++;
quickSort(array, j, end);
quickSort(array, start, i);
}|
66
|
55
|
88
|
11
|
44
|
22
|
|
66
|
55
|
88
|
11
|
44
|
22
|
|
55
|
66
|
88
|
11
|
44
|
22
|
|
55
|
66
|
88
|
11
|
44
|
22
|
|
11
|
55
|
66
|
88
|
44
|
22
|
|
11
|
44
|
55
|
66
|
88
|
22
|
|
11
|
22
|
44
|
55
|
66
|
88
|
// 二分法排序
private static void binaryCheck(int[] array) {
for (int i = 0; i < array.length; i++) {
int key = array[i];
int left = 0;
int right = i - 1;
int mid = 0;
// 每一轮查找的逻辑代码
while (left <= right) {
mid = (right + left) / 2;
if (key < array[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
time++;
}
// 区间[left,i-1]整体往后挪动一个单位
for (int j = i - 1; j >= left; j--) {
array[j + 1] = array[j];
}
// 将目标元素插入指定位置
if (left != i) {
array[left] = key;
}
print(test);
}
}
// 希尔排序
private static void shellSort(int[] array, int len) {
int j;
int d = len / 2;
int temp = 0;
while (d > 0) {
for (int i = d; i < len; i++) {
j = i - d;
while (j >= 0 && array[j + d] < array[j]) {
temp = array[j];
array[j] = array[j + d];
array[j + d] = temp;
j = j - d;
time++;
}
print(test);
}
d = d / 2;
}
}// 归并排序
private static void myMerge(int[] array,int first,int sStart,int sEnd){
int[] temp = new int[sEnd - first + 1];
int i = first,j = sStart,k = 0;
while(i < sStart && j <= sEnd){ //因为每一轮归并后,产生的数组中元素将会顺序排列,若A中的第一个元素大于B中的
//最后一个元素,那么A中的其他元素无需比较,直接转移
if(array[i] <= array[j]){
temp[k] = array[i];
i++;
k++;
}else{
temp[k] = array[j];
k++;
j++;
}
}
while(i < sStart){
temp[k] = array[i];
k++;
i++;
}
while(j <= sEnd){
temp[k] = array[j];
k++;
j++;
}
System.arraycopy(temp, 0, array, first, temp.length);
}
private static void mySort(int[] array,int start,int len){
int size = 0; //计算归并的第一组元素的起始位置
int length = array.length;
int mtime = length/(2*len); //归并的数组个数
int rest = length & (2*len - 1); //统计归并的最后一个数组(当数组元素的个数为奇数时,那么在归并的过程中最后一个数组元素个数是奇数)
// 如果在归并过程中数组的个数刚好是偶数那么rest = 0;
if(mtime == 0){
return;
}
for(int i = 0;i < mtime;i ++){
size = 2*i*len;
myMerge(array, size, size + len, size + 2*len - 1);
}
if(rest != 0){
myMerge(array, length - rest - 2*len, length - rest, length - 1);
}
//下一轮归并
mySort(array, 0, 2*len);
}原文:http://blog.csdn.net/xiaoge0103/article/details/51087757