Sorting algorithm complexity

What do you know about the bubble algorithm?

First, we set out the following data

5 8 6 3 9 1 1 7

Under the precondition of bubble sort, first find out if the array is empty

Method 1:

If the array is defined with a vector s, that is:

vector nums;

//or

vector& nums;

Then write as follows:

if nums.size() == 0:

return false

Method 2:

If the array is defined as:

int nums[] = {1,2,3};

First calculate the length of the array:

nums_length = sizeof(nums)/sizeof(nums[0])

Then judge that the array is empty:

if nums_length == 0:

return false

Original Link: https://blog.csdn.net/qq_40977108/article/details/99290544

After resolving the above problem, the original bubble sort is as follows

#include <iostream> #include <string> using namespace std; void BubbleSort(int a[], int n) { int i, j, temp;//Used to control internal and external loops temp as a temporary variable exchange for (i = 0; i < n; i++) { for (j = 0; j < n - 1; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } for (i = 0; i < n; i++) cout << a[i] << " "; } int main() { int a[8] = { 5,8,6,3,9,1,1,7 }; int b[4] = { 0,2,3,4 }; int count = 0, i = 0; count = sizeof(a) / sizeof(a[0]);//There is no direct function to find the length of an array in c++. This method is needed to get the length of an array. BubbleSort(a,count); return 0; }

The bubble algorithm mentioned above has some drawbacks when we think about it further. For example, when he was already sorted in the fifth round of sorting, the next few rounds would be white and waste time comparing.

So, to improve the above code, create a flag.Determine if there is already an order in the middle and exit early if there is one, then the improved code is as follows

#include <iostream> #include <string> using namespace std; void BubbleSort(int a[], int n) { int i, j, temp;//Used to control internal and external loops temp as a temporary variable exchange for (i = 0; i < n; i++) { int flag=1; for (j = 0; j < n - 1; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; flag=0; } } if(flag) break; } for (i = 0; i < n; i++) cout << a[i] << " "; } int main() { int a[8] = { 5,8,6,3,9,1,1,7 }; int b[4] = { 0,2,3,4 }; int count = 0, i = 0; count = sizeof(a) / sizeof(a[0]);//There is no direct function to find the length of an array in c++. This method is needed to get the length of an array. BubbleSort(a,count); return 0; }

Then a new column is used to judge the superiority of the above bubble algorithm.

3 4 2 1 5 6 7 8

The first half of the list is out of order, the second half is in order, and the right half is already in order, but there are so many white comparisons in each round that the above algorithm needs further improvement.

At this point, after each round of sorting, you can record the location of the last element exchange, which is the boundary of the unordered column, and then the location of the ordered area, so the improved code is as follows

#include <iostream> #include <string> using namespace std; void BubbleSort(int a[], int n) { int i, j, temp;//Used to control internal and external loops temp as a temporary variable exchange int last_exchange=0; int Bubble_Sort_border=n-1;//Boundary of Unordered Array Comparison for (i = 0; i < n; i++) { int flag=1; for (j = 0; j<Bubble_Sort_border; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; flag=0; last_exchange=j; } } Bubble_Sort_border=last_exchange; if(flag) break; } for (i = 0; i < n; i++) cout << a[i] << " "; } int main() { int a[8] = { 3,4,2,1,5,6,7,8 }; int b[4] = { 0,2,3,4 }; int count = 0, i = 0; count = sizeof(a) / sizeof(a[0]);//There is no direct function to find the length of an array in c++. This method is needed to get the length of an array. BubbleSort(a,count); return 0; }

Is this the end of the bubble algorithm, no way, you're looking at this string

2 3 4 5 6 7 8 1

Whether the above code can solve the problem well, clearly only adjust a number to complete the sorting, but also white comparison of seven rounds, how to solve this problem?

Based on the bubble algorithm, an algorithm is extended called

Cocktail sort

The sorting of cocktails is a two-way process

Do you want to sort it forward first or in the same order as the bubble algorithm?

First round, 1 and 8 exchange,

The second round of reverse comparison lets 1 move forward gradually. After the second round of comparison is completed, it is actually ordered, and then the third round of comparison is completed. After the third round of comparison is completed, there is already order. Since flag is set, we only need three rounds of this comparison. Is it much more efficient? Then the specific code is as follows:

#include <iostream> #include <string> using namespace std; void BubbleSort(int a[], int n) { int i, j, temp;//Used to control internal and external loops temp as a temporary variable exchange for (i = 0; i < n/2; i++)//Odd number wheel { int flag=1; for (j = i; j<n-i-1; j++)//Note at this point that the initial value of j is i because the first position of the sort must be ordered every time whether it is odd or even { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; flag=0; } } if(flag) break; // flag reset to 1 before even round start flag=1; for (j = n-i-1; j>i; j--)//Note at this point that the initial value of j is i because the first position of the sort must be ordered every time whether it is odd or even { if (a[j] < a[j -1]) { temp = a[j]; a[j] = a[j - 1]; a[j +-1] = temp; flag=0; } } if(flag) break; } for (i = 0; i < n; i++) cout << a[i] << " "; } int main() { int a[8] = { 3,4,2,1,5,6,7,8 }; int b[4] = { 0,2,3,4 }; int count = 0, i = 0; count = sizeof(a) / sizeof(a[0]);//There is no direct function to find the length of an array in c++. This method is needed to get the length of an array. BubbleSort(a,count); return 0; }

These are all the optimization methods for the bubble algorithm.Have you got it yet? Next Share Quick Sort