|
| 1 | +// Fast and efficient method for large lists |
| 2 | +// Good for when we don't know the state of the list |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +#define SIZE 10 |
| 8 | + |
| 9 | +// Function prototypes |
| 10 | +void printArray(int array[]); |
| 11 | +void quickSort(int array[SIZE], int start, int end); |
| 12 | + |
| 13 | +/** |
| 14 | + * @brief Main function that demonstrates the sorting algorithm. |
| 15 | + * |
| 16 | + * Initializes an array of integers, prints it, sorts it using the quicksort |
| 17 | + * algorithm, and prints the sorted array. |
| 18 | + * |
| 19 | + * @return int Status code (0 for success) |
| 20 | + */ |
| 21 | + |
| 22 | +int main() |
| 23 | +{ |
| 24 | + // Initialize an array of integers in descending order |
| 25 | + int array[SIZE]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; |
| 26 | + |
| 27 | + // Print the original array |
| 28 | + printArray(array); |
| 29 | + std::cout << std::endl; |
| 30 | + std::cout << std::endl; |
| 31 | + |
| 32 | + // Sort the array using quicksort |
| 33 | + quickSort(array, 0, SIZE - 1); |
| 34 | + |
| 35 | + // Print the sorted array |
| 36 | + printArray(array); |
| 37 | + |
| 38 | + std::cout << std::endl; |
| 39 | + system("PAUSE"); |
| 40 | + return 0; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Prints the elements of an integer array. |
| 45 | + * |
| 46 | + * This function prints each element of the array separated by a pipe ('|') for easy readability. |
| 47 | + * |
| 48 | + * @param array The integer array to be printed |
| 49 | + */ |
| 50 | + |
| 51 | +void printArray(int array[]) |
| 52 | +{ |
| 53 | + for (int i = 0; i < SIZE; ++i) |
| 54 | + { |
| 55 | + std::cout << " |" << array[i] << "| "; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief Sorts an array using the QuickSort algorithm. |
| 61 | + * |
| 62 | + * The QuickSort algorithm is a divide-and-conquer algorithm. It works by selecting a pivot element |
| 63 | + * from the array and partitioning the other elements into two sub-arrays, according to whether they |
| 64 | + * are smaller or greater than the pivot. The sub-arrays are then recursively sorted. |
| 65 | + * |
| 66 | + * @param array The integer array to be sorted |
| 67 | + * @param start The starting index of the section of the array to be sorted |
| 68 | + * @param end The ending index of the section of the array to be sorted |
| 69 | + */ |
| 70 | + |
| 71 | +void quickSort(int array[SIZE], int start, int end) |
| 72 | +{ |
| 73 | + int pivot, left, right, middle, temp; |
| 74 | + |
| 75 | + // Left and right boundaries of the section being analyzed |
| 76 | + left = start; |
| 77 | + right = end; |
| 78 | + |
| 79 | + // Find the middle index and set it as the pivot |
| 80 | + middle = (left + right) / 2; |
| 81 | + pivot = array[middle]; |
| 82 | + |
| 83 | + // Partition the array around the pivot |
| 84 | + while (right > left) |
| 85 | + { |
| 86 | + // Move the left boundary to the right until an element larger than the pivot is found |
| 87 | + while (array[left] < pivot) |
| 88 | + { |
| 89 | + left++; |
| 90 | + } |
| 91 | + |
| 92 | + // Move the right boundary to the left until an element smaller than the pivot is found |
| 93 | + while (array[right] > pivot) |
| 94 | + { |
| 95 | + right--; |
| 96 | + } |
| 97 | + |
| 98 | + // If the left boundary is less than or equal to the right boundary, swap the elements |
| 99 | + if (left <= right) |
| 100 | + { |
| 101 | + temp = array[left]; |
| 102 | + array[left] = array[right]; |
| 103 | + array[right] = temp; |
| 104 | + |
| 105 | + // Move the boundaries towards the center |
| 106 | + left++; |
| 107 | + right--; |
| 108 | + } |
| 109 | + |
| 110 | + // Print the array after each partition to show the sorting progress |
| 111 | + printArray(array); |
| 112 | + std::cout << std::endl; |
| 113 | + } |
| 114 | + |
| 115 | + // Recursively sort the left partition if necessary |
| 116 | + if (start < right) |
| 117 | + { |
| 118 | + quickSort(array, start, right); |
| 119 | + } |
| 120 | + |
| 121 | + // Recursively sort the right partition if necessary |
| 122 | + if (left < end) |
| 123 | + { |
| 124 | + quickSort(array, left, end); |
| 125 | + } |
| 126 | +} |
0 commit comments