First, let's take a look at the efficiency of the bubble sorting algorithm and the quick sorting algorithm:
The main method is as follows:
| The code is as follows: |
Copy code |
/**
*
* @ Description:
* @ Author: cuiyaonan2000@163.com
* @ Date 1:02:10, January 1, November 5, 2014
*/
Public static void main (String [] args ){
// Fast sort algorithm test
Int [] qArray = new int [100000];
For (int I = 0; I <100000; I ++ ){
QArray [I] = (int) (Math. random () * 100000 );
}
Long beforeQ = System. currentTimeMillis ();
QuickSort (qArray, 0, qArray. length-1 );
System. out. println ("quick sort run time:" + (System. currentTimeMillis ()-beforeQ ));
// Bubble sort algorithm test
Int [] bArray = new int [100000];
For (int I = 0; I <100000; I ++ ){
BArray [I] = (int) (Math. random () * 100000 );
}
Long beforeB = System. currentTimeMillis ();
Bubble (bArray );
System. out. println ("Bubble sort run time:" + (System. currentTimeMillis ()-beforeB ));
} |
The sorting result in an array with 100000 numbers is as follows:
The following is a familiar bubble algorithm:
| The code is as follows: |
Copy code |
/**
*
* @ Description:
* @ Author: cuiyaonan2000@163.com
* @ Date 1:00:32, January 1, November 5, 2014
*/
Public static void bubble (int [] data ){
For (int I = 0; I <data. length-1; I ++ ){
For (int j = I + 1; j <data. length; j ++)
If (data [I]> data [j]) {
Int temp = data [j];
Data [j] = data [I];
Data [I] = temp;
}
}
} |
Let's talk about the idea of a fast sorting algorithm:
Select the first number of the array as the key. Set the variable I to 0 and j to the array length.
Start from the last digit of the array and look forward. What are you looking? Find a number smaller than the key (not equal to) and record coordinate j. the condition is that if a value smaller than the key is not found during the forward search, stop when I <j (if j is not found, perform the minus operation to continue searching ). if the result is found, replace and end the values of array [j] and array [I.
Start from the first part of the array and look backward. What are you looking? Find a number greater than the key (not equal to) and record the coordinate I. the condition is that if a value greater than the key is not found during the forward search, stop when I <j (if I is not found, add one ). if the result is found, replace and end the values of array [j] and array [I.
After completing the preceding operations, print the output array and find that the data is relatively ordered, that is, the key value coordinates in the array are earlier than the key, and the key value coordinates are greater than the key value,
So everyone understands: split the array with the key value as the coordinate into two arrays, and perform the step 123 operation respectively, then an ordered array will be generated.
The algorithm is as follows:
| The code is as follows: |
Copy code |
/**
*
* @ Description:
* @ Author: cuiyaonan2000@163.com
* @ Date 1:02:45, January 1, November 5, 2014
*/
Public static void quickSort (int [] array, int begin, int end ){
Int theKey = array [begin]; // sets the key value.
Int I = begin;
Int j = end;
While (true ){
While (I <j & array [j]> = theKey) // Find a numeric coordinate smaller than the key.
J --;
If (I <j) {// exchange
Int temp = array [j];
Array [j] = array [I];
Array [I] = temp;
} Else {
Break;
}
While (I <j & array [I] <= theKey) // locate a numeric coordinate greater than the key.
I ++;
If (I <j) {// exchange
Int temp = array [j];
Array [j] = array [I];
Array [I] = temp;
} Else {
Break;
}
}
If (-- I> begin) {// This indicates that only one value is found in the split array. Otherwise, it is called recursively.
QuickSort (array, begin, I );
}
If (++ j <end) {// This indicates that only one value is found in the split array. Otherwise, it is called recursively.
QuickSort (array, j, end );
}
}
|