fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void merge(int L[], int p, int R[], int q, int a[]){
  6. int i = 0, j = 0, k = 0;
  7.  
  8. while(i < p && j < q){
  9. if(L[i] <= R[j])
  10. a[k] = L[i++];
  11. else
  12. a[k] = R[j++];
  13. k++;
  14. }
  15. //while(i < p) a[k++] = L[i++];
  16. //while(j < q) a[k++] = R[j++];
  17. if(i == p)
  18. memcpy(&a[k], &R[j], sizeof(R[0]) * (q - j));
  19. else
  20. memcpy(&a[k], &L[j], sizeof(L[0]) * (p - i));
  21. }
  22.  
  23. void mergesort(int a[], int n){
  24. int i;
  25. int split = n / 2;
  26. int *L = (int *)malloc(split);
  27. int *R = (int *)malloc(n - split);
  28.  
  29. if(n > 1){
  30. for(i = 0; i < split; i++)
  31. L[i] = a[i];
  32. for(i = split; i < n; i++)
  33. R[i - split] = a[i];
  34. mergesort(L, split);
  35. mergesort(R, n - split);
  36. merge(L, split, R, n - split, a);
  37. }
  38. }
  39.  
  40. int main(int argc, char *argv[]){
  41. int i;
  42. int v[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
  43.  
  44. mergesort(v, 10);
  45.  
  46. for(i = 0; i < 10; i++)
  47. printf("%d ", v[i]);
  48.  
  49. scanf("%d", &i);
  50. return 0;
  51. }
Runtime error #stdin #stdout #stderr 0s 2240KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.