Sorting of data structures and algorithms (updating...)

Sorting of data structures and algorithms (updating...)

Bubble Sort

Code analysis:

This code implements the bubble sort algorithm with a time complexity of O ( n 2 ) O(n^2)O ( n2 ). Bubble sort is a simple sorting algorithm that iterates over the array to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of traversing the array is repeated until there are no more elements that need to be exchanged, that is to say, the array has been sorted.

In this code, the outer loop in the bubbleSort function is executed n times, and the inner loop is executed n-1 times, so the total time complexity is O ( n 2 ) O(n^2)O ( n2 ). Although the time complexity of bubble sorting is high, it is simple to implement and works well for small-scale data sorting.

Code interpretation:

This code implements the bubble sort algorithm. Bubble sort is a simple sorting algorithm that iterates over the array to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of traversing the array is repeated until there are no more elements that need to be exchanged, that is to say, the array has been sorted.

In this code, the swap function implements the exchange of two elements, the bubble function implements a bubble sort, and the bubbleSort function performs bubble sort on the entire array. Among them, the bubble function returns a Boolean value, indicating whether the exchange operation has been performed. If no exchange operation has been performed, it means that the array has been sorted, and the loop can be exited directly.

In the main function, the original array is first output, then the bubbleSort function is called to sort the array, and finally the sorted array is output. As you can see, the sorted arrays are arranged in ascending order.

It is worth noting that the template class is used in this code to sort arrays of different types. At the same time, the foreach function in STL is used to easily traverse and output the array.

#include <iostream>
#include <algorithm>

template<class T>
void swap (T& a, T& b)
{
    
    
    T temp = a;
    a = b;
    b = temp;
}

template<class T>
bool bubble (T a[], int n)
{
    
    
    bool swapped = false;
    for (int i = 0; i < n - 1; ++i)
    {
    
    
        if (a[i] > a[i+1])//如果数组已经是排序的话,则排序就终止了
        {
    
    
            swap(a[i], a[i+1]);
            swapped = true;
        }
    }
    return swapped;
}

template<class T>
void bubbleSort(T a[], int n)
{
    
    
    for (int i = n; i > 1 && bubble(a, i); --i); //最后只剩一个数字就是排好序的,故到a>1即可
}
void Show(const int& v)
{
    
    
    std::cout << v << " ";
}

int main()
{
    
    
    int arr[]{
    
     6, 5, 8, 4, 3, 1 };
    for (auto x : arr)
        std::cout << x << " ";
    //std::for_each(arr, arr + sizeof(arr) / sizeof(int), Show);
    std::cout << "\n";
    bubbleSort(arr, sizeof(arr) / sizeof(int));
    std::for_each(arr, arr + sizeof(arr) / sizeof(int), Show);
    std::cout << std::endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45541762/article/details/130694806