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