Advertisement
JoshuaNHardison

lab9.cpp

Nov 6th, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int binarySearch(const int [], int, int);
  7. void insertionSort(int*, int);
  8.  
  9.  
  10.  
  11.  
  12. int main()
  13. {
  14.     int luck1, luck2, luck3, luck4, luck5;
  15.     const int SIZE = 5;
  16.     int winner[SIZE];
  17.     cout << winner << endl;
  18.     cout << "Mrs. Crockett, enter in your lucky numbers!\n";
  19.     cout << "1:     ";
  20.     cin >> luck1;
  21.     cout << "2:     ";
  22.     cin >> luck2;
  23.     cout << "3:     ";
  24.     cin >> luck3;
  25.     cout << "4:     ";
  26.     cin >> luck4;
  27.     cout << "5:     ";
  28.     cin >> luck5;
  29.    
  30.     cout << "The winning lottery ticket number is: "  << endl;
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. int binarySearch(const int array[], int size, int value)
  43. {
  44.     int first = 0,             // First array element
  45.     last = size - 1,       // Last array element
  46.     middle,                // Mid point of search
  47.     position = -1;         // Position of search value
  48.     bool found = false;        // Flag
  49.  
  50.     while (!found && first <= last)
  51.     {
  52.         middle = (first + last) / 2;     // Calculate mid point
  53.         if (array[middle] == value)      // If value is found at mid
  54.         {
  55.             found = true;
  56.             position = middle;
  57.         }
  58.         else if (array[middle] > value)  // If value is in lower half
  59.             last = middle - 1;
  60.         else
  61.             first = middle + 1;           // If value is in upper half
  62.     }
  63.     return position;
  64. }
  65.  
  66. void insertionSort(int *arr, int size)
  67. {
  68.     int key; //key is the item that we are sorting at the moment
  69.     int j;  //j is the position of where key should be inserted into the sorted array
  70.    
  71.     cout << "\nSorting the array with the Insertion Sort algorithm.\n\n";
  72.    
  73.     //i will hold the subscript of the unsorted array
  74.     for(int i=1; i < size; i++)
  75.     {
  76.         key = arr[i]; //select the item to be sorted
  77.         j = i-1; //j is the element to the furthest right of the sorted array
  78.         while(j >= 0 && arr[j] > key) //while we haven't gone out of bounds to the left of the array and the current element is still greater than key
  79.         {
  80.             arr[j+1] = arr[j]; //move elements to the right of the sorted array that are greater than the item (key)
  81.             j = j-1; //decrement j
  82.         }
  83.         arr[j+1] = key; //place item in final sorted position
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement