Reputation: 15
I am trying this merge sort. But I am getting no output. Means it is giving no output on console screen even waited for 2 min. Anyone of you please help me with this. Where should I correct myself.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void MergeSort();
void Merging();
int main() {
int arr[100];
int i;
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
printf("Unsorted\n");
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
MergeSort(arr, 0, 4);
printf("Sorted\n");
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
void MergeSort(int arr[], int l, int h) {
if (l < h) {
int mid = (l + h) / 2;
MergeSort(arr, l, mid);
MergeSort(arr, mid + 1, h);
Merging(arr, l, mid, h);
}
}
void Merging(int arr[], int l, int mid, int h) {
int i1 = l, i2 = mid + 1, j1 = mid, j2 = h;
int b[6];
int k = l;
while (i1 <= j1 && i2 <= j2) {
if (arr[i1] < arr[i2]) {
b[k++] = arr[i1++];
} else {
b[k++] = arr[i2++];
}
}
while (i1 <= j1) {
b[k++] = arr[i1];
}
while (i2 <= j2) {
b[k++] = arr[i2];
}
int i;
for (i = l; i <= h; i++) {
arr[i] = b[i];
}
}
I have seen in other sites using two different arrays and first storing main array values then merging two arrays. I thought of doing it different way.
Upvotes: 0
Views: 151
Reputation: 157
You forgot to increment i1 and i2 that's why it generated an infinite loop and no output was shown on console.
while(i1<=j1){
b[k++] = arr[i1++];
}
while(i2<=j2){
b[k++] = arr[i2++];
Upvotes: 1
Reputation: 5309
The code did not execute within time limits because it encountered itself in an infinite loop.
There are a couple of changes required:
The function declaration should correctly specify the number and types of the parameters.
For the following two while
loops, the value of the iterators i1
and i2
were never changed. Therefore, it caused an infinite loop and nothing was displayed on the screen.
while(i1<=j1){
b[k++] = arr[i1++];
}
while(i2<=j2){
b[k++] = arr[i2++];
}
Have a look at the following corrected code:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void MergeSort(int arr[],int l,int h);
void Merging(int arr[], int l, int mid, int h);
int main(){
int arr[100];
int i;
for(i=0;i<5;i++){
scanf("%d",&arr[i]);
}
printf("Unsorted\n");
for(i=0;i<5;i++){
printf("%d ",arr[i]);
}
printf("\n");
MergeSort(arr,0,4);
printf("Sorted\n");
for(i=0;i<5;i++){
printf("%d ",arr[i]);
}
return 0;
}
void MergeSort(int arr[],int l,int h){
if(l<h){
int mid = (l+h)/2;
MergeSort(arr,l,mid);
MergeSort(arr,mid+1,h);
Merging(arr,l,mid,h);
}
}
void Merging(int arr[], int l, int mid, int h){
int i1 = l,i2 = mid+1, j1 = mid, j2 = h;
int b[6];
int k=l;
while(i1<=j1&&i2<=j2){
if(arr[i1]<arr[i2]){
b[k++] = arr[i1++];
}else{
b[k++] = arr[i2++];
}
}
while(i1<=j1){
b[k++] = arr[i1++];
}
while(i2<=j2){
b[k++] = arr[i2++];
}
int i;
for(i=l;i<=h;i++){
arr[i] = b[i];
}
}
Testing:
Input: 5 4 -1 3 1
Output:
Unsorted
5 4 -1 3 1
Sorted
-1 1 3 4 5
Upvotes: 1