Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. //DISPLAY 7.12 Sorting an Array
  2. //Tests the procedure sort.
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7. void fill_array(vector<string> &a);
  8. //Precondition: size is the declared size of the array a.
  9. //Postcondition: number_used is the number of values stored in a.
  10. //a[0] through a[number_used - 1] have been filled with
  11. //nonnegative integers read from the keyboard.
  12.  
  13. void sort(vector<string> &a);
  14. //Precondition: number_used <= declared size of the array a.
  15. //The array elements a[0] through a[number_used - 1] have values.
  16. //Postcondition: The values of a[0] through a[number_used - 1] have
  17. //been rearranged so that a[0] <= a[1] <= ... <= a[number_used - 1].
  18.  
  19. void swap_values(string& v1, string& v2);
  20. //Interchanges the values of v1 and v2.
  21.  
  22. int index_of_smallest(const vector<string> a, int start_index);
  23. //Precondition: 0 <= start_index < number_used. Referenced array elements have
  24. //values.
  25. //Returns the index i such that a[i] is the smallest of the values
  26. //a[start_index], a[start_index + 1], ..., a[number_used - 1].
  27.  
  28. int main( )
  29. {
  30.     cout << "This program sorts numbers from lowest to highest.\n";
  31.  
  32.     vector<string> sample_array;
  33.     fill_array(sample_array);
  34.     sort(sample_array);
  35.  
  36.     cout << "In sorted order the numbers are:\n";
  37.     for (int index = 0; index < sample_array.size(); index++)
  38.         cout << sample_array[index] << " ";
  39.     cout << endl;
  40.  
  41.     return 0;
  42. }
  43.  
  44. //Uses iostream:
  45. void fill_array(vector<string> a)
  46. {
  47.     string q = "3";
  48.     a.pushback(q);
  49.     string w = "2";
  50.     a.pushback(w);
  51.     string e = "1";
  52.     a.pushback(e);
  53. }
  54.  
  55. void sort(vector<string>a)
  56. {
  57.     int index_of_next_smallest;
  58.  
  59.  //<The rest of the definition of fill_array is given in Display 7.9.>
  60.     for (int index = 0; index < a.size() - 1; index++)
  61.     {//Place the correct value in a[index]:
  62.         index_of_next_smallest =
  63.                      index_of_smallest(a, index);
  64.         swap_values(a[index], a[index_of_next_smallest]);
  65.         //a[0] <= a[1] <=...<= a[index] are the smallest of the original array
  66.         //elements. The rest of the elements are in the remaining positions.
  67.     }
  68. }
  69.  
  70.  
  71. void swap_values(string& v1, string& v2)
  72. {
  73.     string temp;
  74.     temp = v1;
  75.     v1 = v2;
  76.     v2 = temp;
  77. }
  78.  
  79.  
  80. int index_of_smallest(const int a[], int start_index, int number_used)
  81. {
  82.     int min = a[start_index],
  83.         index_of_min = start_index;
  84.     for (int index = start_index + 1; index < number_used; index++)
  85.         if (a[index] < min)
  86.         {
  87.             min = a[index];
  88.             index_of_min = index;
  89.             //min is the smallest of a[start_index] through a[index]
  90.         }
  91.  
  92.     return index_of_min;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement