Home  >  Article  >  Backend Development  >  How to use bubble sort algorithm in C++

How to use bubble sort algorithm in C++

WBOY
WBOYOriginal
2023-09-19 17:12:111474browse

How to use bubble sort algorithm in C++

How to use the bubble sort algorithm in C

The bubble sort algorithm is a simple but inefficient sorting algorithm that uses multiple comparisons and exchanges to Arrange a sequence from smallest to largest (or largest to smallest). Here we will introduce how to implement the bubble sort algorithm using C language, and attach detailed code examples.

  1. Algorithm principle:
    The basic idea of ​​the bubble sort algorithm is to compare adjacent elements one by one from the sequence to be sorted. If the previous element is greater than the latter element, the two are exchanged. The position of the element. After such a comparison, the largest (or smallest) element will "bubble" to the end of the sequence. Then perform the same comparison and exchange operations on the remaining sequences until the entire sequence is in order.
  2. Algorithm implementation:
    The following is a code example using C language to implement the bubble sort algorithm:
#include<iostream>
using namespace std;

// 冒泡排序函数
void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            // 如果前一个元素大于后一个元素,交换它们的位置
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

// 主函数
int main() {
    int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
    int n = sizeof(arr) / sizeof(arr[0]);

    bubbleSort(arr, n);

    cout << "排序后的数组:";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    
    return 0;
}
  1. Sample analysis:
    First, in the main function Define an integer array to be sorted arr and initialize it as needed. Then by calculating the length of the array n, call the bubbleSort function to sort the array. In the bubbleSort function, two nested loops are used to implement the core logic of bubble sort: the outer loop controls the number of rounds of comparison and exchange, and the inner loop is responsible for the specific comparison and exchange of each round. . Finally, the sorted array is output in the main function.
  2. Result demonstration:
    When the above code is run, the console will output the following results:
排序后的数组:11 12 22 25 34 64 90

It can be seen that after bubble sorting, the array elements are arranged in order from small to small. The big ones are arranged in the correct order.

Summary:
The bubble sort algorithm is a simple but inefficient sorting algorithm. In practical applications, the bubble sort algorithm can be used for small-scale data sorting. However, for large-scale data, the bubble sort algorithm has high time complexity and is not recommended.

The above is the detailed content of How to use bubble sort algorithm in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn