package sort;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* mysort
*
* @author cailu
*
*/
public class MySort {
public static void main(String[] args) {
MySort mySort = new MySort();
mySort.insertSort();
mySort.shellSort();
mySort.selectSort();
mySort.bubbleSort();
mySort.quickSort();
mySort.HeapSort();
mySort.mergingSort();
mySort.radixSort();
}
/**
* 直接插入排序:稳定 时间复杂度:最好O(n),其它O(n^2) 空间复杂度:O(1)
*/
public void insertSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
int temp = 0;
for (int i = 1; i < a.length; ++i) {
temp = a[i];
int j = i - 1;
for (; j >= 0 && temp < a[j]; --j) {
a[j + 1] = a[j];
}
a[j + 1] = temp;
}
for (int i = 0; i < a.length; ++i)
System.out.print(a[i] + "--");
System.out.println();
}
/**
* 希尔排序:不稳定 时间复杂度:O(nlog2n)~O(n^2) 空间复杂度:O(1)
*/
public void shellSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
int d = a.length;
int temp = 0;
while (true) {
d = (int) Math.ceil(d / 2);
for (int x = 0; x < d; x++) {
for (int i = x + d; i < a.length; i += d) {
int j = i - d;
temp = a[i];