Package com. java. datastructarithmeic. Sort;
/**
* @ Author gongpeibao
* @ Since 2012.03.16
* Basic sorting method
* Selecting sort put is more efficient than bubble sort because it has fewer exchanges and the number of comparisons is N * (N-1)/2
* Insert sorting is the best basic sorting method here. Although the sorting algorithm still requires O (N ^ 2) time,
* In general, the sorting speed is twice faster than the Bubble Sorting speed, and a little faster than the selected sorting speed.
* Application method:
* When the data volume is small and the data exchange time is more time-consuming than the compared data, the application chooses to sort the data.
* If the data volume is small or basically ordered, inserting the sorting algorithm is the best choice.
* Quick sorting is the best method for sorting large data volumes.
*/
Public class bubblesort {
Public static void main (string [] ARGs ){
// Int [] values = {3, 1, 6, 2, 9, 0, 7, 4, 5}; // Array
Int [] values = {3, 1, 6}; // Array
// Bubblesort. bubblesort (values); // Bubble Sorting
// Selectsort (values); // select sorting
Insertsort2 (values); // insert sorting
Int length = values. length;
For (INT I = 0; I <length; I ++ ){
System. Out. println (Values [I]);
}
}
/**
* Bubble Sorting
* @ Param values: Sorting Array
*/
Public static void bubblesort (INT [] values ){
Int length = values. length;
// The external for loop controls the number of workers.
For (INT I = 0; I <length; I ++ ){
// The for loop control comparison times
For (Int J = 0; j <length-I-1; j ++ ){
// Exchange data if conditions are met
If (Values [J]> values [J + 1]) {
Int temp = values [J];
Values [J] = values [J + 1];
Values [J + 1] = temp;
}
}
}
}
/**
* Select sorting method
* Switching times have been reduced from O (N ^ 2) to O (n)
* @ Param values: Sorting Array
* The Idea Of directly selecting a sorting algorithm is relatively simple: (assume that the data is placed in array A and the length of the array is N)
* Selects the smallest data from a [0]-A [N-1] and exchanges the location with a [0]
* Select the smallest data from a [1]-A [N-1], and then exchange the location with a [1] (after step 1, a [0] is the minimum value of N)
* Selects the smallest data from a [2]-A [N-1] and exchanges it with a [2] (after step 1, a [1] is the minimum number of N-1)
* Similarly, after the N-1 is sorted, the data to be sorted is arranged in the ascending order.
**/
Public static void selectsort (INT [] values ){
Int length = values. length;
For (INT I = 0; I <length-1; ++ I)
{
Int K = I; // records the position of the first element as the benchmark
For (Int J = I; j <length; ++ J)
{
// If there is a value smaller than the benchmark, use a smaller value as the new benchmark and select the smallest value.
If (Values [k]> values [J])
{
K = J;
}
}
If (K! = I)
{
// Exchange Element
Int temp = values [I];
Values [I] = values [k];
Values [k] = temp;
}
}
}
/**
* Insert sorting method
* @ Param values: Sorting Array
*/
Public static void insertsort (INT [] values ){
Int length = values. length;
For (INT I = 1; I <length; I ++ ){
Int J =-1;
// Locate the location where the element [I] should be placed. The search algorithm can be used for optimization.
While (j <= I & values [I]> values [++ J])
{
System. Out. println (Values [I] + "-" + values [++ J]);
}; // Semicolon for null Processing
If (j <I ){
// Move the data after J to one place, and then move values [I] to J.
Int temp = values [I]; // loop the second value of the array into temp
For (int K = I-1; k> = J; k --){
Values [k + 1] = values [k];
}
Values [J] = temp;
}
}
}
/**
* Insert sorting method 2 is easier to understand
* @ Param values: Sorting Array
* Immutability
* At the end of each trip, after the item at the temp position is inserted, the data items with a lower mark smaller than the outer are partially ordered.
*/
Public static void insertsort2 (INT [] values ){
Int length = values. length;
For (INT I = 1; I <length; I ++ ){
// Cyclically place the second value of the array in temp
Int TMP = values [I];
Int J = I-1; // compare from the first on the left
While (TMP <values [J]) {// Loop
Values [J + 1] = values [J]; // move one to the right
J --;
If (j =-1) // checks whether the current element is the first element. If it is the first element, the loop is stopped.
Break;
}
Values [J + 1] = TMP; // place the obtained part in a smaller position than its own.
}
}
}