package comsxsexestudyalgorithm;
public class SortTest {
private static final int ARRAY_SEED = ;
private static final int ARRAY_LENGTH = ;
private static int[] array = new int[ARRAY_LENGTH];
private static void initArray() {
for (int i = ; i < ARRAY_LENGTH; i++) {
array[i] = (int) (Mathrandom() * ARRAY_SEED);
}
}
private static void printArray(boolean before) {
String s = null;
if (before)
s = before :;
else
s = after :;
Systemoutprint(s);
for (int i = ; i < ARRAY_LENGTH; i++) {
Systemoutprint(array[i] + );
}
Systemoutprintln( );
}
/**
* @param args
*/
public static void main(String[] args) {
initArray();
// printArray(true);
long startTime = SystemcurrentTimeMillis();
quickSort( ARRAY_LENGTH );
long endTime = SystemcurrentTimeMillis();
// printArray(false);
Systemoutprintln(quick sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);
initArray();
// printArray(true);
startTime = SystemcurrentTimeMillis();
insertSort();
endTime = SystemcurrentTimeMillis();
// printArray(false);
Systemoutprintln(insert sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);
initArray();
// printArray(true);
startTime = SystemcurrentTimeMillis();
selectSort();
endTime = SystemcurrentTimeMillis();
// printArray(false);
Systemoutprintln(select sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);
initArray();
// printArray(true);
startTime = SystemcurrentTimeMillis();
bubbleSort();
endTime = SystemcurrentTimeMillis();
// printArray(false);
Systemoutprintln(bubble sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);
initArray();
// printArray(true);
startTime = SystemcurrentTimeMillis();
binarySearchSort();
endTime = SystemcurrentTimeMillis();
// printArray(false);
Systemoutprintln(binarySearchSort sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);
}
private static void quickSort(int left int right) {
int key = array[left];
int tmp;
int start = left;
int end = right;
while (left < right) {
// 首先从右向左找 找到一个小于key的值 进行交换 然后准备从左向右找
if (array[right] >= key) {
right;
continue;
}
// 交换
tmp = array[right];
array[right] = array[left];
array[left] = tmp;
// 从左向右找 找到一个大于key的值进行交换 然后循环
if (array[left] <= key) {
left++;
continue;
}
// 交换
tmp = array[right];
array[right] = array[left];
array[left] = tmp;
}
// 以left right为新数组的起点和终点 递归调用
if (start < left)
quickSort(start left);
if (right < end)
quickSort(right + end);
// Systemoutprintln(quick sort over);
}
private static void binarySearchSort() {
int i = middle = tmp;
for (; i < ARRAY_LENGTH; i++) {
int low = ;
int high = i ;
tmp = array[i];
while (low <= high) {
middle = (low + high) / ;
if (tmp > array[middle])
low = middle + ;
else
high = middle ;
}
for (int k = i; k > middle; k)
array[k] = array[k ];
array[low] = tmp;
}
}
private static void insertSort() {
int i = j tmp;
for (; i < ARRAY_LENGTH; i++) {
tmp = array[i];
j = i;
while (j > && array[j ] > tmp) {
array[j] = array[j ];
j;
}
array[j] = tmp;
}
}
private static void selectSort() {
int i = minValue tmp;
int j = i + ;
for (; i < ARRAY_LENGTH ; i++) {
minValue = array[i];
for (j = i + ; j < ARRAY_LENGTH; j++) {
if (array[j] < minValue) {
minValue = array[j];
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
}
}
private static void bubbleSort() {
int i = ;
int j tmp;
for (; i < ARRAY_LENGTH; i++) {
for (j = i + ; j < ARRAY_LENGTH; j++) {
if (array[j] < array[i]) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
}
}
}