Basic algorithm Research 1-bubble sort algorithm Test 1, the basic principle of the classical bubble sorting method
First look at a dynamic diagram, feel the comparative image:
Bubble sort (Bubble sort) is a simple sort algorithm. The default is to order from small to large, that is, the largest data in the last, equivalent to each time the largest data like bubbles floating to the surface of the same. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the series of visits is repeated until there is no need to exchange them again.
Basic steps:
1, compare the adjacent elements. If the first one is bigger than the second one, swap them both.
2, for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number.
3. Repeat the above steps for all elements except the last one.
4. Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare.
The most classic bubble sorting method is the two-layer for loop nesting. The outer for loop is the number of "trips", the inner layer is each "trip" to the adjacent two data size comparison, as needed to exchange positions.
int A[sort_num]; for (int i=0; i<sort_num; i++) a[i]=arc4random ()%sort_num+1;//randomly generated array
for (int i=0; i<sort_num; i++)//outer loop {
for (int j=0; j<sort_num-i-1; j + +)//inner loop {
if (a[j]>a[j+1])//Exchange data { int temp=a[j]; A[J]=A[J+1]; a[j+1]=temp;}}}
2. Bubble Sorting performance
Time complexity: Average: O (n²) Best: O (n²) Worst: O (n²)
Stability: Stable
Space complexity: O (1)
Note: When n is smaller, the sorting effect is better, the advantage is relatively simple. But with the increase of N, time-consuming increases quickly.
3. Actual test results
The bubbling sort was tested using Xcode with the following results
N |
N Multiples |
1th time |
2nd time |
3rd time |
Average |
Time Multiplier |
Use Time (Units) |
10 |
|
18.00 |
25.03 |
14.01 |
19.01 |
|
Microseconds |
100 |
10 |
67.97 |
70.99 |
43.99 |
60.98 |
3.21 |
Microseconds |
1000 |
10 |
2.07 |
1.94 |
2.08 |
2.03 |
33.29 |
Milliseconds |
10 000 |
10 |
298.59 |
265.01 |
279.29 |
280.96 |
138.40 |
Milliseconds |
100 000 |
10 |
29.89 |
31.18 |
32.51 |
31.19 |
111.01 |
Seconds |
200 000 |
2 |
129.95 |
122.60 |
128.04 |
126.98 |
4.07 |
Seconds |
It is basically possible to find that the amount of time that is used is n² times as the data size increases to the previous n times. For example, 10,000 data time is 280.96 milliseconds, 100,000 data time is 31.19 seconds, 100,000 data time is 10,000 times the time of 111.01 times data (about 10² times). The time Complexity O (N²) is consistent with the bubble sort method.
The test procedure is as follows:
Run under Xcode.
#import <Foundation/Foundation.h> #define Sort_num 200000//The length of the array to be sorted int main (int argc, const char * argv[]) {#prag Ma Mark--start int a[sort_num]; printf ("not sorted:"); for (int i=0; i<sort_num; i++) {//randomly generated array a[i]=arc4random ()%sort_num+1; printf ("%d", a[i]); } NSDate * Starttime=[nsdate Date]; Starttime=[starttime Datebyaddingtimeinterval:60*60*8]; for (int i=0; i<sort_num; i++)//Outer loop {for (int j=0; j<sort_num-i-1; j + +)//inner loop { if (a[j]>a[j+1])//Exchange data {int TEMP=A[J]; A[J]=A[J+1]; A[j+1]=temp; }}} NSDate * endtime=[nsdate Date]; Endtime=[endtime Datebyaddingtimeinterval:60*60*8]; Nstimeinterval Sorttime=[endtime Timeintervalsincedate:starttime]; printf ("\ n sort: \ n"); for (int i=0; i<sort_num; i++)//printf ("%d", a[i]); NSLog (@ "Start time:%@", StArttime); NSLog (@ "End time:%@", endTime); NSLog (@ "Use time:%.2f microseconds", sorttime*1000000); NSLog (@ "Use time:%.2f MS", sorttime*1000); NSLog (@ "Use Time:%.2f sec", sorttime); printf ("\ n bubble sort \ n"); #pragma mark--end return 0;}
Basic algorithm Research 1-bubble sort algorithm test