Advertisement
Limited_Ice

insert sort

May 18th, 2021
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. /*
  2.  
  3. David Hawkins
  4. Data Structures & Algorithms
  5. SDEV-345-81
  6. 5/18/21
  7.  
  8. Given the following set of data [23, 54, 2, 8, 3.2, 14, 43, 0, 9, 2] write an insertion sort algorithm
  9. to sort the listed data using an array. Using Big O, analyze your algorithm to show worst case
  10. running time. Make sure you show your work.
  11.  
  12. */
  13.  
  14. ////////////////////////////////////////////////////////////////////////////////////////////////
  15. //  Header Files
  16.  
  17. using namespace std;
  18.  
  19. #include<iostream>
  20. #include<iomanip>
  21.  
  22. ////////////////////////////////////////////////////////////////////////////////////////////////
  23. //  Functions Prototypes
  24.  
  25. void intro();
  26. void pad();
  27. void print(double*, int);   //  prints an array of doubles
  28. void isort(double*, int);   //  performs an insert sort with nested "for" loops
  29. void swap(double*, double*);    //  swaps the values of two doubles
  30. ////////////////////////////////////////////////////////////////////////////////////////////////
  31. //  Main Function
  32.  
  33. int main() {
  34.  
  35.     double test[10] = {23, 54, 2, 8, 3.2, 14, 43, 0, 9, 2};
  36.     int size = sizeof(test) / sizeof(test[0]);
  37.     //intro();
  38.     cout << "The array that will be sorted by insert sort is: ";
  39.     print(test, size);
  40.     cout << endl;
  41.     isort(test, size);
  42.     cout << endl;
  43.     cout << "The array after sorting is: ";
  44.     print(test, size);
  45.    
  46.  
  47.  
  48.     return 0;
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////////////////////
  51. //  Function Definitions
  52.  
  53. void intro() {
  54.  
  55.     int x = 80;
  56.     string pad;
  57.     string intro = "This Is A Demonstartion Of Insert Sort";
  58.     int cent = (80 - intro.length()) / 2;
  59.     for (int i = 0; i < cent; i++)
  60.         pad += ' ';
  61.  
  62.     cout << setw(x) << setfill('*') << '*' << endl;
  63.     cout << setfill(' ') << right << pad << intro << endl;
  64.     cout << setw(x) << setfill('*') << '*' << endl;
  65.  
  66. }
  67.  
  68. void pad() {
  69.     cout << setw(80) << setfill('*') << '*' << endl;
  70. }
  71.  
  72. void print(double* temp, int size) {
  73.     cout << "{";
  74.     for (int i = 0; i < size - 1; i++)
  75.         cout << temp[i] << ", ";
  76.     cout << temp[size - 1] << "}" << endl;
  77.     return;
  78. }
  79.  
  80. void isort(double* test, int size) {
  81.     cout << "After 0 iterations: ";
  82.     print(test, size);
  83.     for (int i = 0; i < size-1; i++) {
  84.         for (int j = i + 1; j < size; j++) {
  85.             if (test[i] > test[j])
  86.                 swap(test[i], test[j]);
  87.         }
  88.         cout << "After " << i +1 << " iterations: ";
  89.         print(test, size);     
  90.     }
  91. }
  92.  
  93. void swap(double* x, double* y) {
  94.     double temp = *x;
  95.     *x = *y;
  96.     *y = temp;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement