|
| 1 | +// Works well for large lists |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +#define SIZE 10 |
| 7 | + |
| 8 | +// Function prototypes |
| 9 | +void printArray(int array[]); |
| 10 | +void merge(int array[SIZE], int leftIndex, int middle, int rightIndex); |
| 11 | +void mergeSort(int array[SIZE], int leftIndex, int rightIndex); |
| 12 | + |
| 13 | +/** |
| 14 | + * @brief Main function demonstrating the Merge Sort algorithm. |
| 15 | + * |
| 16 | + * This function initializes an array, prints it, sorts it using the Merge Sort algorithm, |
| 17 | + * and then prints the sorted array. |
| 18 | + * |
| 19 | + * @return int Status code (0 for success) |
| 20 | + */ |
| 21 | + |
| 22 | +int main() |
| 23 | +{ |
| 24 | + // Initialize the array with values 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 Merge Sort |
| 33 | + mergeSort(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 with a pipe ('|') separator for better 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 Merges two subarrays of the given array. |
| 61 | + * |
| 62 | + * This function merges two sorted subarrays into a single sorted subarray. It is used during |
| 63 | + * the Merge Sort process to combine the divided sections of the array back together. |
| 64 | + * |
| 65 | + * @param array The integer array being sorted |
| 66 | + * @param leftIndex The starting index of the left subarray |
| 67 | + * @param middle The middle index that divides the array |
| 68 | + * @param rightIndex The ending index of the right subarray |
| 69 | + */ |
| 70 | + |
| 71 | +void merge(int array[SIZE], int leftIndex, int middle, int rightIndex) |
| 72 | +{ |
| 73 | + // Calculate the size of the two subarrays to be merged |
| 74 | + int n1 = middle - leftIndex + 1; |
| 75 | + int n2 = rightIndex - middle; |
| 76 | + |
| 77 | + // Create temporary arrays for the left and right subarrays |
| 78 | + int *leftArray = new int[n1]; |
| 79 | + int *rightArray = new int[n2]; |
| 80 | + |
| 81 | + // Copy data into temporary arrays |
| 82 | + for (int i = 0; i < n1; i++) |
| 83 | + { |
| 84 | + leftArray[i] = array[leftIndex + i]; |
| 85 | + } |
| 86 | + for (int j = 0; j < n2; j++) |
| 87 | + { |
| 88 | + rightArray[j] = array[middle + 1 + j]; |
| 89 | + } |
| 90 | + |
| 91 | + // Merge the two subarrays back into the original array |
| 92 | + int i = 0, j = 0, k = leftIndex; |
| 93 | + |
| 94 | + while (i < n1 && j < n2) |
| 95 | + { |
| 96 | + // If the element in the left array is smaller or equal, add it to the original array |
| 97 | + if (leftArray[i] <= rightArray[j]) |
| 98 | + { |
| 99 | + array[k] = leftArray[i]; |
| 100 | + i++; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + array[k] = rightArray[j]; |
| 105 | + j++; |
| 106 | + } |
| 107 | + k++; |
| 108 | + } |
| 109 | + |
| 110 | + // Copy any remaining elements from the left array, if any |
| 111 | + while (i < n1) |
| 112 | + { |
| 113 | + array[k] = leftArray[i]; |
| 114 | + i++; |
| 115 | + k++; |
| 116 | + } |
| 117 | + |
| 118 | + // Copy any remaining elements from the right array, if any |
| 119 | + while (j < n2) |
| 120 | + { |
| 121 | + array[k] = rightArray[j]; |
| 122 | + j++; |
| 123 | + k++; |
| 124 | + } |
| 125 | + |
| 126 | + // Free dynamically allocated memory |
| 127 | + delete[] leftArray; |
| 128 | + delete[] rightArray; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * @brief Performs Merge Sort on the given array. |
| 133 | + * |
| 134 | + * This function recursively divides the array into halves, sorts each half, and then merges |
| 135 | + * the sorted halves back together. Merge Sort is a divide-and-conquer sorting algorithm. |
| 136 | + * |
| 137 | + * @param array The integer array to be sorted |
| 138 | + * @param leftIndex The starting index of the array section to be sorted |
| 139 | + * @param rightIndex The ending index of the array section to be sorted |
| 140 | + */ |
| 141 | + |
| 142 | +void mergeSort(int array[SIZE], int leftIndex, int rightIndex) |
| 143 | +{ |
| 144 | + if (leftIndex < rightIndex) |
| 145 | + { |
| 146 | + // Find the middle index |
| 147 | + int middle = leftIndex + (rightIndex - leftIndex) / 2; |
| 148 | + |
| 149 | + // Recursively sort the left half |
| 150 | + mergeSort(array, leftIndex, middle); |
| 151 | + |
| 152 | + // Recursively sort the right half |
| 153 | + mergeSort(array, middle + 1, rightIndex); |
| 154 | + |
| 155 | + // Merge the two sorted halves |
| 156 | + merge(array, leftIndex, middle, rightIndex); |
| 157 | + |
| 158 | + // Optional: Print the array after each merge (for visualization) |
| 159 | + printArray(array); |
| 160 | + std::cout << std::endl; |
| 161 | + } |
| 162 | +} |
0 commit comments