The basic idea of bubble sorting: (in ascending order, for example) an array of n elements is in principle ordered n-1. For each lying sort, start with the first number and then compare the size of the previous number to the next. If the previous number is larger than the last one, it is exchanged. After such a round, the largest number will appear as the last array element. In the second round, the last number is removed, and the number of the first n-1 is followed by the above steps to find the maximum number, which will be called the second-lowest array element ... after the n-1 round, the sorting was completed.
/* Bubble Sort Analysis Detailed:
Raw data: 28, 30, 19, 2, 23
First trip:
First time: 28, 30, 19, 2, 23
Second time: 28, 19, 30, 2, 23
Third time: 28, 19, 2, 30, 23
Fourth time: 28, 19, 2, 23, 30
Second trip:
First time: 19, 28, 2, 23, 30
Second time: 19, 2, 28, 23, 30
Third time: 19, 2, 23, 28, 30
Third trip:
First time: 2, 19, 23, 28, 30
Second time: 2, 19, 23, 28, 30
Four trips:
First time: 2, 19, 23, 28, 30
*/
Comparison of n elements n-1 trip
Times per trip = number of elements in array-number of times
Code implementation:
Randomly generate a set of 20 elements array values range from [20, 40]
int a[] = {0};
printf("a randomly generated array containing 20 elements: \ n");
For (int i = 0; i < ; i++) {
A[i] = arc4random ()% (+- 1 + ) + 20; //arc4random generating random numbers
printf ("%d", A[i]);
}
for (int i = 0; i < 1; i++) {//Compare the number of trips
for (int j = 0; J < - 1-i; j + +) {//times per trip
if (A[j] > a[j + 1]) {
int temp = a[j];
A[J] = a[j + 1];
A[j + 1] = temp;
}
}
}
printf("\ n bubble sort, produces a set of arrays from small to large order: \ n");
For (int i = 0; i < ; i++) {
printf ("%d", A[i]);
}
The bubble sort of the iOS algorithm (iv)