package sort.demo;
import java.util.Arrays;
import java.util.Calendar;
public class SortDemo {
public static void main(String[] args) {
int[] ints = new int[] { 32, 2342, 234, 2, 90, 1, 5, 67, 8, 35, 122, -23 };
System.out.println("选择排序");
GetSystemTime();
Print(ints);
selectSort(ints);
Print(ints);
GetSystemTime();
System.out.println("开始堆排序");
GetSystemTime();
int[] ps = new int[] { 81, 49, 38, 27, 97, 76, 19, 13 };
Print(ps);
heapSort(ps);
Print(ps);
GetSystemTime();
ps = new int[] { 81, 49, 38, 27, 97, 76, 19, 13 };
Print(mergeSort(ps));
}
public static int[] mergeSort(int[] nums) {
if (nums == null) {
return null;
}
return mergeSort(nums, 0, nums.length - 1);
}
/**
* 归并排序
*
* @param nums
* @param low
* @param high
* 数组下标的上限
* @return
*/
public static int[] mergeSort(int[] nums, int low, int high) {
int mid = (high - low) / 2 + low;
if (low < high) {
mergeSort(nums, low, mid);
mergeSort(nums, mid + 1, high);
merge(nums, low, mid, high);
}
return nums;
}
/**
* 归并操作
*
* @param nums
* @param low
* @param mid
* @param high
*/
private static void merge(int[] nums, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= high) {
if (nums[i] < nums[j]) {
temp[k++] = nums[i++];
} else {
temp[k++] = nums[j++];
}
}
while (i <= mid) {
temp[k++] = nums[i++];
}
while (j <= high) {
temp[k++] = nums[j++];
}
for (int k2 = 0; k2 < temp.length; k2++) {
nums[k2 + low] = temp[k2];
}
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`~~~选择排序~~~~~~~~~~~~~~~~~~
*
*/
/**
* 直接选择排序,每次选择i位置的为最小,然后跟i到n-1位置的元素比较,如果更小,更新min=i,遍历完了之后 交换min和i位置处的元素值
*
* @param table
*/
public static void selectSort(int[] table) {
for (int i = 0; i < table.length - 1; i++) {
int min = i;
for (int j = i + 1; j < table.length; j++)
if (table[j] < table[min])
min = j;
if (min != i) {
int temp = table[i];
table[i] = table[min];
table[min] = temp;
}
System.out.print("第" + i + "次选择排序");
Print(table);
}
}
/**
* 关于堆排序有错误
*
* @param table
*/
public static void heapSort(int[] table) {
if (table == null)
return;
int temp;
int length = table.length;
for (int i = length / 2 - 1; i >= 0; i--) {
shift(table, i, length - 1);
}
Print(table);
for (int j = length - 1; j > 0; j--) {
temp = table[0];
table[0] = table[j];
table[j] = table[0];
shift(table, 0, j - 1);
}
}
private static void shift(int[] table, int begin, int end) {
int i = begin, j = 2 * i + 1;
int temp = table[i];
while (j <= end) {
if ((j < end) && (table[j] > table[j + 1]))
j++;
if (temp > table[j]) {
table[i] = table[j];
i = j;
j = 2 * i + 1;
} else {
break;
}
}
table[i] = temp;
}
/**
* 冒泡排序
*
* @param table
*/
public static void bubbleSort(int[] table) {
boolean exchange = true;
for (int i = 1; i < table.length && exchange; i++) {
exchange = false;
for (int j = 0; j < table.length - i; j++)
if (table[j] > table[j + 1]) {
int temp = table[j];
table[j] = table[j + 1];
table[j + 1] = temp;
exchange = true;
}
}
}
public static void quickSort(int[] table) {
quickSort(table, 0, table.length - 1);
}
private static void quickSort(int[] table, int begin, int end) {
if (begin < end) {
int L = begin, R = end;
int base = table[R];
while (L != R) {
while (L < R && table[L] <= base)
L++;
if (L < R)
table[R--] = table[L];
while (L < R && table[R] >= base)
R--;
if (L < R)
table[L++] = table[R];
}
table[L] = base;
quickSort(table, begin, L - 1);
quickSort(table, L + 1, end);
}
}
/**
* 以对int关键字的插入排序, 插入排序的关键点是,每趟只是插入一个关键字到一个有序的序列中 对于n个元素的数组需要n-1趟扫描插入
*
* @param table
*/
public static void insertSort(int[] table) {
Print(table);
for (int i = 1; i < table.length; i++) {
int temp = table[i], j;
for (j = i - 1; j >= 0 && temp < table[j]; j--)
table[j + 1] = table[j];
table[j + 1] = temp;
System.out.print("第" + i + "次插入 ");
Print(table);
}
}
/**
* 折半插入排序只是减少了寻找插入位置的寻找次数,但是找到了插入位置后移动元素的位置还是一样那么多 。计算过程:对于插入第i个元素,计算 0 ~
* i-1 的中间点,用 i 索引处的元素与中间值进行比较,如果 i 索引处的元素相比中间节点大,
* 说明要插入的这个元素应该在中间值和刚加入i索引之间,反之,就是在刚开始的位置
* 到中间值的位置,这样很简单的完成了折半;确定位置之后,将整个序列后移,并将元素插入到相应位置
*
* @param arr
* @return
*/
private static int[] insertBinarySort(int[] arr) {
int temp, hight, low;
for (int i = 1; i < array.length; i++) {
temp = array[i];
hight = i - 1;
low = 0;
int middleIndex;
int locationIndex = hight;
while (low <= hight) {
middleIndex = ((hight - low) / 2) + low;
if (array[middleIndex] > temp) {
hight = middleIndex - 1;
locationIndex = hight;
} else if (array[middleIndex] < temp) {
low = middleIndex + 1;
locationIndex = low;
} else {
locationIndex = middleIndex;
break;
}
}
if (locationIndex < 0) {
locationIndex = 0;
} else if (locationIndex >= i) {
locationIndex = i;
}
for (int j = i - 1; j >= locationIndex; j--) {
array[j + 1] = array[j];
}
array[locationIndex] = temp;
}
return array;
}
public static void shellSort(int[] table) {
for (int delta = table.length / 2; delta > 0; delta /= 2) {
for (int i = delta; i < table.length; i++) {
int temp = table[i], j;
for (j = i - delta; j >= 0 && temp < table[j]; j -= delta)
table[j + delta] = table[j];
table[j + delta] = temp;
}
Print(table);
}
}
public static void GetSystemTime() {
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime());
}
private static void Print(int[] table) {
for (int i = 0; i < table.length; i++)
System.out.print(table[i] + " ");
System.out.println();
}
}