Algorithm analysis and improvement of bubble sort
The basic idea of exchange sorting is: 22 Compare the keywords of the records to be sorted, and find that the order of the two records is reversed to exchange until there is no reverse-order record.
The main sorting methods for applying the basic idea of exchange sorting are bubble sort and quick sort.
Copy Code code as follows:
public class Bubblesort implements sortutil.sort{
public void sort (int[] data) {
int temp;
for (int i=0;i<data.length;i++) {
for (int j=data.length-1;j>i;j--) {
if (Data[j]<data[j-1]) {
Sortutil.swap (data,j,j-1);
}
}
}
}
Bubble Sort
1, sorting method
The sorted array of records R[1..N] is arranged vertically, and each record R is considered to be a bubble with a weight of r.key. According to the principle that light bubbles cannot be under a heavy bubble, scan the array r from bottom to top "float" the light bubbles that violate this principle. So repeatedly, until the end of any two bubbles are light on the top, the heavy in the next.
(1) Initial
R[1..N] is a unordered area.
(2) First scan
From the bottom of the unordered area to compare the weight of the adjacent two bubbles, if the light is found in the lower, the heavy on, then exchange the position of the two. That is to compare (R[n],r[n-1]), (r[n-1],r[n-2]), ..., (r[2],r[1]), and for each pair of bubbles (r[j+1],r[j), if R[j+1].key<r[j].key, Exchange r[j+1] and r[j] contents.
At the end of the first scan, the "lightest" bubbles float to the top of the range, where the minimum number of keywords is placed at the highest position r[1].
(3) The first two trip scan
Scan R[2..N]. When the scan is complete, the "secondary light" bubbles float to the r[2] position ...
Finally, the ordered area R[1..N can be obtained by n-1 Scan]
Note: When I scan, r[1..i-1] and r[i. N] is the current ordered and unordered regions respectively. The scan is still up from the bottom of the unordered area up to the top of the area. When the scan is complete, the lightest bubbles in the zone float to the top position r, and the result is r[1..i] into a new ordered region.
2. Sample bubbling sort process
The process of bubbling a file with a keyword sequence of 49 38 65 97 76 13 27 49
3, sorting algorithm
(1) Analysis
Because each sort of order has added a bubble in the ordered area, after the n-1, in the ordered area there are n-1 bubbles, and the bubble in the disordered area is always greater than the weight of the bubble in the ordered area, so the whole bubble sort process needs to be n-1.
If the exchange of bubbles is not found in a certain sort of order, then all bubbles in the unordered area to be sorted satisfy the light on and the heavier, so the bubbling sort process can be terminated after this trip sort. To do this, in the algorithm given below, introduce a Boolean exchange and set it to false before the start of each trip. If an exchange occurs during the sort process, it is set to true. Check exchange at the end of each trip, and terminate the algorithm if no swap has occurred, and no longer proceed to the next sort.
(2) Specific algorithm
Copy Code code as follows:
void Bubblesort (Seqlist R)
{//r (L..) N) is a sorted file, with a bottom-up scan and a bubble sort on r
int i,j;
Boolean Exchange//Exchange flag
for (i=1;i<n;i++) {//up to do n-1 trip sort
Exchange=false//The Exchange mark should be false before the start of this trip sort
for (j=n-1;j>=i;j--)//R[i the current unordered area. N] from the bottom up scan
if (R[j+1].key<r[j].key) {//Exchange records
R[0]=R[J+1];//r[0] Not a sentinel, just a staging unit.
R[J+1]=R[J];
R[J]=R[0];
Exchange=true///exchange of exchange marks as True
}
if (!exchange)//This trip sort does not occur the exchange, the early termination algorithm
Return
}//endfor (Outer loop)
}//bubblesort
4, algorithm analysis
(1) The best time complexity of the algorithm
If the initial state of the file is positive sequence, a scan can complete the sorting. The required number of keyword comparisons C and record movement times M are all up to the minimum value:
Cmin=n-1
Mmin=0.
The best time complexity for bubble sorting is O (n).
(2) The worst time complexity of the algorithm
If the initial file is reversed, a n-1 order is required. Each trip is compared to the N-i keyword (1≤i≤n-1), and each comparison must move the record three times to reach the Exchange record location. In this case, the comparison and the number of moves are maximized:
Cmax=n (n-1)/2=o (n2)
Mmax=3n (n-1)/2=o (n2)
The worst time complexity for a bubble sort is O (N2).
(3) The average time complexity of the algorithm is O (n2)
Although the bubble sort does not necessarily have to be n-1, the average time performance is much worse than the direct insertion order because of the number of times it moves.
(4) Algorithm stability
The bubble sort is in-place ordering, and it is stable.
5. Improved algorithm
The above bubbling sort can also be improved as follows:
(1) Remember the last swap occurrence position lastexchange bubble sort
In each scan, remember where the last swap took place Lastexchange, where the adjacent records were ordered before. When the next sort begins, R[1..lastexchange-1] is an ordered area, R[lastexchange. N] is a unordered area. In this way, a trip sort may allow the current ordered area to expand multiple records, thereby reducing the number of trips ordered. The specific algorithm "see Exercise".
(2) Change the bubble sort of scanning direction
The asymmetry of ① bubble sort
Can be a scan to complete the sorting situation:
Only the lightest bubbles are located in the r[n] position, and the rest of the bubbles are lined up, and the order can be done with just one scan.
The example "12,18,42,44,45,67,94,10" for the initial sequence of keywords requires only one scan.
Need to n-1 to complete the sort of scan:
When only the heaviest bubbles are located in the r[1], the rest of the bubbles are lined up, and a n-1 scan is still required to complete the order.
"Example" for the initial keyword sequence: 94,10,12,18,42,44,45,67 requires seven scans.
Causes of asymmetry caused by ②
Each scan only allows the heaviest bubble to "sink" one position, so that when the heaviest bubbles at the top are sunk to the bottom, a n-1 scan is required.
③ method of improving asymmetry
In order to change the direction of scanning alternately, the asymmetry can be improved.
Java code:
Copy Code code as follows:
Package utils.sort;
/**
* @author Linyco
* By using bubble sort, the elements in the array must implement the comparable interface.
*/
public class Bubblesort implements Sortstrategy
{
/**
* The elements in the array obj are sorted by the bubble sort algorithm
*/
public void sort (comparable[] obj)
{
if (obj = null)
{
throw new NullPointerException ("The argument can not be null!");
}
comparable TMP;
for (int i = 0; i < obj.length; i++)
{
Remember, every time you start from the first one. No more than the last.
for (int j = 0; J < obj.length-i-1; j + +)
{
Compare the adjacent elements, and if the back is small, exchange
if (Obj[j].compareto (Obj[j + 1]) > 0)
{
TMP = Obj[j];
OBJ[J] = obj[j + 1];
Obj[j + 1] = tmp;
}
}
}
}
}