Common algorithm techniques—sorting algorithms

a brief introdction

Sorting algorithms are a common algorithmic technique used to arrange a set of elements in a specific order. There are many different ways to implement sorting algorithms, each with its own pros and cons and applicable scenarios.

Application examples

Below I will introduce several common sorting algorithms and provide a sample program implemented in C++.

Bubble Sort

Bubble sort is a simple sorting algorithm that repeatedly swaps adjacent elements, gradually moving larger elements to the right to the correct position. The specific implementation is as follows:

#include <iostream>
#include <vector>

void bubbleSort(std::vector<int>& nums) {
    
    
    int n = nums.size();

    for (int i = 0; i < n - 1; i++) {
    
    
        for (int j = 0; j < n - i - 1; j++) {
    
    
            if (nums[j] > nums[j + 1]) {
    
    
                std::swap(nums[j], nums[j + 1]);
            }
        }
    }
}

int main() {
    
    
    std::vector<int> nums = {
    
    5, 2, 9, 1, 3, 6};

    bubbleSort(nums);

    std::cout << "Sorted array: ";
    for (int num : nums) {
    
    
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Insertion Sort

Insertion sort inserts unsorted elements into the sorted part one by one, and achieves sorting by continuously comparing and exchanging elements with elements in the sorted part. The specific implementation is as follows:

#include <iostream>
#include <vector>

void insertionSort(std::vector<int>& nums) {
    
    
    int n = nums.size();

    for (int i = 1; i < n; i++) {
    
    
        int key = nums[i];
        int j = i - 1;

        while (j >= 0 && nums[j] > key) {
    
    
            nums[j + 1] = nums[j];
            j--;
        }

        nums[j + 1] = key;
    }
}

int main() {
    
    
    std::vector<int> nums = {
    
    5, 2, 9, 1, 3, 6};

    insertionSort(nums);

    std::cout << "Sorted array: ";
    for (int num : nums) {
    
    
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Quick Sort

Quick sort is an efficient sorting algorithm. It divides the array into two sub-arrays by selecting a reference element, so that all elements of the left sub-array are smaller than the reference element and all elements of the right sub-array are greater than the reference element. Then recursively sort the two subarrays separately. The specific implementation is as follows:

#include <iostream>
#include <vector>

int partition(std::vector<int>& nums, int low, int high) {
    
    
    int pivot = nums[high];
    int i = low - 1;

    for (int j = low; j <= high - 1; j++) {
    
    
        if (nums[j] < pivot) {
    
    
            i++;
            std::swap(nums[i], nums[j]);
        }
    }

    std::swap(nums[i + 1], nums[high]);

    return i + 1;
}

void quickSort(std::vector<int>& nums, int low, int high) {
    
    
    if (low < high) {
    
    
        int pivotIndex = partition(nums, low, high);
        quickSort(nums, low, pivotIndex - 1);
        quickSort(nums, pivotIndex + 1, high);
    }
}

int main() {
    
    
    std::vector<int> nums = {
    
    5, 2, 9, 1, 3, 6};

    quickSort(nums, 0, nums.size() - 1);

    std::cout << "Sorted array: ";
    for (int num : nums) {
    
    
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Article summary

The above example programs demonstrate the implementation of bubble sort, insertion sort and quick sort respectively. These sorting algorithms have different performance and applicability in different scenarios. Choosing an appropriate sorting algorithm depends on the size of the problem, the characteristics of the data, and performance requirements.

Guess you like

Origin blog.csdn.net/qq_45902301/article/details/131649282