# Three implementations of bubble sort

Source: Internet
Author: User

Bubbling sort is very easy to understand and implement, with an example from small to large:

Set the array length to n.

1. Compare the next two data, if the previous data is larger than the data, two data will be exchanged.

2. So that the No. 0 data of the array to N-1 data after one traversal, the largest one of the data is "sink" to the N-1 position of the array.

3. N=n-1, if N is not 0, repeat the previous two steps, otherwise the sort is complete.

It's easy to write code by definition:

[CPP]View PlainCopy
1. Bubble Sort 1
2. void BubbleSort1 (int a[], int n)
3. {
4. int I, J;
5. For (i = 0; i < n; i++)
6. For (j = 1; j < N-i; J + +)
7. if (a[j-1] > a[j])
8. Swap (A[j-1], a[j]);
9. }

The following optimizes it to set a flag that is true if the interchange occurred, otherwise false. Obviously if there was a trip without an exchange, the description sort has been completed.

[CPP]View PlainCopy
[CPP]View PlainCopy
1. Bubble Sort 2
2. void BubbleSort2 (int a[], int n)
3. {
4. Int J, K;
5. BOOL Flag;
6. k = n;
7. Flag = true;
8. While (flag)
9. {
10. Flag = false;
11. For (j = 1; j < K; J + +)
12. if (a[j-1] > a[j])
13. {
14. Swap (A[j-1], a[j]);
15. Flag = true;
16. }
17. k--;
18. }
19. }

Further optimization is done. If there are 100 number of arrays, only the first 10 unordered, and the next 90 are all ordered and are greater than the preceding 10 digits, then after the initial traversal, the position of the last interchange must be less than 10, and the position after the data must be ordered, record the position, The second time, just walk from the array head to this position.

[CPP]View PlainCopy
1. Bubble Sort 3
2. void BubbleSort3 (int a[], int n)
3. {
4. Int J, K;
5. int flag;
6. Flag = N;
7. While (Flag > 0)
8. {
9. k = Flag;
10. Flag = 0;
11. For (j = 1; j < K; J + +)
12. if (a[j-1] > a[j])
13. {
14. Swap (A[j-1], a[j]);
15. flag = j;
16. }
17. }
18. }

After all, bubble sorting is an inefficient sort method, which can be used when the data size is very small. When the data size is large, it is best to use other sorting methods.

Three implementations of bubble sort

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.