Bubble Sort front to back / forward from the realization

First, what is the bubble sort

Bubble Sort (Bubble Sort) it is also a simple and intuitive sorting algorithm. It repeatedly visited the number of columns to be sorted, a comparison of two elements, if they put them in the wrong order switching over. The number of visits to the column work is repeated until there is no longer need to swap, that is to say the number of columns already sorted completed. The origin of the name of the algorithm is because the smaller elements will slowly through the exchange of "float" to the top of the columns.

Second, the moving map presentation

* FIG movable from novice tutorial

Third, the implementation

Compare adjacent elements. If the first is greater than the second, the two of them exchanged.

Do the same work for each pair of adjacent elements, from the beginning to the end of the first to the last pair. Once this is done, the last element is the biggest number.

Repeat these steps for all elements, except the last one.

Continued to repeat the above steps for each time fewer and fewer elements, a pair of numbers until there is no need to compare.

Fourth, performance parameters on bubble sort

1, time complexity

the best: O ( n ) O (n) (Ordered
Average: O ( n 2 ) O (n ^ 2)
Worst: O ( n 2 ) O (n ^ 2)

2, space complexity

O ( 1 ) O (1)

3, is stable

Stable - compare and swap procedure, determining the time to the same elements, not exchanged

4, any suitable type of storage

Sequential storage and storage chain

Fifth, code implementation

c++:

1, j=n-1from back to front

* Used here toflag

There is also a bubble sort algorithm optimization is to set up a flag, when the element does not occur in the exchange trip traversal sequence, then prove that the sequence has been ordered. But this improvement to enhance performance and it does not have much effect.

void BubbleSort(ElemType A[],int n ){
    for(int i = 0; i<n-1; i++){		// n-1次冒泡
        bool flag = false;
        for(int j = n - 1; j>i; j--)	// 从后向前
            if(A[j-1] > A[j]){	//逆序
                swap(A[j-1],A[j]);
                flag = true;		// 只有交换了flag才为true
            }
        if(flag == false)		//冒泡完成
            return;
    }
}

2, j=0front to back

This is easy to remember, the amount of one to become a little bit more

void BubbleSort(ElemType A[],int n ){
    for(int i = 0; i<n-1; i++){		// n-1次冒泡
        for(int j = 0; j< n-1-i; j++)	// 从前向后
            if(A[j] > A[j+1]){	//逆序
                swap(A[j],A[j+1]);
            }
    }
}

Examples of implementation:

/*
 * @Descripttion: 
 * @version: 
 * @Author: edisonhuang
 * @Date: 2020-03-09 16:20:29
 * @LastEditors: edisonhuang
 * @LastEditTime: 2020-03-09 16:31:07
 */
#include <iostream>
void BubbleSort1(int A[], int n);
void BubbleSort2(int A[], int n);
void Print(int A[],int n);
using namespace std;

int main()
{
    int arr1[] = {13, 14, 8, 100, 25, 75, 9, 64, 12};
    int len1 = (int) sizeof(arr1) / sizeof(*arr1);
    Print(arr1,len1);
    BubbleSort1(arr1,len1);
    Print(arr1,len1);

    int arr2[] = {13, 14, 8, 100, 25, 75, 9, 64, 12};
    int len2 = (int) sizeof(arr1) / sizeof(*arr1);
    Print(arr2,len2);
    BubbleSort2(arr2,len2);
    Print(arr2,len2);

    return 0;
}

void BubbleSort1(int A[], int n){
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n-1-i; j++)
        {
            if (A[j] > A[j+1])
            {
                swap(A[j],A[j+1]);
            }   
        }
    }
}
void BubbleSort2(int A[], int n){
    for (int i = 0; i <  n - 1 ; i++)
    {
        for (int j = n - 1; j > i; j--)
        {
            if (A[j-1] > A[j])
            {
                swap(A[j-1], A[j]);
            }
        }
    }
    
    
}
void Print(int A[],int n ){
    for (int i = 0; i < n; i++)
    {
        cout << A[i] << " ";
    }
    cout << endl;
}
Published 35 original articles · won praise 1 · views 1842

Guess you like

Origin blog.csdn.net/qq_40672635/article/details/104755589