Package cn. by. Struct. Sort;
Public class Sort {
/**
* Insert sorting
*
* Basic idea:
* Each trip inserts a record to be sorted into the sorted record according to its keyword value.
* Some files are properly inserted until all the files are inserted.
*/
/**
* Insert sort directly (insert method)
*
* Insert records one by one to an ordered table in the sorted order to obtain a new ordered table.
*
*/
Public static void chaSort (int [] table ){
Int I, j;
Int shao; // The sentry position.
For (I = 1; I <table. length; I ++) {// starts with an element whose subscript is 1.
Shao = table [I]; // assign the current element to the sentry position each time.
J = I-1; // j indicates the last element of the ordered table.
While (j> = 0 & shao <table [j]) {// control the boundary and locate the location based on the size.
Table [j + 1] = table [j]; // move the element behind to insert shao in a blank position.
J --; // next cycle
}
Table [j + 1] = shao; // locate and insert shao. Why j + 1? Because
// When j is located, it points to an element smaller than or equal to shao. Therefore, add 1 to the shao position j.
}
}
/**
* Shell sorting
*
* Basic idea:
*
* Records are grouped by a certain increment of d according to the subject, and each group of records is sorted by direct insertion and sorting. As the increment decreases gradually, the group package is divided
* More and more records are contained. When the incremental value is reduced to 1, the entire data is merged into a group to form a group of ordered records.
*
* @ Param table
* @ Param n number of elements
*/
Public static void shellSort (int table [], int n ){
Int I;
Int j;
Int shao; // The sentry position.
Int gap = n/2; // The initial increment is the length minus 1.
While (gap> 0 ){
For (I = gap; I <n; I ++) {// sort all elements separated by gaps
Shao = table [I]; // assign the current element to the sentry position each time.
J = I-gap; // j indicates the last element of the ordered table each time.
While (j> = 0 & shao <table [j]) {// sort element groups separated by gaps
Table [j + gap] = table [j]; // The element is moved back so that the shao can be inserted in a blank position.
J = j-gap; // the next cycle
}
Table [j + gap] = shao; // locate and insert shao. Why is j + gap? Because
// When j points to an element smaller than or equal to shao
}
Gap = gap/2 ;//
}
}
/**
* Select sorting
*
* Basic idea:
* In each step, the records with the smallest keyword are selected from the records to be sorted and placed at the end of the sorted record sequence in order,
* Until all rows are completed.
*
*/
/**
* Directly select sorting
*
* Perform n-1 queries. Each query is compared with the remaining elements one by one. If you find a small one, you can exchange it with it.
*/
Public static void selectSort (int table []) {
Int temp; // used for exchange
For (int I = 0; I <table. length-1; I ++ ){
For (int j = I + 1; j <table. length; j ++ ){
If (table [I]> table [j]) {
Temp = table [I];
Table [I] = table [j];
Table [j] = temp;
}
}
}
}
/**
* Exchange sorting
*
* Basic idea:
* Compare the keywords of the records to be sorted, and exchange the even pairs that do not meet the order requirements until all the matching results are met.
*/
/**
* Bubble Sorting
*
* The number of elements adjacent to each other must be n-1. Each trip minus n (current trip) times.
*
* The smallest number emerges from the forward and backward.
* @ Param array refers to an array to be queried.
* @ Return no return value
*/
Public static void maoSort (int [] table ){
/**
* It is mainly used to end the comparison in advance, that is, if there is no element exchange in a trip, there is no need to proceed with the trip that has not yet been conducted.
*/
Int exchange;
// Temporary swap variable
Int temp;
For (int I = 1; I <= table. length-1; I ++) {// Number of partitions to be taken
Exchange = 0; // The initial value is 0.
For (int j = table. length-1; j> I-1; j --) // the number of times each trip is exchanged.
If (table [j] <table [j-1]) {
Temp = table [j];
Table [j] = table [j-1];
Table [j-1] = temp;
Exchange = 1; // change to 1 if there is any exchange
}
// Early termination
If (exchange = 0)
Return;
}
}
/**
* Fast sorting:
*
* Sort the records to be sorted into two separate parts,
* The keywords in one part of the record are smaller than those in the other part.
* The records are sorted to make the whole sequence orderly.
*
* @ Param table-array to be sorted
* @ Param begin-starting source point to be sorted
* @ Param end-the end point to be sorted
*/
Public static void quickSort (int [] table, int begin, int end ){
// Exception Handling. the start point to be sorted cannot be smaller than 0 and cannot be greater than the end point to be sorted
If (begin <0) | (begin> end ))
Return;
Int I = begin;
Int j = end;
Int tem;
// Store the demarcation point. Use the first element of the interval as the benchmark.
Tem = table [begin];
// Scanning from both ends of the interval to the middle until I = j
While (I! = J ){
// Scan right to left to find the first element smaller than tem
While (j> I) & table [j]> tem)
J --;
// Scan from left to right to find the first element greater than tem
While (I <j) & table [I] <tem)
I ++;
// Conditional exchange
If (I <j ){
Int change;
Change = table [j];
Table [j] = table [I];
Table [I] = change;
}
}
// Split the record into two parts
Table [I] = tem;
// Sort the records before the demarcation point
QuickSort (table, begin, I-1 );
// Sort partial records after the demarcation point
QuickSort (table, I + 1, end );
}
}
This article is from the "Enthusiasm 10 years" blog