Paste Search Dynamic
Recent pastes
insertionSort
  1. // C++ program for insertion sort
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /* Function to sort an array using insertion sort*/
  6. void insertionSort(int arr[], int n)
  7. {
  8.         int i, key, j;
  9.         for (i = 1; i < n; i++)
  10.         {
  11.                 key = arr[i];
  12.                 j = i - 1;
  13.  
  14.                 /* Move elements of arr[0..i-1], that are
  15.                 greater than key, to one position ahead
  16.                 of their current position */
  17.                 while (j >= 0 && arr[j] > key)
  18.                 {
  19.                         arr[j + 1] = arr[j];
  20.                         j = j - 1;
  21.                 }
  22.                 arr[j + 1] = key;
  23.         }
  24. }
  25.  
  26. // A utility function to print an array of size n
  27. void printArray(int arr[], int n)
  28. {
  29.         int i;
  30.         for (i = 0; i < n; i++)
  31.                 cout << arr[i] << " ";
  32.         cout << endl;
  33. }
  34.  
  35. /* Driver code */
  36. int main()
  37. {
  38.         int arr[] = {2 92 25 68 5 60 91 93 59 80 17 84 78 24 46 54 52 13 38 94 77 40 69 75 55 86 74 72 53 19 1 81 8 83 90 89 37 28 45 41 14 70 62 21 35 76 98 4 50 15 99 23 18 10 43 16 73 39 65 100 56 71 51 7 31 97 9 26 67 44 82 27 6 57 64 30 49 36 87 32 48 85 79 29 11 12 96 22 61 3 42 47 95 63 58 20 34 66 88 33 };
  39.         int n = sizeof(arr) / sizeof(arr[0]);
  40.  
  41.         insertionSort(arr, n);
  42.         printArray(arr, n);
  43.  
  44.         return 0;
  45. }
  46.  
  47. // This is code is contributed by rathbhupendra
  48.  
Parsed in 0.004 seconds