Bubble Sorting: If you don't need to talk about it, go to the Code directly. Pay attention to the difference between the Bubble Method in 2.
Package com. sqtds. algorithm. sort;
/**
* User: sqtds
* Date: 13-1-15
* Time: PM
*/
Public class Bubble {
Public static void sort (int [] array ){
Int I, j;
For (I = 0; I <array. length; I ++ ){
For (j = I; j <array. length; j ++ ){
If (array [I]> array [j]) {
Swap (array, I, j );
}
}
}
}
Public static void swap (int [] array, int left, int right ){
Int temp = array [left];
Array [left] = array [right];
Array [right] = temp;
}
Public static void BubbleSort (int [] array)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j, t;
Boolean exchange; // exchange flag
For (I = 0; I <array. length; I ++) {// sorting a maximum of N-1 troughs
Exchange = false; // The exchange flag should be false before this sort starts.
For (j = array. length-2; j> = I; j --) // scan the current unordered zone R [I.. n] from bottom to top.
If (array [j + 1] <array [j]) {// exchange records
T = array [j + 1]; // R [0] is not a sentry, only a temporary storage unit
Array [j + 1] = array [j];
Array [j] = t;
Exchange = true; // The exchange flag is set to true due to exchange.
}
If (! Exchange) // This sort order has not been exchanged, and the algorithm is terminated in advance
Return;
} // Endfor (External Loop)
} // BubbleSort
Public static void main (String args []) {
Int [] array = {3, 5, 3, 6, 6, 8, 0 };
// Sort (array );
BubbleSort (array );
For (int I: array ){
System. out. print (I + "");
}
}
}
(1) initial
R [1. n] is an unordered area.
(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.
(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Quick sorting:
(1) Basic Idea of divide and conquer Law
The basic idea of the division and control law is to break down the original problem into several subproblems with smaller sizes but similar structures as the original problem. Recursively solve these subproblems, and then combine the solutions of these subproblems into the solutions of the original problem.
(2) Basic Idea of fast sorting
Set the unordered area to R [low .. high], and describe the basic idea of fast sorting using the division and control method as follows:
① Decomposition:
In R [low .. select a record in high] as the benchmark to divide the current disordered zoning into two smaller subintervals (left and right) R [low .. pivotpos-1) and R [pivotpos + 1 .. high], and make the keywords of all records in the left subinterval less than or equal to the benchmark record (may be recorded as the benchmark) keyword. key, the key of all records in the subinterval on the right is greater than or equal to limit. the benchmark record is located at the correct position (pivotpos), and does not need to be sorted in the future.
Note:
The key to division is to determine the location of the benchmark record, TPOs. The division result can be simply expressed as (note that partition = R [pivotpos]):
R [low... pivotpos-1]. keys ≤ R [small TPOs]. key ≤ R [small TPOs + 1 .. high]. keys
Here, low ≤ TPOs ≤ high.
② Solution:
Use recursive call to quickly sort the Left and Right subintervals R [low... pivotpos-1] and R [small TPOs + 1 .. high.
③ Combination:
Because when the two recursive calls in the "solving" Step end, the left and right subintervals are ordered. For quick sorting, the "Combination" step does not need to be done, and can be considered as a null operation.
Package com. sqtds. algorithm. sort;
/**
* User: sqtds
* Date: 13-1-14
* Time: AM
*/
Public class QuickSort {
/**
*
* @ Param array
* @ Param left
* @ Param right
*/
Public static void qucikSort (int [] array, int left, int right ){
// Get a number from array
// Foreach array, if the number <the last number, put the last number in the font of the number
// Then sort the two array spilt by the number.
If (left <right ){
Int temp = array [left];
Int point = left;
Int j = right, I = left + 1;
While (I <j ){
While (j> = I & point! = J + 1 ){
If (array [j] <temp ){
Swap (array, j, point );
Point = j;
}
J --;
}
While (I <= j & point! = I-1 ){
If (array [I]> temp ){
Swap (array, I, point );
Point = I;
}
I ++;
}
}
For (int a: array ){
System. out. print (a + "");
}
System. out. println ("");
QucikSort (array, point + 1, right );
QucikSort (array, left, point-1 );
}
}
Public static void swap (int [] array, int x, int y ){
Int temp = array [x];
Array [x] = array [y];
Array [y] = temp;
}
Public static int partition (int array [], int left, int right)
{
// P <-Get a number from array
Int p = array [left];
// Put elements <p to the left side
// Put elements> = p to the right side
Int I = left, j;
For (j = left + 1; j <= right; j ++)
{
If (array [j] <p)
{
I ++;
Swap (array, I, j );
}
}
// Put p in the middle slot which index is invalid
Swap (array, I, left );
Return I;
}
Public static void quicksort (int array [], int left, int right)
{
// Do nothing if left <= right
If (left <right)
{
Int partition = partition (array, left, right );
// Recursive quicksort the left parts and right parts
Quicksort (array, left, skip-1 );
Quicksort (array, rows + 1, right );
}
}
Public static void main (String args []) {
Int [] array = };
/**
*, I = 1, j = 1, p = 0
*, I = 1, j = 2, p = 0
*, I = 2, j = 3, p = 0
*, I = 2, j = 4, p = 0
*...
* Switching I and p
*/
QucikSort (array, 0, array. length-1 );
// Quicksort (array, 0, array. length-1 );
For (int a: array ){
System. out. print (a + "");
}
}
}
Int Partition (SeqList R, int I, int j)
{// When calling Partition (R, low, high), divide R [low... high,
// Return the location of the benchmark record
ReceType benchmark = R [I]; // use the first record of the interval as the benchmark'
While (I <j) {// scanning from both ends of the interval to the middle until I = j
While (I <j & R [j]. key> = keys. key) // equals
J --; // scan from the right to the left to find records whose 1st keywords are smaller than keyword. key. R [j]
If (I <j) // indicates the keyword <keyword. key
R [I ++] = R [j]; // equivalent to switching R [I] and R [j]. After switching, the I pointer adds 1
While (I <j & R [I]. key <= keys. key) // equals
I ++; // scan from left to right to find records with 1st keywords greater than keywords. key R [I]
If (I <j) // indicates that R [I] is found, so that R [I]. key> keys. key
R [j --] = R [I]; // equivalent to switching R [I] and R [j]. After switching, the j pointer is reduced by 1.
} // Endwhile
R [I] = benchmark; // the benchmark record has been located.
Return I;
} // Partition