The basic idea of Bubble Sorting is: set n elements to be sorted to be stored in array a [n], and the unordered range is (a (0 ), A (1), a (2 ),..., A [n-1]), the Bubble sorting method is in the current unordered area, starting from the top element a [0, for each two adjacent elements a [I + 1] and a [I] (I = ,..., n-1), and the elements with smaller values are converted to those with larger values (if a [I]> A [I + 1], then, the values of a [I] And a [I + 1] are exchanged. After a Bubble sorting, assume that the last moving element is a [K]. then, the elements with a large value in the unordered area reach the lower end and are stored in a [k + 1], a [K + 2] in sequence,... in a [n-1], the unordered range is (a [0], a [1], a [2],..., A [k]). Perform the Next Bubble Sorting in the unordered area. This process ends until there is no element exchange action in a certain sort. The entire sorting process can be executed up to n-1 times. By comparing and exchanging adjacent elements, This sorting method gradually moves elements with smaller values from the back to the front (from the unit with larger subscript to the unit with smaller subscript ), gradually rises like bubbles under water. It is called the Bubble sorting method.
1. Sorting Method
The sorted record array R [1. N] is vertically arranged. Each record R [I] is considered as a bubble with the weight of R [I]. Key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(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.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. N] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R [I]. The result is that R [1. I] is changed to a new ordered area.
2. Bubble sorting process example
Keyword sequence: 49 38 65 97 76 13 2749For more information, see animation demonstration]
3. SortingAlgorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to false before each sort starts. If an exchange occurs during the sorting process, set it to true. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.
(2) specific algorithms
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace exebullitionsorter
{
Class ebullitionsorter
{
Public void sort (INT [] ARR)
{
Int I, j, temp;
Bool done = false;
J = 1;
While (j <arr. Length )&&(! Done) // determine the length
{
Done = true;
For (I = 0; I <arr. Length-J; I ++)
{
If (ARR [I]> arr [I + 1])
{
Done = false;
Temp = arr [I];
Arr [I] = arr [I + 1]; // exchange data
Arr [I + 1] = temp;
}
}
J ++;
}
}
Static void main (string [] ARGs)
{
Int [] array = new int [] {1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
Ebullitionsorter E = new ebullitionsorter ();
E. Sort (array );
Foreach (INT m in array)
Console. writeline ("{0}", M );
}
}
}