1. Direct insertion sort
principles: 1. the entire interval is divided into sections ordered disorder section 2.
The first element of a random selection each time interval, select a proper position in the ordered insertion section
thought: regard per step records to be sorted, according to the size of the value is inserted into place already lined array in order to know all inserted last.
Code:
public static void insertSort(int[] array) {
for (int bound = 1; bound < array.length; bound++) {
//bound 下标对应的元素就是待插入元素
//[0,bound)已排序区间
//[bound,array.length)待排序区间
int tmp = array[bound];
int cur = bound-1;
for(; cur >= 0; cur--) {
if(array[cur] > tmp) {
array[cur + 1] = array[cur];
}else {
break;
}
}
array[cur + 1] = tmp;
}
}
Time Complexity: preferably: O (n) worst: O (n ^ 2)
space complexity: O (1)
Stability: Stable
2. Hill sorting
idea is: choose a first integer, the file to be sorted All the records into groups, all of the distance is recorded in the same sub-group, and recording in each group are sorted. Then, extraction, repeating the grouping and sorting work. When reaching = 1, all records in the unified set sorted.
1. Hill sorting is optimized for direct insertion sort.
2. When the gap> 1 is pre-sorted, the purpose is to get closer to an orderly array. When the gap == 1, the array has been close and orderly, so it will soon. In this way the whole, to achieve optimal results. We can achieve comparative performance tests.
Illustration:
code for:
public static void shellSort(int[] array) {
int gap = array.length;
while( gap > 1) {
insertSortGap(array,gap);
gap = gap / 2;
}
insertSortGap(array,1);
}
private static void insertSortGap(int[] array,int gap) {
for(int bound = 1; bound <array.length;bound++) {
int tmp = array[bound];
int cur = bound - gap;
for (; cur >= 0; cur -= gap) {
if (array[cur] > tmp) {
array[cur + gap] = array[cur];
} else {
break;
}
}
array[cur + gap] = tmp;
}
}
Time Complexity: preferably: O (n) worst: O (n ^ 2)
space complexity: O (1)
Stability: Unstable
3. Direct selection sort
Principle: Each disorder is selected from the maximum interval ( or minimum) of an element stored in the last (or most disorderly front section), until all the data elements to be sorted drained.
public static void selsctSort(int[] array) {
for(int bound = 0;bound < array.length;bound++) {
for(int cur = bound + 1;cur < array.length; cur++) {
if(array[cur] < array[bound]) {
swap(array,cur,bound);
}
}
}
}
private static void swap(int[] array,int x,int y) {
int tmp = array[x];
array[x] = array[y];
array[y] = tmp;
}
Time complexity: O (n ^ 2)
space complexity: O (1)
Stability: Unstable
4. HEAPSORT
Principle: The basic principle is to select the sort, but not in use to traverse the way to find the maximum number of random intervals, but to select a range of disorderly heap by the greatest number.
Note: To build piles in ascending row; ranked in descending order to build a small heap.
Code:
public static void heapSort(int[] array) {
//1.创建堆
createHeap(array);
//2.循环取出堆顶最大值,放到最后面
for (int i = 0; i < array.length; i++) {
swap(array,0,array.length - i - 1);
shiftDown(array,array.length - i - 1,0);
}
}
private static void shiftDown(int[] array,int size,int index) {
int parent = index;
int child = 2 * parent + 1;
while(child < size) {
if(child + 1 < size
&& array[child + 1] > array[child]) {
child = child + 1;
}
if(array[child] > array[parent]) {
swap(array,child,parent);
}else {
break;
}
parent = child;
child = 2 * parent + 1;
}
}
private static void createHeap(int[] array) {
for(int i = (array.length - 1 - 1) / 2;i >= 0;i--) {
shiftDown(array,array.length,i);
}
}
private static void swap(int[] array,int x,int y) {
int tmp = array[x];
array[x] = array[y];
array[y] = tmp;
}
Time complexity: O (n * log (n ))
space complexity: O (1)
Stability: Unstable