-
-
Save anonymous/2be98d46c7cef6dde15bac2a1724a414 to your computer and use it in GitHub Desktop.
Need Help In Merge Short
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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