Skip to content

Instantly share code, notes, and snippets.

/merge_short.c Secret

Created September 19, 2017 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2be98d46c7cef6dde15bac2a1724a414 to your computer and use it in GitHub Desktop.
Save anonymous/2be98d46c7cef6dde15bac2a1724a414 to your computer and use it in GitHub Desktop.
Need Help In Merge Short
#include <cs50.h>
#include <stdio.h>
void merge_short(int arr[], int start, int end);
void merge(int arr[],int start1,int mid, int mid1, int end);
int temparr[6] = {0};
int main(void)
{
int arr[] = {5,4,9,8,2,6};
int size = 6;
for(int i = 0; i < size; i++)
{
printf("%d\n",arr[i]);
}
printf("new array\n");
merge_short(arr, 0, size - 1);
for(int i = 0; i < size; i++)
{
printf("%d\n",temparr[i]);
}
}
void merge_short(int arrp[], int start, int end)
{
if(end > start)
{
int mid = (end + start) / 2;
merge_short(arrp, start, mid);
merge_short(arrp, mid+1, end);
merge(arrp, start, mid,mid+1,end);
}
}
void merge(int arrp[],int start1, int mid, int mid1, int end)
{
int count = start1;
int i = start1;
int j = mid1;
while(i <= mid && j <= end)
{
if(arrp[i] < arrp[j])
{
temparr[count] = arrp[i];
i++;
}
else
{
temparr[count] = arrp[j];
j++;
}
count++;
}
if(i > mid)
{
for(int a = j; a < end; a++)
{
temparr[count] = arrp[a];
count++;
}
}
if(j > end)
{
for(int a = i; a < mid+1; a++)
{
temparr[count] = arrp[a];
count++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment