Java implements various sorting algorithms and comparisons, and java sorting algorithms
There are seven common sorting algorithms: Select sorting, Bubble sorting, insert sorting, fast sorting, Hill sorting, heap sorting, and merge sorting.
Before learning how to implement and compare specific algorithms, you must first learn the dimensions of algorithm comparison.
First, Stability
Stability refers to whether two elements with equal values exchange positions before and after sorting. If there is no exchange, it is regarded as a stable algorithm; otherwise, it is considered as an unstable sorting algorithm.
Second, time complexity refers to the time required to execute an algorithm. Simply put, it is the speed program for Algorithm Execution.
The third is space complexity, which refers to the memory size occupied by the execution algorithm.
With these basic concepts, let's look at the java Implementation of the above seven sorting algorithms.
/*** <One-sentence function description> */public class AllSort {/*** fast sorting <br/> * unstable sorting, time complexity O (nlogn) ** @ param a * @ param left * @ param right * @ return */public static void quickSort (int [] a, int left, int right) {if (left> = right) return; int I, j, temp; I = left; j = right; temp = a [left]; while (I! = J) {while (a [j]> = temp & I <j) j --; while (a [I] <= temp & I <j) I ++; if (I <j) {int t = a [I]; a [I] = a [j]; a [j] = t; j --;} a [left] = a [I]; a [I] = temp; if (left <I-1) quickSort (a, left, i-1); if (I + 1 <right) quickSort (a, I + 1, right);}/*** select sorting <br/> * unstable sorting, the time complexity is O (n2) ** @ param a * @ return */public static void choseSort (int [] a) {for (int I = 0; I <. length-1; I ++ ){ Int min_index = I; for (int j = I + 1; j <. length; j ++) {if (a [min_index]> a [j]) {min_index = j ;}} if (I! = Min_index) {int temp = a [I]; a [I] = a [min_index]; a [min_index] = temp ;}}} /*** bubble sort <br/> * stable sorting with the time complexity of O (n2) ** @ param a * @ return */public static void bubbleSort (int [] a) {for (int I = 1; I <. length; I ++) {for (int j = 0; j <. length-I; j ++) {if (a [j]> a [j + 1]) {int temp = a [j]; a [j] = a [j + 1]; a [j + 1] = temp ;}}}/*** insert sort directly <br/> * stable sorting, time complexity O (n2) ** @ param a * @ return */public static void insertSort (int [] a) {for (int I = 1; I <. length; I ++) {int temp = a [I]; int pos = I-1; while (pos> = 0 & a [pos]> temp) {a [pos + 1] = a [pos]; pos --;} a [pos + 1] = temp ;}} /*** Hill sorting <br/> * unstable sorting, time complexity O (nlogn) ** @ param a * @ return */public static void shellSort (int [] a) {int d =. length/2; while (d> = 1) {for (int I = 0; I <d; I ++) {for (int j = I + d; j <. length; j = j + d) {int temp = a [j]; int pos = j-d; while (pos> = 0 & a [pos]> temp) {a [pos + d] = a [pos]; pos-= d;} a [pos + d] = temp;} d = d/2 ;}} /*** Merge Sorting <br/> * stable sorting, time complexity O (nlogn ), speed is second only to fast sorting ** @ param a * @ param left * @ param right * @ return */public static void mergeSort (int [] a, int left, int right) {if (left <right) {int middle = (left + right)/2; // recursive mergeSort (a, left, middle) on the left ); // recursive mergeSort (a, middle + 1, right) on the right; // merge (a, left, middle, right );}} private static void merge (int [] a, int left, int middle, int right) {int [] tmpArr = new int [right-left + 1]; int pos = 0; int I = left; // start position on the left int j = middle + 1; // start position on the right while (I <= middle & j <= right) {// select a small number from two arrays and place it in the intermediate array if (a [I] <= a [j]) {tmpArr [pos ++] = a [I ++];} else {tmpArr [pos ++] = a [j ++];} // Add the remaining parts to the intermediate array while (I <= middle) {tmpArr [pos ++] = a [I ++];} while (j <= right) {tmpArr [pos ++] = a [j ++];} // copy the intermediate array back to the original array int start = 0; while (left <= right) {a [left ++] = tmpArr [start ++] ;}/ *** heap sorting <br/> * unstable sorting, time complexity O (nlogn) * @ param a * @ return */public static void heapSort (int [] a) {int lastIndex =. length-1; for (int I = 0; I <lastIndex; I ++) {// create a heap buildMaxHeap (a, lastIndex-I ); // swap (a, 0, lastIndex-I); // System. out. println (Arrays. toString (a) ;}} private static void buildMaxHeap (int [] data, int lastIndex) {// node from lastIndex (last node) the parent node of for (int I = (lastIndex-1)/2; I> = 0; I --) {// k stores the node int k = I; // If the subnode of the current k node has a while (k * 2 + 1 <= lastIndex) {// int biggerIndex = 2 * k + 1 index of the Left subnode of the k node; // if biggerIndex is smaller than lastIndex, that is, if (biggerIndex <lastIndex) {// if the value of the right subnode is large, if (data [biggerIndex] <data [biggerIndex + 1]) {// biggerIndex always records the index of a large subnode biggerIndex ++ ;}} // if the value of a k node is smaller than the value of a large sub-node, if (data [k] <data [biggerIndex]) {// exchange their swap (data, k, biggerIndex); // assign biggerIndex to k to start the next loop of the while loop, and re-ensure that the value of k nodes is greater than the value of its left and right subnodes k = biggerIndex ;} else {break ;}}} private static void swap (int [] data, int I, int j) {int tmp = data [I]; data [I] = data [j]; data [j] = tmp;} public static void main (String [] args) {Random random = new Random (); int [] a = new int [20]; for (int I = 0; I <. length; I ++) {a [I] = random. nextInt (20);} // quickSort (a, 0,. length-1); // mergeSort (a, 0,. length-1); // shellSort (a); // heapSort (a); // choseSort (a); // bubbleSort (a); // insertSort (); system. out. println (Arrays. toString ());}}
The sorting algorithms are compared as follows:
This is an online comparison for your reference only.
I am skeptical about some items, such as merging and sorting. I personally think that the space complexity should not be O (1), but O (nlog2n ). The space complexity of quick sorting is O (1 ).