Skip to content
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

Merge sort added more efficient #1294

Closed
wants to merge 3 commits into from
Closed
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
183 changes: 81 additions & 102 deletions sorting/merge_sort.c
Original file line number Diff line number Diff line change
@@ -1,139 +1,118 @@
/**
* @file
* @brief Implementation of [merge
* sort](https://en.wikipedia.org/wiki/Merge_sort) algorithm
*@author [Bama Charan Chhandogi](https://github.com/BamaCharanChhandogi)
*/
#include <assert.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these need to be documented as well

#include <header.h> /// <reason for header.h>

#include <stdio.h>
#include <stdlib.h>

/**
* @addtogroup sorting Sorting algorithms
* @{
*/
/** Swap two integer variables
* @param [in,out] a pointer to first variable
* @param [in,out] b pointer to second variable
*/
void swap(int *a, int *b)
// Function to merge two sorted subarrays into one sorted array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why have you decreased the documentation please restore the old documentation

Copy link
Member Author

@BamaCharanChhandogi BamaCharanChhandogi Aug 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the documentation of the "swap" function. because here I don't use the Swap function. so why I write documentation if it! I wrote more efficient code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood! Sorry for the confusion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood! Sorry for the confusion.

void merge(int arr[], int left, int mid, int right)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;

/**
* @brief Perform merge of segments.
*
* @param a array to sort
* @param l left index for merge
* @param r right index for merge
* @param n total number of elements in the array
*/
void merge(int *a, int l, int r, int n)
{
int *b = (int *)malloc(n * sizeof(int)); /* dynamic memory must be freed */
if (b == NULL)
{
printf("Can't Malloc! Please try again.");
exit(EXIT_FAILURE);
}
int c = l;
int p1, p2;
p1 = l;
p2 = ((l + r) / 2) + 1;
while ((p1 < ((l + r) / 2) + 1) && (p2 < r + 1))
// Create temporary arrays to store the left and right subarrays
int L[n1], R[n2];

// Copy data to temporary arrays
for (i = 0; i < n1; i++) L[i] = arr[left + i];
for (j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];

// Merge the temporary arrays back into arr[left..right]
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (a[p1] <= a[p2])
if (L[i] <= R[j])
{
b[c++] = a[p1];
p1++;
arr[k] = L[i];
i++;
}
else
{
b[c++] = a[p2];
p2++;
arr[k] = R[j];
j++;
}
k++;
}

if (p2 == r + 1)
// Copy the remaining elements of L and R, if any
while (i < n1)
{
while ((p1 < ((l + r) / 2) + 1))
{
b[c++] = a[p1];
p1++;
}
arr[k] = L[i];
i++;
k++;
}
else
while (j < n2)
{
while ((p2 < r + 1))
{
b[c++] = a[p2];
p2++;
}
arr[k] = R[j];
j++;
k++;
}

for (c = l; c < r + 1; c++) a[c] = b[c];

free(b);
}

/** Merge sort algorithm implementation
* @param a array to sort
* @param n number of elements in the array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this style of documentation was prefered

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added these portions.

* @param l index to sort from
* @param r index to sort till
* @param arr the array
* @param left index to sort from
* @param right index to sort till
*/
void merge_sort(int *a, int n, int l, int r)
void merge_sort(int arr[], int left, int right)
{
if (r - l == 1)
if (left < right)
{
if (a[l] > a[r])
swap(&a[l], &a[r]);
}
else if (l != r)
{
merge_sort(a, n, l, (l + r) / 2);
merge_sort(a, n, ((l + r) / 2) + 1, r);
merge(a, l, r, n);
}
int mid = left + (right - left) / 2;

// Sort the left and right halves separately
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);

/* no change if l == r */
// Merge the two sorted halves
merge(arr, left, mid, right);
}
}
/** @} */

/** Main function */
int main(void)
#define MAX 100 // Maximum array size for testing
// Function to check if an array is sorted in ascending order
int is_sorted(int arr[], int size)
{
int *a, n, i;
printf("Enter Array size: ");
scanf("%d", &n);
if (n <= 0) /* exit program if arraysize is not greater than 0 */
for (int i = 0; i < size - 1; i++)
{
printf("Array size must be Greater than 0!\n");
return 1;
}
a = (int *)malloc(n * sizeof(int));
if (a == NULL) /* exit program if can't malloc memory */
{
printf("Can't Malloc! Please try again.");
return 1;
}
for (i = 0; i < n; i++)
{
printf("Enter number[%d]: ", i);
scanf("%d", &a[i]);
if (arr[i] > arr[i + 1])
{
return 0; // Not sorted
}
}
return 1; // Sorted
}

merge_sort(a, n, 0, n - 1);
printf("Sorted Array: ");
for (i = 0; i < n; i++)
// Test function for merge sort
static void test_merge_sort()
{
int test_arr[MAX];

// Populate the test array with random integers
for (int i = 0; i < MAX; i++)
{
printf("%d ", a[i]);
test_arr[i] = rand() % 1000; // Using a larger range for better testing
}
printf("\n");

free(a);
// Perform merge sort on the test array
merge_sort(test_arr, 0, MAX - 1);

// Check if the array is sorted
assert(is_sorted(test_arr, MAX));

printf("Merge sort test passed!\n");
}
/**
* @brief main function
* @returns 0 on successful exit
**/
int main()
{
// Run the merge sort test function
test_merge_sort();

return 0;
}