1+ /* *
2+ * Merge Sort Algorithm
3+ *
4+ * A divide-and-conquer algorithm that divides the input array into two halves,
5+ * recursively sorts them, and then merges the sorted halves.
6+ *
7+ * Time Complexity: O(n log n) in all cases
8+ * Space Complexity: O(n) for temporary array
9+ *
10+ * Author: Karanjot Singh
11+ * Date: September 2025
12+
13+ */
14+
15+ #include < iostream>
16+ #include < vector>
17+ using namespace std ;
18+
19+ /* *
20+ * Merges two sorted subarrays into one sorted array
21+ *
22+ * @param arr The main array containing both subarrays
23+ * @param left Starting index of first subarray
24+ * @param mid Ending index of first subarray
25+ * @param right Ending index of second subarray
26+ */
27+ void merge (vector<int >& arr, int left, int mid, int right) {
28+ // Calculate sizes of two subarrays
29+ int n1 = mid - left + 1 ;
30+ int n2 = right - mid;
31+
32+ // Create temporary arrays
33+ vector<int > leftArr (n1);
34+ vector<int > rightArr (n2);
35+
36+ // Copy data to temporary arrays
37+ for (int i = 0 ; i < n1; i++)
38+ leftArr[i] = arr[left + i];
39+ for (int j = 0 ; j < n2; j++)
40+ rightArr[j] = arr[mid + 1 + j];
41+
42+ // Merge the temporary arrays back into arr[left..right]
43+ int i = 0 , j = 0 , k = left;
44+
45+ while (i < n1 && j < n2) {
46+ if (leftArr[i] <= rightArr[j]) {
47+ arr[k] = leftArr[i];
48+ i++;
49+ } else {
50+ arr[k] = rightArr[j];
51+ j++;
52+ }
53+ k++;
54+ }
55+
56+ // Copy remaining elements of leftArr[], if any
57+ while (i < n1) {
58+ arr[k] = leftArr[i];
59+ i++;
60+ k++;
61+ }
62+
63+ // Copy remaining elements of rightArr[], if any
64+ while (j < n2) {
65+ arr[k] = rightArr[j];
66+ j++;
67+ k++;
68+ }
69+ }
70+
71+ /* *
72+ * Main merge sort function that recursively divides and sorts the array
73+ *
74+ * @param arr The array to be sorted
75+ * @param left Starting index
76+ * @param right Ending index
77+ */
78+ void mergeSort (vector<int >& arr, int left, int right) {
79+ if (left < right) {
80+ // Find the middle point
81+ int mid = left + (right - left) / 2 ;
82+
83+ // Sort first and second halves
84+ mergeSort (arr, left, mid);
85+ mergeSort (arr, mid + 1 , right);
86+
87+ // Merge the sorted halves
88+ merge (arr, left, mid, right);
89+ }
90+ }
91+
92+ /* *
93+ * Utility function to print an array
94+ */
95+ void printArray (const vector<int >& arr) {
96+ for (int num : arr)
97+ cout << num << " " ;
98+ cout << endl;
99+ }
100+
101+ /* *
102+ * Main function demonstrating merge sort usage
103+ */
104+ int main () {
105+ vector<int > arr = {64 , 34 , 25 , 12 , 22 , 11 , 90 };
106+
107+ cout << " Original array: " ;
108+ printArray (arr);
109+
110+ mergeSort (arr, 0 , arr.size () - 1 );
111+
112+ cout << " Sorted array: " ;
113+ printArray (arr);
114+
115+ return 0 ;
116+ }
117+
118+ /*
119+ Example Output:
120+ Original array: 64 34 25 12 22 11 90
121+ Sorted array: 11 12 22 25 34 64 90
122+
123+ Test Cases:
124+ 1. Empty array: [] -> []
125+ 2. Single element: [5] -> [5]
126+ 3. Already sorted: [1,2,3,4,5] -> [1,2,3,4,5]
127+ 4. Reverse sorted: [5,4,3,2,1] -> [1,2,3,4,5]
128+ 5. Duplicates: [3,1,3,2,1] -> [1,1,2,3,3]
129+ */
0 commit comments