fork download
  1. /*
  2.   Copyright 2011 Marek "p2004a" Rusinowski
  3.   Merge sort
  4. */
  5. #include <cstdio>
  6.  
  7. #define MAXN 1000000
  8.  
  9. int array[MAXN];
  10. int tmp_array[MAXN];
  11.  
  12. void merge(int *begin, int *center, int *end) {
  13. int *i1 = begin, *i2 = center, i = 0;
  14. while (i1 != center || i2 != end) {
  15. if (((*i1 < *i2) || (i2 == end)) && (i1 != center)) {
  16. tmp_array[i++] = *(i1++);
  17. } else {
  18. tmp_array[i++] = *(i2++);
  19. }
  20. }
  21. for (int j = 0; begin != end; ++j) {
  22. *(begin++) = tmp_array[j];
  23. }
  24. }
  25.  
  26. void sort(int *begin, int *end) {
  27. if (begin + 1 >= end) return;
  28. int *center = (end - begin) / 2 + begin;
  29. sort(begin, center);
  30. sort(center, end);
  31. merge(begin, center, end);
  32. }
  33.  
  34. int main() {
  35. int n;
  36. scanf("%d", &n);
  37. for (int i = 0; i < n; ++i) {
  38. scanf("%d", &array[i]);
  39. }
  40. sort(array, array + n);
  41. for (int i = 0; i < n; ++i) {
  42. printf("%d ", array[i]);
  43. }
  44. printf("\n");
  45. return 0;
  46. }
  47.  
stdin
10
10 9 8 7 6 5 4 3 2 1
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:36: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout
1 2 3 4 5 6 7 8 9 10