Int [] Max = {6, 5, 4, 0} is a known array. Sort it in descending order using a fast Sorting Algorithm and return an array.
Public class testquicksort {
Private int [] array = NULL;
Private void quicksort (INT lowest, int highest ){
If (array = NULL | lowest <0 | lowest> = highest
| Highest> = array. Length ){
Return;
}
Int low = lowest;
Int high = highest;
Int key = low ++;
For (; low <= high ;){
If (Key If (array [Key]> array [High]) {
Array [High] = array [Key] + (array [Key] = array [High]) * 0;
Key = high;
}
High --;
}
If (Key> LOW ){
If (array [Key] <array [low]) {
Array [low] = array [Key] + (array [Key] = array [low]) * 0;
Key = low;
}
Low ++;
}
}
Quicksort (lowest, key-1 );
Quicksort (Key + 1, highest );
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Testquicksort test = new testquicksort ();
Int [] array = {6, 5, 2, 9, 7, 4, 0 };
6 5 2 9 7 4 0
0 5 2 9 7 4 6
0 5 2 6 7 4 9
0 5 2 4 7 6 9
0 5 2 4 6 7 9
Test. array = array;
Test. quicksort (0, array. Length-1 );
Int length = test. array. length;
For (INT I = 0; I <length; I ++ ){
System. Out. println (test. array [I]);
}
}
}
Quick sorting is the best internal Sorting Algorithm with comprehensive performance!
One bubble sort
[Algorithm description]
N numbers,
Compare 1st to 2nd. If n [1]> N [2], exchange N [1] with N [2;
Compare 2nd to 3rd. If n [2]> N [3], exchange N [2] with N [3;
..................
Compare the N-1 with the N, if n [N-1]> N [N], Let N [N-1] exchange with N [N;
So compare a circle, the maximum number to the end, and then the remaining number of N-1 for the above processing,
Place the largest number of the N-1 to the position of the N-1,
In the end, the sorting is OK. Because the process is like a bubble, it is called the bubble algorithm.
[Source program 1] sequence bubbling
Protected static void maopaosort1 (INT [] ARR ){
For (INT I = 1; I <arr. length; I ++) {// loops from 1 to n
For (Int J = 0; j <arr. Length-I; j ++) {// loops from 0 to n-I
If (ARR [J]> arr [J + 1]) {
Int TMP = arr [J];
Arr [J] = arr [J + 1];
Arr [J + 1] = TMP;
}
}
}
} // End maopaosort1 func
[Source program 2] Reverse bubble, first compare the N-1 and the N
Protected static void maopaosort2 (INT [] ARR ){
For (INT I = 0; I <arr. Length-1; I ++ ){
For (Int J = arr. Length-1; j> I; j --){
If (ARR [J-1]> arr [J]) {
Int TMP = arr [J-1];
Arr [J-1] = arr [J];
Arr [J] = TMP;
}
}
}
} // End maopaosort2 func
Note: If a flag is added to the above program in the loop, if there is no exchange in this loop, the function will be exited, which can improve the efficiency.
2. Select sorting
[Algorithm description]
Scan all the data and select the minimum data to swap with the 1st data;
Scan all data except 1st, and select the minimum data to swap with 2nd;
..................
Scans all data except the N-2, and selects the smallest data to swap with the N-1;
Sorting completed for N-1 scans
[Source program]
Protected static void selectsort (INT [] ARR ){
For (INT I = 0; I <arr. Length-1; I ++ ){
Int smallno = I;
For (Int J = I + 1; j <arr. length; j ++ ){
If (ARR [J] <arr [smallno]) {
Smallno = J; // select the minimum serial number
}
}
If (smallno! = I) {// the sequence number of the minimum number is changed.
Int TMP = arr [I];
Arr [I] = arr [smallno];
Arr [smallno] = TMP;
}
}
} // End selectsort func
Three insert sorting
[Algorithm description]
First, we can see the sorted array from the first element of the array,
Insert the second element into the sorted array,
Insert the third element to the first two sorted arrays, and so on.
Insert an element x into an ordered array so that the array is still ordered.
1. Scan the array from left to right, and start from 1st array elements K greater than element x,
The following elements shift one digit to the right, and store X in K;
2. If the array element greater than element x does not exist, insert X at the end of the array.
[Source program]
Protected static void insertsort (INT [] ARR ){
Int INS, I = 0, j = 0;
For (I = 1; I <arr. length; I ++ ){
INS = arr [I]; // elements waiting for insertion
If (INS <arr [I-1]) {// elements before I (excluding I) are sorted
J = I-1;
While (j> = 0 & ins <arr [J]) {// note that INS cannot be written. <arr [J] & J> = 0
Arr [J + 1] = arr [J]; // move the elements greater than ins one by one
J --; // move back until the position of the INS is obtained.
}
Arr [J + 1] = INS;
}
} // End for I
} // End insertsort func
Iv. Quick sorting
[Algorithm description]
Quick sorting is an improvement of Bubble sorting. Its basic idea is: Split the data to be sorted into two independent parts by means of a lie-down sorting, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by the second method. The whole sorting process can be recursive to convert the entire data into an ordered sequence.
1. Take the intermediate element of the array as the reference value;
2. Place the element on the left that is greater than the reference value and on the right that is less than the reference value of the intermediate element;
3. Think of all the elements on the left of the intermediate element as an array and recursively execute 1 ~ 4;
4. Think of all the elements on the right of the intermediate element as an array and recursively execute 1 ~ 4;
[Source program]
Protected static void quicksort (INT [] arr, int leftno, int rightno ){
Int mid = (INT) math. Floor (leftno + rightno)/2); // obtain the intermediate point
// Math. Floor (3.6) = 3; math. Ceiling (3.1) = 4. You can use either of the two methods.
Int I = leftno;
Int J = rightno;
Do {
While (ARR [I] <arr [Mid]) I ++;
While (ARR [J]> arr [Mid]) j --;
If (I <= J ){
Int TMP = arr [I];
Arr [I] = arr [J];
Arr [J] = TMP;
I ++;
J --;
}
} While (I <= J );
If (leftno <j) quicksort (ARR, leftno, J );
If (rightno> I) quicksort (ARR, I, rightno );
} // End quicksort func