Review data structure: Sort algorithm (ii)--bubble sort

Source: Internet
Author: User

This review bubbles the sort.

Bubble sort is also a sort of stable, internal sort.
Bubble sort of the basic idea: to the current is not well-sequenced range of all the number, from top to bottom of the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.
Insert sort faster than bubble sort!


Above is the ordinary bubble sorting algorithm, time complexity is O (n^2), this method can only be a trip to the sorting operation can only find a maximum or minimum value, consuming too much time.

Improvement Method 1: We can make the forward and reverse two-time bubbling method in each order, we can get the maximum value and the minimum value at the same time, so that the number of orders is reduced by half. This method image is like oscillating ball, if the two sides have been selected to the maximum and minimum value, then the next sort of time will reduce the sort of distance, the amplitude of the oscillation will be reduced, has been when the amplitude of 0, is not very much like the oscillation of the ball due to the existence of damping, and continuously reduce the amplitude until stopped.

Improvement Method 2: In each sequencing process, with the marker bit records the last interchange in each order of the position, if the last exchange position in 0, then the entire array is sorted. The idea of this improvement is to use the prior information of the previous sort to reduce the number of sorts.


Implementation code:

#include <iostream>using namespace std; void Bubblesort (int a[], int n) {for (int i = 0; i < n-1; i++) {for (int j = 0; J < N-i-1; J + +) {if (A[j] > a[j+1]) swap (A[j], a[j+1]); }}}void bubblesort_2 (int a[], int n) {int low = 0; int. = n-1; int J; while (Low < high) {for (j = low; J < high; j+ +) {if (A[j] > A[j+1]) swap (A[j], a[j+1]);} high--; for (j = high; j > Low; j--) {if (A[j] < a[j-1]) swap (A[j], a[j-1]);} low++; }}void bubblesort_3 (int a[], int n) {int i = n-1;  The last swap position at the beginning while (i > 0)  //  until the interchange position is 0{int pos = 0; for (int j = 0; J < i; j + +)  //per order only to the last position {if (a[ J] > A[j+1]) {pos = j; swap (A[j], a[j+1]);}} i = pos; }}int Main () {int a[] = {1, 4, 8, 6, 2, 7}; Bubblesort_3 (A, 6); for (int i = 0; i< 6; i++) cout<<a[i]<< "; cout<<endl; return 0; }




Review data structure: Sort algorithm (ii)--bubble sort

Contact Us

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.