简体   繁体   English

合并排序没有得到 output

[英]Not getting output for Merge Sort

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.但我没有得到 output。意味着它在控制台屏幕上没有给出 output 甚至等待 2 分钟。 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.我在其他网站上看到使用两个不同的 arrays 并首先存储主数组值然后合并两个 arrays。我想用不同的方式来做。

You forgot about index incrementing in tail treatment in Merging .您忘记了Merging尾部处理中的索引递增。

ideone意念

while(i1<=j1){
    b[k++] = arr[i1++];
}
while(i2<=j2){
    b[k++] = arr[i2++];

The code did not execute within time limits because it encountered itself in an infinite loop.代码没有在时间限制内执行,因为它遇到了无限循环。

There are a couple of changes required:需要进行一些更改:

  1. The function declaration should correctly specify the number and types of the parameters. function 声明应正确指定参数的数量和类型。

  2. For the following two while loops, the value of the iterators i1 and i2 were never changed.对于以下两个while循环,迭代器i1i2的值从未改变。 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输入:5 4 -1 3 1

Output: Output:

Unsorted
5 4 -1 3 1 
Sorted
-1 1 3 4 5  

You forgot to increment i1 and i2 that's why it generated an infinite loop and no output was shown on console.您忘记增加 i1 和 i2 这就是它生成无限循环并且控制台上没有显示 output 的原因。

while(i1<=j1){
        b[k++] = arr[i1++];
}
while(i2<=j2){
        b[k++] = arr[i2++];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM