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

How to use bubble sort algorithm in C++

WBOY
Release: 2023-09-19 17:12:11
Original
1509 people have browsed it

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;
}
Copy after login
  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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template