Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. //*****************************************************************************
  2. //
  3. //   Insertion Sort.cpp
  4. //   Input:  list length n, and array list.
  5. //   Output: list in increasing order.
  6. //
  7. //*****************************************************************************
  8.  
  9. #include<iostream>
  10. using namespace std;
  11.  
  12. #define MAX_LIST_LEN  100
  13.  
  14. int main(){
  15.  
  16.    int    n,                    // list length
  17.           j,                    // loop control
  18.           i;                    // index of maximum element in unsorted section
  19.                                 // index of rightmost element in unsorted section
  20.    double max,                  // maximum value in unsorted section
  21.           temp,                 // temporary storage
  22.           list[MAX_LIST_LEN];   // array to be sorted
  23.  
  24.    // Get values for n and list.
  25.    cout << "Enter list length (must be less than or equal to "
  26.         << MAX_LIST_LEN << "): ";
  27.    cin  >> n;
  28.    cout << "Enter " << n << " numbers:" << endl;
  29.    for(j=0; j<n; j++){
  30.       cin >> list[j];
  31.    }
  32.  
  33.    // perform InsertionSort algorithm //
  34.  
  35.  
  36.       for (i = 1; i < n; i++) {
  37.             j = i;
  38.             while (j > 0 && list[j - 1] > list[j]) {
  39.                   temp = list[j];
  40.                   list[j] = list[j - 1];
  41.                   list[j - 1] = temp;
  42.                   j--;
  43.             }
  44.       }
  45.  
  46. // Print out sorted list.
  47.    for(j=0; j<n; j++){
  48.       cout << list[j] << " ";
  49.    }
  50.    cout << endl;
  51.  
  52.    return(0);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement