Skip to content

Update MergeSort.cpp #54

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 67 additions & 96 deletions C++/MergeSort.cpp
Original file line number Diff line number Diff line change
@@ -1,98 +1,69 @@
#include <iostream>
#include <vector>

using std::cout;
using std::cin;
using std::vector;

// Merge Sort Algorithm

// Time complexity
// Best: O(n log n)
// Average: O(n log n)
// Worst: O(n log n)

// Space complexity: O(n)
template <class T>
void merge(vector<T>& list, int start, int mid, int end, int &nComparisons) { // merge two sorted sublists
vector<T> left; // left sublist
vector<T> right; // right sublist
int leftSize = mid - start + 1; // size of left sublist
int rightSize = end - mid; // size of right sublist

for (int i = 0; i < leftSize; i++) { // copy left sublist to left vector
left.push_back(list[start+i]); // copy element
}

for (int i = 0; i < rightSize; i++) { // copy right sublist to right vector
right.push_back(list[i+mid+1]); // copy element
}

int pos = start; // position in list
int leftPos = 0; // position in left sublist
int rightPos = 0; // position in right sublist

while (leftPos < leftSize && rightPos < rightSize) { // while both sublists are not empty
nComparisons++; // increment comparisons
if (left[leftPos] < right[rightPos]) { // if left element is smaller than right element
list[pos] = left[leftPos]; // copy left element to list
leftPos++; // increment left sublist position
} else { // if right element is smaller than left element
list[pos] = right[rightPos]; // copy right element to list
rightPos++; // increment right sublist position
}
pos++; // increment position in list
}

while (leftPos < leftSize) { // while left sublist is not empty
list[pos] = left[leftPos]; // copy left element to list
leftPos++; // increment left sublist position
pos++; // increment position in list
}

while (rightPos < rightSize) { // while right sublist is not empty
list[pos] = right[rightPos]; // copy right element to list
rightPos++; // increment right sublist position
pos++; // increment position in list
}
#include<iostream>
using namespace std;
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}

template <class T> // merge sort algorithm
void mergeSort(vector<T>& list, int start, int end, int &nComparisons) { // sort sublist

if(end > start) { // if sublist is not empty
int mid = (start + end) / 2; // find midpoint of sublist
mergeSort(list, start, mid, nComparisons); // sort left sublist
mergeSort(list, mid+1, end, nComparisons); // sort right sublist
merge(list, start, mid, end, nComparisons); // merge two sorted sublists
}
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void merge(int *array, int l, int m, int r) {
int i, j, k, nl, nr;
//size of left and right sub-arrays
nl = m-l+1; nr = r-m;
int larr[nl], rarr[nr];
//fill left and right sub-arrays
for(i = 0; i<nl; i++)
larr[i] = array[l+i];
for(j = 0; j<nr; j++)
rarr[j] = array[m+1+j];
i = 0; j = 0; k = l;
//marge temp arrays to real array
while(i < nl && j<nr) {
if(larr[i] <= rarr[j]) {
array[k] = larr[i];
i++;
}else{
array[k] = rarr[j];
j++;
}
k++;
}
while(i<nl) { //extra element in left array
array[k] = larr[i];
i++; k++;
}
while(j<nr) { //extra element in right array
array[k] = rarr[j];
j++; k++;
}
}
void mergeSort(int *array, int l, int r) {
int m;
if(l < r) {
int m = l+(r-l)/2;
// Sort first and second arrays
mergeSort(array, l, m);
mergeSort(array, m+1, r);
merge(array, l, m, r);
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
mergeSort(arr, 0, n-1); //(n-1) for last index
cout << "Array after Sorting: ";
display(arr, n);
}


int main()
{
int nComparisons = 0;
vector<int> list;
int n;
cout << "Enter the number of elements in the list: ";
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cout << "Enter element " << i+1 << ": ";
cin >> x;
list.push_back(x);
}
cout << "The list is: ";
for (int i = 0; i < n; i++) {
cout << list[i] << " ";
}
cout << "\n";
mergeSort(list, 0, n-1, nComparisons);
cout << "The sorted list is: ";
for (int i = 0; i < n; i++) {
cout << list[i] << " ";
}
cout << "\n";
cout << "The number of comparisons is: " << nComparisons << "\n";
return 0;
}