Java source file Address:
Baidu Cloud: Http://pan.baidu.com/s/1qW6ygzU
Extract code: DNDR
Swap functions:
public static void swap (int array[], int x, int y) {
int t = array[x];
ARRAY[X] = Array[y];
Array[y] = t;
}
First, insert directly:
public static void Insertsort (int[] array) {
if (array = = NULL | | array.length <= 1)
Return
for (int i = 1; i < Array.Length; i++) {
for (int j = i-1; J >= 0 && Array[j] > array[j + 1]; j--) {
Swap (Array, J, j + 1);
}
}
}
Ii. sort of Hill:
public static void Shellsort (int[] array) {
if (array = = NULL | | array.length <= 1)
Return
int length = Array.Length, J;
for (int value = length >> 1; value > 0; value >>= 1) {
for (int i = value; i < length; i++) {
int temp = Array[i];
for (j = i; J >= Value && temp < Array[j-value]; J-= value) {
ARRAY[J] = Array[j-value];
}
ARRAY[J] = temp;
}
}
}
Third, Direct choice:
public static void Choosesort (int[] array) {
if (array = = NULL | | array.length <= 1)
Return
for (int i = 0; i < Array.Length; ++i) {
int min = i;
for (int j = i + 1; j < Array.Length; ++j) {
if (Array[min] > Array[j])
min = j;
}
if (i! = min)
Swap (array, min, i);
}
}
Iv. Heap Sequencing:
public static int Left (int index) {
Return 2 * index + 1;
}
public static int Right (int index) {
Return 2 * index + 2;
}
public static void Maxheapify (int[] array, int size, int index) {
int left = left (index);
int right = right (index);
int largest = index;
if (Left < size && Array[left] > Array[index]) {
largest = left;
}
if (Right < size && Array[right] > Array[largest]) {
largest = right;
}
if (Largest! = Index) {
Swap (array, index, largest);
Maxheapify (array, size, largest);
}
}
public static void Buildmaxheap (int[] array) {
for (int index = ARRAY.LENGTH/2-1; index >= 0;--index) {
Maxheapify (array, array.length, index);
}
}
public static void Heapsort (int[] array) {
if (array = = NULL | | array.length <= 1)
Return
int size = Array.Length;
Buildmaxheap (array);
for (int i = array.length-1; i > 0; i.) {
Swap (array, 0, i);
--size;
Maxheapify (array, size, 0);
}
}
Five, bubble sort:
public static void Bubblesort (int[] array) {
if (array = = NULL | | array.length <= 1)
Return
for (int i = 0; i < Array.Length; ++i) {
for (int j = 0; J < array.length-i-1; ++j) {
if (Array[j] > array[j + 1]) {
Swap (Array, J, j + 1);
}
}
}
}
Six, quick sort:
public static void QuickSort (int[] array) {
if (array = = NULL | | array.length <= 1)
Return
Doquicksort (array, 0, array.length-1);
}
public static void Doquicksort (int[] array, int begin, int end) {
if (begin < End) {
int random = randomizedpartition (array, begin, end);
Doquicksort (array, begin, random-1);
Doquicksort (array, random + 1, end);
}
}
public static int randomizedpartition (int array[], int begin, int end) {
int random = (int) (Math.random () * (End-begin + 1) + begin);
Swap (array, end, random);
return partition (array, begin, end);
}
public static int partition (int[] array, int begin, int end) {
int value = Array[end];
int index = begin-1;
for (int i = begin; I < end; ++i) {
if (Array[i] <= value) {
++index;
Swap (array, index, i);
}
}
Swap (array, index + 1, end);
return index + 1;
}
Seven, merge sort:
public static void MergeSort (int[] array) {
if (array = = NULL | | array.length <= 1)
Return
Domergesort (array, 0, array.length-1);
}
public static void Domergesort (int[] array, int begin, int end) {
if (begin >= End)
Return
int separate = (begin + End)/2;
Domergesort (array, begin, separate);
Domergesort (array, separate + 1, end);
Domergearray (array, begin, separate, end);
}
public static void Domergearray (int[] array, int begin, int separate,
int end) {
int[] Temparray = new Int[end-begin + 1];
int i = BEGIN;
Int j = separate + 1;
int index = 0;
while (i <=separate && J <= End) {
if (Array[i] < array[j]) {
temparray[index++] = array[i++];
} else {
temparray[index++] = array[j++];
}
}
while (i <= separate) {
temparray[index++] = array[i++];
}
while (j <= end) {
temparray[index++] = array[j++];
}
for (int k = 0; k < temparray.length; ++k) {
Array[begin + K] = temparray[k];
}
}
Eight, the base sort:
public static void Radixsort (int[] array) {
if (array = = NULL | | array.length <= 1)
Return
int max = array[0];
int i, bit;
for (i = 0; i < Array.Length; ++i) {
if (Array[i] > max) {
max = Array[i];
}
}
bit = 0;
while (Max > 0) {
Max/= 10;
++bit;
}
int[] temp = new Int[array.length];
Int[] count = new INT[10];
int divide = 1;
for (i = 0; i < bit; ++i) {
System.arraycopy (array, 0, temp, 0, array.length);
Arrays.fill (count, 0);
for (int j = 0; j < Array.Length; ++j) {
int tempkey = (Temp[j]/divide)% 10;
count[tempkey]++;
}
for (int j = 1; J <; J + +) {
COUNT[J] = Count[j] + count[j-1];
}
for (int j = array.length-1; J >= 0; j--) {
int tempkey = (Temp[j]/divide)% 10;
count[tempkey]--;
Array[count[tempkey]] = temp[j];
}
Divide *= 10;
}
}
Java Implementation sequencing