-
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
Conversation
Added MergeSort Algorithm
|
👋 @pranaydeep1 |
|
Hi @pranaydeep1, It's seems like you have some formatting issue with your source code. Please format you code properly so that we can proceed to merging your PR. If you need some help with formatting, you can checkout the results of the linting workflow under the show diff and exit section. |
Modified Mergesort by adding specific folder
| #include <bits/stdc++.h> | ||
| 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; | ||
| } |
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.
Hi @pranaydeep1, It's great to see you following the guidelines but it seems like you still haven't formatted your source code. Here are the required changes you need to make to format your source code,
View the changes
Don't worry it's still your code but formatted.
| #include <bits/stdc++.h> | |
| 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; | |
| } | |
| #include <bits/stdc++.h> | |
| 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; | |
| } |
|
This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
|
This PR was closed because it has been stalled for 10 days with no activity. |
Added MergeSort Algorithm