Skip to content

Conversation

@pranaydeep1
Copy link

Added MergeSort Algorithm

Added MergeSort Algorithm
@github-actions github-actions bot requested a review from iamwatchdogs October 31, 2024 13:41
@github-actions github-actions bot added the hacktoberfest Opted for hacktoberfest label Oct 31, 2024
@github-actions
Copy link

👋 @pranaydeep1
Thank you for raising your pull request.
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.

@iamwatchdogs
Copy link
Contributor

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
Comment on lines +1 to +91
#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;
}
Copy link
Contributor

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.

Suggested change
#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;
}

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Dec 17, 2024
@github-actions
Copy link

This PR was closed because it has been stalled for 10 days with no activity.

@github-actions github-actions bot closed this Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest Opted for hacktoberfest Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants