-
Notifications
You must be signed in to change notification settings - Fork 12
Create Mergesort.cpp #11
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,91 @@ | ||
| #include <bits/stdc++.h> | ||
|
Check notice on line 1 in Solved-Problems/Mergesort.cpp
|
||
| using namespace std; | ||
|
|
||
| // Merges two subarrays of arr[]. | ||
| // First subarray is arr[left..mid] | ||
| // Second subarray is arr[mid+1..right] | ||
| void merge(vector<int>& arr, int left, | ||
| int mid, int right) | ||
| { | ||
| int n1 = mid - left + 1; | ||
| int n2 = right - mid; | ||
|
|
||
| // Create temp vectors | ||
| vector<int> L(n1), R(n2); | ||
|
|
||
| // Copy data to temp vectors L[] and R[] | ||
| for (int i = 0; i < n1; i++) | ||
| L[i] = arr[left + i]; | ||
| for (int j = 0; j < n2; j++) | ||
| R[j] = arr[mid + 1 + j]; | ||
|
|
||
| int i = 0, j = 0; | ||
| int k = left; | ||
|
|
||
| // Merge the temp vectors back | ||
| // into arr[left..right] | ||
| while (i < n1 && j < n2) { | ||
| if (L[i] <= R[j]) { | ||
| arr[k] = L[i]; | ||
| i++; | ||
| } | ||
| else { | ||
| arr[k] = R[j]; | ||
| j++; | ||
| } | ||
| k++; | ||
| } | ||
|
|
||
| // Copy the remaining elements of L[], | ||
| // if there are any | ||
| while (i < n1) { | ||
| arr[k] = L[i]; | ||
| i++; | ||
| k++; | ||
| } | ||
|
|
||
| // Copy the remaining elements of R[], | ||
| // if there are any | ||
| while (j < n2) { | ||
| arr[k] = R[j]; | ||
| j++; | ||
| k++; | ||
| } | ||
| } | ||
|
|
||
| // begin is for left index and end is right index | ||
| // of the sub-array of arr to be sorted | ||
| void mergeSort(vector<int>& arr, int left, int right) | ||
| { | ||
| if (left >= right) | ||
| return; | ||
|
|
||
| int mid = left + (right - left) / 2; | ||
| mergeSort(arr, left, mid); | ||
| mergeSort(arr, mid + 1, right); | ||
| merge(arr, left, mid, right); | ||
| } | ||
|
|
||
| // Function to print a vector | ||
| void printVector(vector<int>& arr) | ||
| { | ||
| for (int i = 0; i < arr.size(); i++) | ||
| cout << arr[i] << " "; | ||
| cout << endl; | ||
| } | ||
|
|
||
| // Driver code | ||
| int main() | ||
| { | ||
| vector<int> arr = { 12, 11, 13, 5, 6, 7 }; | ||
| int n = arr.size(); | ||
|
|
||
| cout << "Given vector is \n"; | ||
| printVector(arr); | ||
|
|
||
| mergeSort(arr, 0, n - 1); | ||
|
|
||
| cout << "\nSorted vector is \n"; | ||
| printVector(arr); | ||
| return 0; | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.