0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Merge Sort マージソート

Last updated at Posted at 2025-01-17

We can use the following C program to merge 2 sorted sequence.

#include <stdio.h>

#define M 10
#define N 5

int main(){
  int a[] = {2,4,5,7,8,10,15,20,30,40},
      b[] = {6,11,25,33,35},
      c[M+N];

      int i = 0;
      int j = 0;
      int p = 0;

while(i<M && j<N){
  if(a[i]<=b[j]){
    c[p++] = a[i++];
  }
  if(a[i]>=b[j]){
    c[p++] = b[j++];
  }
}
while(i<M){
  c[p++] = a[i++];
}
while(j<N){
  c[p++] = b[j++];
}

for(i=0;i<M+N;i++){
  printf("%d ",c[i]);
}
printf("\n");

return 0;
}
0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?