排序算法——排序算法大全

包含所有排序算法-使用Github Copilot生成。

#include<bits/stdc++.h>
using namespace std;

void InsertionSort(int arr[], int n)
{
    int i, j;
    int get;
    for (i = 1; i < n; i++)
    {
        get = arr[i];
        for (j = i - 1; j >= 0; j--)
        {
            if (arr[j] > get)
                arr[j + 1] = arr[j];
            else
                break;
        }
        arr[j + 1] = get;
    }
}

void BubbleSort(int arr[], int n)
{
    int i, j;
    for (i = 0; i < n-1; i++)

        // Last i elements are already in place
        for (j = 0; j < n-i-1; j++)
            if (arr[j] > arr[j+1])
                swap(arr[j], arr[j+1]);
}

void SelectionSort(int arr[], int n)
{
    int i, j;
    int min;
    for (i = 0; i < n - 1; i++)
    {
        min = i;
        for (j = i + 1; j < n; j++)
        {
            if (arr[j] < arr[min])
                min = j;
        }
        if (min != i)
            swap(arr[i], arr[min]);
    }
}

void QuickSort(int arr[], int left, int right)
{
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];

    /* partition */
    while (i <= j) {
        while (arr[i] < pivot)
            i++;
        while (arr[j] > pivot)
            j--;
        if (i <= j) {
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    };

    /* recursion */
    if (left < j)
        QuickSort(arr, left, j);
    if (i < right)
        QuickSort(arr, i, right);
}

void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l + (r - l) / 2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);

        merge(arr, l, m, r);
    }
}

void BucketSort(int arr[], int n)
{
    int i, j;
    int count[n];
    for (i = 0; i < n; i++)
        count[i] = 0;

    for (i = 0; i < n; i++)
        (count[arr[i]])++;

    for (i = 0, j = 0; i < n; i++)
        for (; count[i] > 0; (count[i])--)
            arr[j++] = i;
}

void CountSort(int arr[], int n)
{
    int i, j;
    int max = arr[0];
    int min = arr[0];
    for (i = 1; i < n; i++)
    {
        if (arr[i] > max)
            max = arr[i];
        if (arr[i] < min)
            min = arr[i];
    }
    int range = max - min + 1;
    int count[range];
    int output[n];
    for (i = 0; i < range; i++)
        count[i] = 0;

    for (i = 0; i < n; i++)
        count[arr[i] - min]++;

    for (i = 1; i < range; i++)
        count[i] += count[i - 1];

    for (i = n - 1; i >= 0; i--)
    {
        output[count[arr[i] - min] - 1] = arr[i];
        count[arr[i] - min]--;
    }

    for (i = 0; i < n; i++)
        arr[i] = output[i];
}

void RadixSort(int arr[], int n)
{
    int i, j;
    int max = arr[0];
    for (i = 1; i < n; i++)
    {
        if (arr[i] > max)
            max = arr[i];
    }
    for (int exp = 1; max / exp > 0; exp *= 10)
    {
        int output[n];
        int count[10] = { 0 };

        for (i = 0; i < n; i++)
            count[(arr[i] / exp) % 10]++;

        for (i = 1; i < 10; i++)
            count[i] += count[i - 1];

        for (i = n - 1; i >= 0; i--)
        {
            output[count[(arr[i] / exp) % 10] - 1] = arr[i];
            count[(arr[i] / exp) % 10]--;
        }

        for (i = 0; i < n; i++)
            arr[i] = output[i];
    }
}

void Heapify(int arr[], int n, int i)
{
    int largest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;

    if (l < n && arr[l] > arr[largest])
        largest = l;

    if (r < n && arr[r] > arr[largest])
        largest = r;

    if (largest != i)
    {
        swap(arr[i], arr[largest]);
        Heapify(arr, n, largest);
    }
}

void Heap_sort(int arr[], int n)
{
    for (int i = n / 2 - 1; i >= 0; i--)
        Heapify(arr, n, i);

    for (int i = n - 1; i >= 0; i--)
    {
        swap(arr[0], arr[i]);
        Heapify(arr, i, 0);
    }
}

void ShellSort(int arr[], int n)
{
    for (int gap = n / 2; gap > 0; gap /= 2)
    {
        for (int i = gap; i < n; i += 1)
        {
            int temp = arr[i];
            int j;
            for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
                arr[j] = arr[j - gap];
            arr[j] = temp;
        }
    }
}

int main(){
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements: ";
    for (int i = 0; i < n; i++)
        cin >> arr[i];
    int choice;
    cout << "Enter the choice of sorting algorithm: " << endl;
    cout << "1. Bubble Sort" << endl;
    cout << "2. Selection Sort" << endl;
    cout << "3. Insertion Sort" << endl;
    cout << "4. Quick Sort" << endl;
    cout << "5. Merge Sort" << endl;
    cout << "6. Bucket Sort" << endl;
    cout << "7. Count Sort" << endl;
    cout << "8. Radix Sort" << endl;
    cout << "9. Heap Sort" << endl;
    cout << "10. Shell Sort" << endl;
    cin >> choice;
    switch (choice)
    {
    case 1:
        BubbleSort(arr, n);
        break;
    case 2:
        SelectionSort(arr, n);
        break;
    case 3:
        InsertionSort(arr, n);
        break;
    case 4:
        QuickSort(arr, 0, n - 1);
        break;
    case 5:
        mergeSort(arr, 0, n - 1);
        break;
    case 6:
        BucketSort(arr, n);
        break;
    case 7:
        CountSort(arr, n);
        break;
    case 8:
        RadixSort(arr, n);
        break;
    case 9:
        Heap_sort(arr, n);
        break;
    case 10:
        ShellSort(arr, n);
        break;
    default:
        cout << "Invalid choice" << endl;
    }
    cout << "Sorted array: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
    return 0;
}

类似文章

12条评论

    1. Sorry I didn't see your commit.
      This blog is powered by WordPress,which is a framework for making websites.
      This theme is named Kratos.The photo on the top is a character called HuTao in a game called Genshin Impact.

  1. Wonderful beat ! I would like to apprentice while you amend your web site, how could i subscribe for a weblog
    web site? The account aided me a appropriate deal.
    I have been a little bit familiar of this your broadcast offered vivid transparent idea

  2. Good day! This is my 1st comment here so I just wanted to
    give a quick shout out and say I truly enjoy reading your blog posts.
    Can you recommend any other blogs/websites/forums that cover the same subjects?

    Thank you so much!

  3. Its like you read my mind! You appear to know so much about this,
    like you wrote the book in it or something. I think that you
    could do with some pics to drive the message home a bit, but
    instead of that, this is magnificent blog. An excellent read.
    I'll certainly be back.

  4. I'd like to thank you for the efforts you have put in penning this site.
    I'm hoping to view the same high-grade content by you
    in the future as well. In fact, your creative writing
    abilities has inspired me to get my own blog now 😉

回复 buy instagram followers uk 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注