Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. const int KEY_NOT_FOUND = -1;
  5.  
  6. /* Binary search
  7.  * It finds the key in the vector<int>
  8.  */
  9. int BinarySearch(const std::vector <int>* array, int first_index, int last_index, const int key) {
  10.     while (first_index <= last_index)   {
  11.         int middle_index = first_index + (last_index - first_index) / 2;
  12.        
  13.         if (array->at(middle_index) > key) {
  14.             last_index = middle_index - 1;
  15.         }
  16.         else if (array->at(middle_index) < key) {
  17.             first_index = middle_index + 1;
  18.         }
  19.         else {
  20.             return middle_index;
  21.         }
  22.     }
  23.     return KEY_NOT_FOUND;
  24. }
  25.  
  26. /* Recursive function for a binary search
  27.  * It finds the key in the vector<int>
  28.  */
  29. int RecursiveBinarySearch(const std::vector <int>* array, const int first_index, const int last_index, const int key) {
  30.     if (first_index == last_index) {
  31.         if (key == array->at(first_index)) {
  32.             return first_index;
  33.         }
  34.         else {
  35.             return KEY_NOT_FOUND;
  36.         }
  37.     }
  38.     else {
  39.         int middle_index = first_index + (last_index - first_index) / 2;
  40.         if (key == array->at(middle_index)) {
  41.             return middle_index;
  42.         }
  43.         else {
  44.             if (key < array->at(middle_index)) {
  45.                 RecursiveBinarySearch(array, first_index, middle_index - 1, key);
  46.             }
  47.             else {
  48.                 RecursiveBinarySearch(array, middle_index + 1, last_index, key);
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. /* QuickSort first divides a large array into two smaller sub-array:
  55.  * the low elements and the high elements.
  56.  * QuickSort can then recursively sort the sub-arrays.
  57. */
  58. void QuickSort(std::vector <int>* array, const int first_index, const int last_index) {
  59.     int left_index = first_index;
  60.     int right_index = last_index;
  61.     int pivot = array->at((first_index + last_index) / 2);
  62.    
  63.     while (left_index <= right_index) {
  64.         while (array->at(left_index) < pivot) {
  65.             ++left_index;
  66.         }
  67.         while (array->at(right_index) > pivot) {
  68.             --right_index;
  69.         }
  70.         if (left_index <= right_index) {
  71.             std::swap(array->at(left_index), array->at(right_index));
  72.             ++left_index;
  73.             --right_index;
  74.         }
  75.     }
  76.  
  77.     if (first_index < right_index) {
  78.         QuickSort(array, first_index, right_index);
  79.     }
  80.     if (left_index < last_index) {
  81.         QuickSort(array, left_index, last_index);
  82.     }
  83. }
  84.  
  85. int main()
  86. {
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement