fork download
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<math.h>
  4. using namespace std;
  5.  
  6. void MergeSort(int** , int, int);
  7. void Merge(int**, int, int, int);
  8. void duplicate_array(int**, int**, int, int);
  9. void printout_array(int**,int,int);
  10. void ins_sort(int**, int, int);
  11. int main(){
  12.  
  13. int n, a_min, a_max, rseed;
  14.  
  15. cout << "Please input [n, a_min, a_max. rseed]" << endl ;
  16. cin >> n >> a_min >> a_max >> rseed;
  17.  
  18. int** forI = new int*[1];// Building Dynamic array
  19. int** forM = new int*[1];
  20. for (int i = 1 ; i <= 1 ; i++){
  21. forI[i] = new int [n];
  22. forM[i] = new int [n];
  23. }
  24.  
  25.  
  26. srand(rseed); //initial randomly variable
  27. cout << "Randomly sequence :" << endl;
  28. for(int i = 1; i <= n; i++){ //randomly variable between a_min & a_max
  29. int x = rand()%(a_max-a_min+1) + a_min;
  30. forI[1][i] = x;
  31. }
  32. cout << forI[1][10] << endl;
  33. printout_array(forI, 1, n);
  34. duplicate_array(forI, forM, 1, n); //copy to another array that for merge sort
  35.  
  36. cout << endl << "By insertion sort" << endl ;
  37. ins_sort(forI, 1, n);
  38. printout_array(forI, 1, n);
  39.  
  40. cout << endl << "By merge sort" << endl ;
  41.  
  42. MergeSort(forM, 1, n);
  43. printout_array(forM, 1, n);
  44.  
  45. for(int i = 0; i <= n; i++){ // delete pointer array
  46. delete []forI[i];
  47. delete []forM[i];
  48. }
  49. delete []forI;
  50. delete []forM;
  51.  
  52. return 0;
  53. }
  54.  
  55. void ins_sort(int**a, int raw, int col){ //raw = 1
  56. int i, j, key, rec;
  57. for(i = 1; i <= raw; i++){ //key is additional place
  58. for(j = 2; j <= col; j++)
  59. {
  60. key = a[i][j];
  61. rec = j; // record position of j
  62. while (a[i][rec-1] < key && rec > 1 ) //the value of the left of the space is bigger than key
  63. {
  64. a[i][rec] = a[i][rec-1]; //move to left space
  65. rec--;
  66. }
  67. a[i][rec] = key; //the last space equal to key
  68. }
  69. }
  70. }
  71.  
  72. void MergeSort(int **a, int low, int high)
  73. {
  74. int mid;
  75. if (low < high)
  76. {
  77. mid = (low + high) / 2;
  78. // divide into two blocks
  79. MergeSort(a, low, mid);
  80. MergeSort(a, mid+1, high);
  81. //make a recursion
  82. // start to mergec
  83. Merge(a, low, high, mid);
  84. }
  85. }
  86.  
  87. void Merge(int **a, int low, int high, int mid){
  88. // low -> middle & middle -> high have been sorted
  89. int i, j, k;
  90. int ram[high - low + 1];
  91. i = low;
  92. k = 0;
  93. j = mid + 1;
  94.  
  95. // merge into ram[]
  96. while (i <= mid && j <= high){
  97. if (a[1][i] > a[1][j]){
  98. ram[k] = a[1][i];
  99. k++;
  100. i++;
  101. }
  102. else{
  103. ram[k] = a[1][j];
  104. k++;
  105. j++;
  106. }
  107.  
  108. }
  109. // insert remaining function
  110. while (i <= mid){
  111. ram[k] = a[1][i];
  112. k++;
  113. i++;
  114. }
  115. while (j <= high){
  116. ram[k] = a[1][j];
  117. k++;
  118. j++;
  119. }
  120. // copy data from ram[] to a[][]
  121. for (i = low; i <= high; i++){
  122. a[1][i] = ram[i-low];
  123. }
  124. }
  125.  
  126.  
  127. void printout_array(int**A, int b, int c){//printout every single element of A[][]
  128. for(int i = 1; i <= b; i++){
  129. for(int j = 1; j <= c; j++){
  130. cout << A[i][j] << " ";
  131. }
  132. }
  133. }
  134.  
  135. void duplicate_array(int**O, int**C,int m,int n){ //copy array
  136. for(int i = 1; i <= m; i++)
  137. {
  138. for(int j = 0; j <= n; j++)
  139. {
  140. C[i][j] = O[i][j];
  141. }
  142. }
  143.  
  144. }
  145.  
Runtime error #stdin #stdout #stderr 0s 80768KB
stdin
Standard input is empty
stdout
Please input [n, a_min, a_max. rseed]
Randomly sequence :
0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
By insertion sort
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
By merge sort
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
stderr
*** Error in `./prog': double free or corruption (!prev): 0x0000560754d16c80 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x2b97c17fcbcb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x2b97c1802f96]
/lib/x86_64-linux-gnu/libc.so.6(+0x7778e)[0x2b97c180378e]
./prog(+0xcaf)[0x560752ddecaf]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x2b97c17ac2b1]
./prog(+0xd5a)[0x560752dded5a]
======= Memory map: ========
2b97c0aac000-2b97c0acf000 r-xp 00000000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2b97c0acf000-2b97c0ad3000 rw-p 00000000 00:00 0 
2b97c0adc000-2b97c0ae1000 rw-p 00000000 00:00 0 
2b97c0ccf000-2b97c0cd0000 r--p 00023000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2b97c0cd0000-2b97c0cd1000 rw-p 00024000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2b97c0cd1000-2b97c0cd2000 rw-p 00000000 00:00 0 
2b97c0cd2000-2b97c0e44000 r-xp 00000000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b97c0e44000-2b97c1044000 ---p 00172000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b97c1044000-2b97c104e000 r--p 00172000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b97c104e000-2b97c1050000 rw-p 0017c000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b97c1050000-2b97c1054000 rw-p 00000000 00:00 0 
2b97c1054000-2b97c1157000 r-xp 00000000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2b97c1157000-2b97c1356000 ---p 00103000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2b97c1356000-2b97c1357000 r--p 00102000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2b97c1357000-2b97c1358000 rw-p 00103000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2b97c1358000-2b97c136e000 r-xp 00000000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b97c136e000-2b97c156d000 ---p 00016000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b97c156d000-2b97c156e000 r--p 00015000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b97c156e000-2b97c156f000 rw-p 00016000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b97c156f000-2b97c1587000 r-xp 00000000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b97c1587000-2b97c1786000 ---p 00018000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b97c1786000-2b97c1787000 r--p 00017000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b97c1787000-2b97c1788000 rw-p 00018000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b97c1788000-2b97c178c000 rw-p 00000000 00:00 0 
2b97c178c000-2b97c1921000 r-xp 00000000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2b97c1921000-2b97c1b20000 ---p 00195000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2b97c1b20000-2b97c1b24000 r--p 00194000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2b97c1b24000-2b97c1b26000 rw-p 00198000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2b97c1b26000-2b97c1b2a000 rw-p 00000000 00:00 0 
2b97c4000000-2b97c4021000 rw-p 00000000 00:00 0 
2b97c4021000-2b97c8000000 ---p 00000000 00:00 0 
560752dde000-560752de0000 r-xp 00000000 fd:00 25933826                   /home/7b1AZL/prog
560752fdf000-560752fe0000 r--p 00001000 fd:00 25933826                   /home/7b1AZL/prog
560752fe0000-560752fe1000 rw-p 00002000 fd:00 25933826                   /home/7b1AZL/prog
560754d03000-560754d35000 rw-p 00000000 00:00 0                          [heap]
7ffd09505000-7ffd09526000 rw-p 00000000 00:00 0                          [stack]
7ffd095d0000-7ffd095d2000 r-xp 00000000 00:00 0                          [vdso]
7ffd095d2000-7ffd095d4000 r--p 00000000 00:00 0                          [vvar]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]