下面代码主要是排序算法的java实现,针对不同的算法,程序分别对于实现。
1. 桶排序
很简单,时间复杂度是O(M+N),但是得占用的空间很大。比如1,4,2,9999,那么就需要开辟出10000个大小的数组来,造成空间的浪费。算法很不灵活。
2. 冒泡排序
时间复杂度是O(n^2)。基本思想就是逐趟把最小的数字放到数组的最前端。
3. 快速排序
平均的时间复杂度是O(nlgn),但是如果遇见最坏得情况,时间复杂度就变成了O(n^2)与冒泡排序的时间复杂度是一样的。虽然不稳定,但是是经常使用的一种排序方法。
4. 选择排序
时间复杂度是O(n^2),基本思想第一次循环,选出来最小的数字,放到数组的开头,然后第二次循环,选出第二小的数字放到开头。所以时间的复杂度是O(n^2).
package chapter1;
public class CheckExistDemo {
// the main function
public static void main(String[] args) {
// test array
int[] testArray = { 12, 35, 99, 18, 76 };
// using bucket sort and printing the result.
CheckExistDemo.bucketSort(testArray);
// using bubble sort and printing the result.
CheckExistDemo.bubbleSort(testArray);
CheckExistDemo.quickSort(testArray, 1, testArray.length - 1);
// print the quick sort result
System.out.print("The quick sort result is: ");
for (int i = 0; i < testArray.length; i++) {
if (i < testArray.length - 1) {
System.out.print(testArray[i] + ", ");
} else {
System.out.print(testArray[i]);
}
}
System.out.println();
// using select sort
CheckExistDemo.selectionSort(testArray);
}
/*
* Bucket sort: the time complexity is O(M+N); This method works as that:
* the size of the bucket is not depending on the size of t he array. It is
* depending on the value of the items of the array.
*/
public static void bucketSort(int[] array) {
System.out.print("The result of bucket sort is: ");
// initialize the whole array list into 0
// since the value of the arraylist is less than 100, then we must
// allocate 99 seats for the whole values
int[] bucket = new int[100];
for (int i = 0; i < 100; i++) {
bucket[i] = 0;
}
// for certain value, the bucket will add 1;
for (int i = 0; i < array.length; i++) {
bucket[array[i]]++;
}
// print the result
for (int i = 0; i < bucket.length; i++) {
if (bucket[i] != 0) {
if (i < bucket.length - 1) {
System.out.print(i + ", ");
} else {
System.out.print(i);
}
}
}
System.out.println();
}
/*
* Bubble sort: Time complexity is O(n^2). This one is not recommended.
*/
public static void bubbleSort(int[] array) {
System.out.print("The bubble sort result is: ");
// swap two integer values
int dwTemp = 0;
// loop the whole array
for (int i = 0; i < array.length; i++) {
// for each loop, if the first value is greater than the second
// value,
// then, swap those two values.
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
dwTemp = array[j];
array[j] = array[j + 1];
array[j + 1] = dwTemp;
}
}
}
// Printing the result.
for (int i = 0; i < array.length; i++) {
if (i < array.length - 1) {
System.out.print(array[i] + ", ");
} else {
System.out.print(array[i]);
}
}
System.out.println();
}
/*
* Quick Sort: it is suitable for sorting big data volume. The
* divide-conquer strategy is used in quicksort.
* Three steps: 1. Choose a pivot value: we take the value of the middle
* element as pivot value, which is in range of sorted values,
* even if it doesn't present in the array.
* 2. Partition: left to left, right to right.
* 3. Sort both parts:
* recursively. Time complexity is: the least one is O(N^2), and the average
* is O(nlgn) Two guards, right guard moves first, only if it detects the
* smaller one and stop. Then, left guard starts to move. If it detects
* greater one, then stop. Swap two values. recursively, detect all the
* values. Why does it work? On the partition step, algorithm divides the
* array into two parts and every element a from the left part is less or equal
* than every b from the right part.
* After completion of the recursion calls, both of the parts become sorted.
*
* Complexity analysis:
* Average complexity is O(nlgn), but the worst case is O(n^2).
*/
// partition step
public static int partition(int[] array, int low, int high){
int dwLeftGuard = low;
int dwRightGuard = high;
int dwTemp = 0;
int dwKey = array[(low + high) / 2];
while(dwLeftGuard <= dwRightGuard){
while(array[dwLeftGuard] < dwKey)
dwLeftGuard ++;
while(array[dwRightGuard] > dwKey)
dwRightGuard --;
if (dwLeftGuard <= dwRightGuard) {
dwTemp = array[dwLeftGuard];
array[dwLeftGuard] = array[dwRightGuard];
array[dwRightGuard] = dwTemp;
dwLeftGuard ++;
dwRightGuard --;
}
}
return dwLeftGuard;
}
public static void quickSort(int[] array, int low, int high) {
int index = CheckExistDemo.partition(array, low, high);
if (low < index - 1) {
quickSort(array, low, index - 1);
}
if (index < high) {
quickSort(array, index, high);
}
}
/*
* Selection sort:
* Find the smallest element using a linear scan;
* Then, find the second smallest element, and move it to the front.
* Again, doing linear scan.
* Time complexity is O(n^2)
*
*/
public static void selectionSort(int[] array){
int dwTemp = 0;
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
dwTemp = array[j];
array[j] = array[i];
array[i] = dwTemp;
}
}
}
// print the result
System.out.print("The selection sort result is: ");
for (int i = 0; i < array.length; i++) {
if (i < array.length - 1) {
System.out.print(array[i] + ", ");
} else {
System.out.print(array[i]);
}
}
}
}