How to sort two vectors (ascending order ) into a third vector using only one loop?

Asked

Viewed 2,383 times

2

My teacher posed this problem and I’m not able to sort in ascending order just by using a loop to sort.

#include<stdio.h>

main(){
    int i,a[5]={1,4,8,9,11},b[5]={3,6,7,10,15},c[10];

    for(i=0;i<10;i++){

        if(a[i]<b[i] || b[i]>a[i]){

        }
    }

    for(i=0;i<10;i++){
        printf("%d\n",c[i]);            
    }   
}
  • Look for the Merge Sort algorithm

  • Have you ever thought of doing one for inside another for testing conditions?

1 answer

-1

#include <stdio.h>
int main() {
    int i, j=0, k=0, a[5]={1,4,8,9,11}, b[5]={3,6,7,10,15}, c[10];
    for(i=0; i<10; i++) {
        if (a[j] < b[k]) {
            c[i] = a[j];
            j++;
        }
        else {
            c[i] = b[k];
            k++;
    }
    for (i=0; i<10; i++) {
        printf("%d\n",c[i]);
    }
    return 0;
}
  • I recommend reading the following link, just play the code ready does not help much, try to explain what turn of change so that those who asked can learn from your answer. https://answall.com/help/how-to-answer

Browser other questions tagged

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