-
Notifications
You must be signed in to change notification settings - Fork 275
Add: Implement Merge Sort algorithm in C++ #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+129
β0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,129 @@ | ||||||||||
| /** | ||||||||||
| * Merge Sort Algorithm | ||||||||||
| * | ||||||||||
| * A divide-and-conquer algorithm that divides the input array into two halves, | ||||||||||
| * recursively sorts them, and then merges the sorted halves. | ||||||||||
| * | ||||||||||
| * Time Complexity: O(n log n) in all cases | ||||||||||
| * Space Complexity: O(n) for temporary array | ||||||||||
| * | ||||||||||
| * Author: Karanjot Singh | ||||||||||
| * Date: September 2025 | ||||||||||
|
|
||||||||||
| */ | ||||||||||
|
|
||||||||||
| #include <iostream> | ||||||||||
| #include <vector> | ||||||||||
| using namespace std; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Merges two sorted subarrays into one sorted array | ||||||||||
| * | ||||||||||
| * @param arr The main array containing both subarrays | ||||||||||
| * @param left Starting index of first subarray | ||||||||||
| * @param mid Ending index of first subarray | ||||||||||
| * @param right Ending index of second subarray | ||||||||||
| */ | ||||||||||
| void merge(vector<int>& arr, int left, int mid, int right) { | ||||||||||
| // Calculate sizes of two subarrays | ||||||||||
| int n1 = mid - left + 1; | ||||||||||
| int n2 = right - mid; | ||||||||||
|
|
||||||||||
| // Create temporary arrays | ||||||||||
| vector<int> leftArr(n1); | ||||||||||
| vector<int> rightArr(n2); | ||||||||||
|
|
||||||||||
| // Copy data to temporary arrays | ||||||||||
| for (int i = 0; i < n1; i++) | ||||||||||
| leftArr[i] = arr[left + i]; | ||||||||||
| for (int j = 0; j < n2; j++) | ||||||||||
| rightArr[j] = arr[mid + 1 + j]; | ||||||||||
|
|
||||||||||
| // Merge the temporary arrays back into arr[left..right] | ||||||||||
| int i = 0, j = 0, k = left; | ||||||||||
|
|
||||||||||
| while (i < n1 && j < n2) { | ||||||||||
| if (leftArr[i] <= rightArr[j]) { | ||||||||||
| arr[k] = leftArr[i]; | ||||||||||
| i++; | ||||||||||
| } else { | ||||||||||
| arr[k] = rightArr[j]; | ||||||||||
| j++; | ||||||||||
| } | ||||||||||
| k++; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Copy remaining elements of leftArr[], if any | ||||||||||
| while (i < n1) { | ||||||||||
| arr[k] = leftArr[i]; | ||||||||||
| i++; | ||||||||||
| k++; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Copy remaining elements of rightArr[], if any | ||||||||||
| while (j < n2) { | ||||||||||
| arr[k] = rightArr[j]; | ||||||||||
| j++; | ||||||||||
| k++; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Main merge sort function that recursively divides and sorts the array | ||||||||||
| * | ||||||||||
| * @param arr The array to be sorted | ||||||||||
| * @param left Starting index | ||||||||||
| * @param right Ending index | ||||||||||
| */ | ||||||||||
| void mergeSort(vector<int>& arr, int left, int right) { | ||||||||||
| if (left < right) { | ||||||||||
| // Find the middle point | ||||||||||
| int mid = left + (right - left) / 2; | ||||||||||
|
|
||||||||||
| // Sort first and second halves | ||||||||||
| mergeSort(arr, left, mid); | ||||||||||
| mergeSort(arr, mid + 1, right); | ||||||||||
|
|
||||||||||
| // Merge the sorted halves | ||||||||||
| merge(arr, left, mid, right); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Utility function to print an array | ||||||||||
| */ | ||||||||||
| void printArray(const vector<int>& arr) { | ||||||||||
| for (int num : arr) | ||||||||||
| cout << num << " "; | ||||||||||
| cout << endl; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Main function demonstrating merge sort usage | ||||||||||
| */ | ||||||||||
| int main() { | ||||||||||
| vector<int> arr = {64, 34, 25, 12, 22, 11, 90}; | ||||||||||
|
|
||||||||||
| cout << "Original array: "; | ||||||||||
| printArray(arr); | ||||||||||
|
|
||||||||||
| mergeSort(arr, 0, arr.size() - 1); | ||||||||||
|
||||||||||
| mergeSort(arr, 0, arr.size() - 1); | |
| if (!arr.empty()) { | |
| mergeSort(arr, 0, arr.size() - 1); | |
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Using 'using namespace std;' in header files or example code is considered bad practice as it pollutes the global namespace. Consider using specific using declarations like 'using std::vector;' or fully qualified names.