Skip to content

doc, fix: typo in merge sort and use vector instead of raw array #2841

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 3 commits into
base: master
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
47 changes: 31 additions & 16 deletions sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* \addtogroup sorting Sorting Algorithms
* @{
* \file
* \brief [Merege Sort Algorithm
* (MEREGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation
* \brief [Merge Sort Algorithm
* (MERGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation
*
* \author [Ayaan Khan](http://github.com/ayaankhan98)
*
Expand All @@ -14,6 +14,7 @@
*
*/
#include <iostream>
#include <vector>

/**
*
Expand All @@ -31,20 +32,18 @@
* @param r - end index or right index of second half array
*/
void merge(int *arr, int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

int *L = new int[n1], *R = new int[n2];
std::vector<int> L(n1), R(n2);

for (i = 0; i < n1; i++) L[i] = arr[l + i];
for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];

i = 0;
j = 0;
k = l;
while (i < n1 || j < n2) {
if (j >= n2 || (i < n1 && L[i] <= R[j])) {
int i = 0, j = 0, k = l;

while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
Expand All @@ -54,8 +53,17 @@
k++;
}

delete[] L;
delete[] R;
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

/**
Expand Down Expand Up @@ -88,18 +96,25 @@

/** Main function */
int main() {
int size;

Check warning on line 99 in sorting/merge_sort.cpp

View workflow job for this annotation

GitHub Actions / Code Formatter

sorting/merge_sort.cpp:99:9 [cppcoreguidelines-init-variables]

variable 'size' is not initialized
std::cout << "Enter the number of elements : ";
std::cout << "Enter the number of elements: ";
std::cin >> size;

if (size <= 0) {
std::cout << "Invalid size.\n";
return 1;
}

int *arr = new int[size];

Check warning on line 108 in sorting/merge_sort.cpp

View workflow job for this annotation

GitHub Actions / Code Formatter

sorting/merge_sort.cpp:108:5 [cppcoreguidelines-owning-memory]

initializing non-owner 'int *' with a newly created 'gsl::owner<>'
std::cout << "Enter the unsorted elements : ";
std::cout << "Enter the unsorted elements: ";
for (int i = 0; i < size; ++i) {
std::cin >> arr[i];
}

mergeSort(arr, 0, size - 1);
std::cout << "Sorted array : ";
std::cout << "Sorted array: ";
show(arr, size);
delete[] arr;

Check warning on line 117 in sorting/merge_sort.cpp

View workflow job for this annotation

GitHub Actions / Code Formatter

sorting/merge_sort.cpp:117:5 [cppcoreguidelines-owning-memory]

deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead
return 0;
}
/** @} */
Loading