Sorting algorithm
Angol
Főnév
sorting algorithm (tsz. sorting algorithms)Kategória:angol lemmák#SORTING%20ALGORITHMKategória:angol főnevek#SORTING%20ALGORITHMKategória:angol megszámlálható főnevek#SORTING%20ALGORITHMKategória:angol kifejezések#SORTING%20ALGORITHM
Here’s a C++ program implementing six different sorting algorithms:
- Bubble Sort
- Selection Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- 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[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
// 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[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
// Insertion Sort
void insertionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = 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[i] = arr[left + i];
for (int i = 0; i < n2; i++) R[i] = arr[mid + 1 + i];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
// 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[high], i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) swap(arr[++i], arr[j]);
}
swap(arr[i + 1], arr[high]);
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[left] > arr[largest]) largest = left;
if (right < n && arr[right] > arr[largest]) largest = right;
if (largest != i) {
swap(arr[i], arr[largest]);
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[0], arr[i]);
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
- The user chooses a sorting algorithm (1-6).
- The program sorts a predefined array using the selected algorithm.
- 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.
További információk
- sorting algorithm - Szótár.net (en-hu)
- sorting algorithm - Sztaki (en-hu)
- sorting algorithm - Merriam–Webster
- sorting algorithm - Cambridge
- sorting algorithm - WordNet
- sorting algorithm - Яндекс (en-ru)
- sorting algorithm - Google (en-hu)
- sorting algorithm - Wikidata
- sorting algorithm - Wikipédia (angol)Kategória:en