Mergesort Liguagem C

Asked

Viewed 25 times

-1

I’m doing a job for college and I’m having a little problem with my code because it’s the first time I’m having contact with Mergesort. I’m trying to make me exit it on the screen be kind of:

Loja 1: 1
Loja 2: 8
loja 3: 7
Loja 4: 8
Loja 5: 3
Loja 6: 3
Loja 7: 7
Loja 8: 5

Instead of just:

1, 8, 7, 8, 3, 3, 7, 5
#include <stdio.h>

#define max 7

int a [8] = { 1, 8, 7, 8, 3, 3, 7, 5};    
int b [7];

void merging (int low, int mid, int high){        
    int l1, l2, i;
    
    for (l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++){            
        if (a[l1] <= a[l2])
            b [i] = a[l1++];
        else
            b[i] = a[l2++];            
    }
    
    while(l1 <= mid)
        b[i++] = a[l1++];
        
    while(l2 <= high)
        b[i++] = a[l2++];
        
    for (i = low; i <= high; i++)
        a[i] = b[i];
}

void sort(int low, int high){    
    int mid;
    
    if(low < high){            
        mid = (low + high) / 2;
        sort(low, mid);
        sort(mid + 1, high);
        merging(low, mid, high);            
    }else{            
        return;            
    }
}

int main(){
    int i;
    
    printf("Lista antes da classificação:\n\n");
    
    for(i = 0; i <= max; i++)
        printf("%d ", a[i]);
        
    sort(0, max);
    
    printf("\n\nLista após a classificação:\n\n");
    
    for(i = 0; i <= max; i++)
        printf("%d ", a[i]);
}
  • not only write the text you want as output on printf("%d ", a[i]);?

  • I would like it to be so, but that is not the way you want it, I will try that and see if it would be an accepted answer to that work

1 answer

2


From what I understand, this is your current comeback:

1, 8, 7, 8, 3, 3, 7, 5

So what you need to do is instead of just printing "number, ", you print the text you need:

printf("Loja %d: %d\n", i, a[i]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.