fork(1) download
  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. const int sz = 1000000;
  8.  
  9. int arr[sz];
  10. int temp[sz];
  11.  
  12. void mergeParts(int *arr, int l, int mid, int r) {
  13. int i = l, j = mid + 1;
  14. int k = 0;
  15.  
  16. while (i <= mid && j <= r) {
  17. int nextValue;
  18.  
  19. if (arr[i] < arr[j]) nextValue = arr[i++];
  20. else nextValue = arr[j++];
  21.  
  22. temp[k++] = nextValue;
  23. }
  24.  
  25. while (i <= mid) temp[k++] = arr[i++];
  26. while (j <= r) temp[k++] = arr[j++];
  27.  
  28. for (int i=0; i<k; i++) arr[l + i] = temp[i];
  29.  
  30. }
  31.  
  32. void mergeSort(int *arr, int l, int r) {
  33. if (l >= r) return;
  34.  
  35. int mid = (l + r) / 2;
  36.  
  37. mergeSort(arr, l, mid);
  38. mergeSort(arr, mid + 1, r);
  39.  
  40. mergeParts(arr, l, mid, r);
  41. }
  42.  
  43.  
  44. bool check() {
  45. for (int i=0; i<sz-1; i++){
  46. if (arr[i] > arr[i + 1]) return false;
  47. }
  48. return true;
  49. }
  50.  
  51. int main(){
  52. for (int i=0; i<sz; i++) {
  53. arr[i] = rand() % sz;
  54. }
  55.  
  56. mergeSort(arr, 0, sz - 1);
  57.  
  58. cout<<(check() ? "OK": "FAIL");
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.19s 11316KB
stdin
Standard input is empty
stdout
OK