Advertisement
soyan_bid

insertion_sort

Apr 17th, 2016
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void input(int &n, int the_array[]);
  6. void output(int n, int the_array[]);
  7. void sorting(int n, int the_array[]);
  8.  
  9. main()
  10. {
  11.     int n;
  12.     int arr[n];
  13.     input(n, arr);
  14.     cout << "Before sorting : " << endl;
  15.     output(n, arr);
  16.     cout << endl;
  17.     cout << "After sorting : " << endl;
  18.     sorting(n, arr);
  19. }
  20.  
  21. void input(int &n, int the_array[])
  22. {
  23.     cout << "input n : ";
  24.     cin >> n;
  25.     the_array[n];
  26.     for(int i = 0; i < n; i++)
  27.     {
  28.         cout << "[" << i+1 << "] = ";
  29.         cin >> the_array[i];
  30.     }
  31. }
  32. void output(int n, int the_array[])
  33. {
  34.     for(int i = 0; i < n; i++)
  35.     {
  36.         cout << the_array[i] << " ";
  37.     }
  38. }
  39. void sorting(int n, int the_array[])
  40. {
  41.     for(int i = 0; i < n; i++)
  42.     {
  43.         int j = i;
  44.         while((j > 0) && (the_array[j] < the_array[j-1]))
  45.         {
  46.             int temp = the_array[j];
  47.             the_array[j] = the_array[j-1];
  48.             the_array[j-1] = temp;
  49.             j = j - 1;
  50.         }
  51.     }
  52.     output(n, the_array);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement