sorting algorithm

Üdvözlöm, Ön a sorting algorithm szó jelentését keresi. A DICTIOUS-ban nem csak a sorting algorithm szó összes szótári jelentését megtalálod, hanem megismerheted az etimológiáját, a jellemzőit és azt is, hogyan kell a sorting algorithm szót egyes és többes számban mondani. Minden, amit a sorting algorithm szóról tudni kell, itt található. A sorting algorithm szó meghatározása segít abban, hogy pontosabban és helyesebben fogalmazz, amikor beszélsz vagy írsz. Asorting algorithm és más szavak definíciójának ismerete gazdagítja a szókincsedet, és több és jobb nyelvi forráshoz juttat.

Főnév

sorting algorithm (tsz. sorting algorithms)

  1. (informatika) rendezési algoritmus

Here’s a C++ program implementing six different sorting algorithms:

  1. Bubble Sort
  2. Selection Sort
  3. Insertion Sort
  4. Merge Sort
  5. Quick Sort
  6. Heap Sort

This program lets the user choose a sorting algorithm and sorts an array accordingly.



C++ Implementation of Six Sorting Algorithms

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Bubble Sort
void bubbleSort(vector<int>& arr) {
    int n = arr.size();
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr > arr) {
                swap(arr, arr);
            }
        }
    }
}

// Selection Sort
void selectionSort(vector<int>& arr) {
    int n = arr.size();
    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr < arr) {
                minIndex = j;
            }
        }
        swap(arr, arr);
    }
}

// Insertion Sort
void insertionSort(vector<int>& arr) {
    int n = arr.size();
    for (int i = 1; i < n; i++) {
        int key = arr;
        int j = i - 1;
        while (j >= 0 && arr > key) {
            arr = arr;
            j--;
        }
        arr = key;
    }
}

// Merge Sort (Helper function)
void merge(vector<int>& arr, int left, int mid, int right) {
    int n1 = mid - left + 1, n2 = right - mid;
    vector<int> L(n1), R(n2);
    for (int i = 0; i < n1; i++) L = arr;
    for (int i = 0; i < n2; i++) R = arr;

    int i = 0, j = 0, k = left;
    while (i < n1 && j < n2) arr = (L <= R) ? L : R;
    while (i < n1) arr = L;
    while (j < n2) arr = R;
}

// Merge Sort
void mergeSort(vector<int>& arr, int left, int right) {
    if (left < right) {
        int mid = left + (right - left) / 2;
        mergeSort(arr, left, mid);
        mergeSort(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }
}

// Quick Sort (Helper function)
int partition(vector<int>& arr, int low, int high) {
    int pivot = arr, i = low - 1;
    for (int j = low; j < high; j++) {
        if (arr < pivot) swap(arr, arr);
    }
    swap(arr, arr);
    return i + 1;
}

// Quick Sort
void quickSort(vector<int>& arr, int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

// Heap Sort (Helper function)
void heapify(vector<int>& arr, int n, int i) {
    int largest = i, left = 2 * i + 1, right = 2 * i + 2;
    if (left < n && arr > arr) largest = left;
    if (right < n && arr > arr) largest = right;
    if (largest != i) {
        swap(arr, arr);
        heapify(arr, n, largest);
    }
}

// Heap Sort
void heapSort(vector<int>& arr) {
    int n = arr.size();
    for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i);
    for (int i = n - 1; i > 0; i--) {
        swap(arr, arr);
        heapify(arr, i, 0);
    }
}

// Function to print the array
void printArray(const vector<int>& arr) {
    for (int num : arr) cout << num << " ";
    cout << endl;
}

int main() {
    vector<int> arr = {64, 25, 12, 22, 11, 90, 34, 56};
    int choice;

    cout << "Choose a sorting algorithm:\n";
    cout << "1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n";
    cout << "4. Merge Sort\n5. Quick Sort\n6. Heap Sort\n";
    cout << "Enter your choice: ";
    cin >> choice;

    switch (choice) {
        case 1: bubbleSort(arr); break;
        case 2: selectionSort(arr); break;
        case 3: insertionSort(arr); break;
        case 4: mergeSort(arr, 0, arr.size() - 1); break;
        case 5: quickSort(arr, 0, arr.size() - 1); break;
        case 6: heapSort(arr); break;
        default: cout << "Invalid choice!\n"; return 1;
    }

    cout << "Sorted array: ";
    printArray(arr);
    return 0;
}

How It Works

  1. The user chooses a sorting algorithm (1-6).
  2. The program sorts a predefined array using the selected algorithm.
  3. The sorted array is printed.



Time Complexity of Sorting Algorithms

Algorithm Best Case Average Case Worst Case
Bubble Sort O(n) O(n²) O(n²)
Selection Sort O(n²) O(n²) O(n²)
Insertion Sort O(n) O(n²) O(n²)
Merge Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(n log n) O(n log n) O(n²) (worst case)
Heap Sort O(n log n) O(n log n) O(n log n)



Try It Out!

  • Compile and run the program.
  • Enter a number (1-6) to select a sorting algorithm.
  • The program sorts and prints the sorted array.