fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void printArray(int arr[], int n){
  5. int i;
  6. for(i = 0; i < n; i++){
  7. cout << arr[i] << " ";
  8. }
  9. cout << endl;
  10. }
  11.  
  12. void linearSearch(int a[], int key, int n){
  13. int i;
  14. for(i = 0; i < n; i++){
  15. if(a[i] == key){
  16. cout << " Found at: " << i << " th index" << endl;
  17. return;
  18. }
  19. else
  20. cout << "Not Found" << endl;
  21. }
  22. }
  23.  
  24. void binarySearch(int a[], int key, int n){
  25. int i, mid;
  26. int high = n;
  27. int low = 0;
  28. while(low < high){
  29. mid = (low + high) / 2;
  30. if(a[mid] == key){
  31. cout << "Found at: " << mid << " th index" << endl;
  32. return;
  33. }
  34. else if(a[mid] < key){
  35. low = mid + 1;
  36. }
  37. else if(a[mid] > key)
  38. high = mid - 1;
  39. else
  40. cout << "Not found" << endl;
  41. }
  42. }
  43.  
  44. void bubbleSort(int a[], int n){
  45. int i, j;
  46. int temp = 0;
  47. for(i = 0; i < n; i++){
  48. for(j = 0; j < n - i - 1; j++){
  49. if(a[j] > a[j+1]){
  50. temp = a[j];
  51. a[j] = a[j+1];
  52. a[j+1] = temp;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. void selectionSort(int a[], int n){
  59. int i, j;
  60. for(i = 0; i < n; i++){
  61. int minIndex = i;
  62. for(j = i + 1; j < n; j++){
  63. if(a[minIndex] > a[j]){
  64. minIndex = j;
  65. }
  66. }
  67. int temp = a[i];
  68. a[i] = a[minIndex];
  69. a[minIndex] = temp;
  70. printArray(a, n);
  71. }
  72. }
  73.  
  74. void insertionSort(int a[], int n){
  75. int i, j, key;
  76. for(i = 1; i < n; i++){
  77. key = a[i];
  78. j = i - 1;
  79. while(a[j] > key && j >= 0){
  80. a[j+1] = a[j];
  81. j--;
  82. }
  83. a[j+1] = key;
  84. printArray(a, n);
  85. cout << endl;
  86. }
  87. }
  88.  
  89. int quickPartition(int a[], int low, int high){
  90. int i, j, pi;
  91. pi = high;
  92. i = low - 1;
  93. int temp;
  94. for(j = low; j < high; j++){
  95. if(a[j] < a[pi]){
  96. i++;
  97. temp = a[i];
  98. a[i] = a[j];
  99. a[j] = temp;
  100. }
  101. cout << "In side loop j: " << j << " th iteratio" << endl;
  102. cout << endl;
  103. cout << endl;
  104. printArray(a, high+1);
  105. }
  106. temp = a[high];
  107. a[high] = a[i+1];
  108. a[i+1] = temp;
  109. cout << "before return " << i << " th iteratio" << endl;
  110. cout << endl;
  111. cout << endl;
  112. printArray(a, high+1);
  113. return i + 1;
  114. }
  115.  
  116. void quickSort(int a[], int low, int high){
  117. if(low < high){
  118. int pi = quickPartition(a, low, high);
  119. quickSort(a, low, pi-1);
  120. quickSort(a, pi+1, high);
  121. }
  122. }
  123.  
  124. void merge(int a[], int l, int mid, int h){
  125. int n1 = (mid + 1 - l);
  126. int n2 = (h - mid);
  127. int al[n1];
  128. int ar[n2];
  129. int x, y;
  130. for(x = 0; x < n1; x++){
  131. al[x] = a[l+x];
  132. }
  133. for(x = 0; x < n2; x++){
  134. ar[x] = a[mid+1+x];
  135. }
  136. int i = 0;
  137. int j = 0;
  138. int k = l;
  139. while(i < n1 && j < n2){
  140. if(al[i] < ar[j]){
  141. a[k] = al[i];
  142. i++;
  143. }
  144. else{
  145. a[k] = ar[j];
  146. j++;
  147. }
  148. k++;
  149. }
  150. while(i < n1){
  151. a[k] = al[i];
  152. i++;
  153. k++;
  154. }
  155. while(j < n2){
  156. a[k] = ar[j];
  157. j++;
  158. k++;
  159. }
  160. }
  161.  
  162. void mergeSort(int a[], int l, int h){
  163. if(l < h){
  164. int mid = (l + h) / 2;
  165. mergeSort(a, l, mid);
  166. mergeSort(a, mid+1, h);
  167. merge(a, l, mid, h);
  168. }
  169. }
  170.  
  171. int main()
  172. {
  173. cout << "Before Sorting" << endl;
  174. int arr[10]{3, 6, 9, 13, 16, 18, 24, 23, 29, 33};
  175. int arr1[10]{34, 46, 2, 18, 36, 12, 39, 27, 45, 69};
  176. cout << endl;
  177. cout << endl;
  178. printArray(arr1, 10);
  179. mergeSort(arr1, 0, 9);
  180. cout << "After Sorting" << endl;
  181. printArray(arr1, 10);
  182. return 0;
  183. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Before Sorting


34 46 2 18 36 12 39 27 45 69 
After Sorting
2 12 18 27 34 36 39 45 46 69