1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#ifndef BINARYSEARCH_H
#define BINARYSEARCH_H
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
//Global constant
const string infile_name = "testfile.bin";
//Function declarations
int32_t count_ints(string file_name); //Counts number of integers in a binary file
void value_main(string file_name, int32_t *arrptr, int size); //Reads integers from a binary file into an array
//******************************************
//RecursiveSortandSearch class declaration *
//******************************************
/* This class accepts pointers to an array of ints, and a pointer to an int indicating the array size.
It contains members to perform a recursive QuickSort to sort the array, and to perform a recursive BinarySearch
to find a given value in the array
IMPT NOTE: to retain modularity, this class does not handle memory mangement for any pointers passed to it, and as a result
does not contain a destructor to delete dynamic memory; if dynamic memory is allocated outside of this class and pointers to
that memory are passed to this class's objects, it's incumbent on functions outside of the class that allocated dynamic memory
to manage that memory after it has been used */
class RecursiveSortandSearch {
private:
int *arrptr; //Pointer to an array of ints
int *sizeptr; //Pointer to an int specifying the size of the array
int search_cntr; //Determines the number of times the binary search recurses to find a given value
//****************************************************************************
//Private function to partition a sublist as part of the QuickSort algorithm *
//****************************************************************************
int partition(int lo, int hi) {
int pivotValue, pivotIndex, mid;
mid = (lo + hi) / 2;
swap(arrptr[lo], arrptr[mid]);
pivotIndex = lo;
pivotValue = arrptr[lo];
for (int scan = lo + 1; scan <= hi; scan++) {
if (arrptr[scan] < pivotValue) {
pivotIndex++;
swap(arrptr[pivotIndex], arrptr[scan]);
}
}
swap(arrptr[lo], arrptr[pivotIndex]);
return pivotIndex;
}
//******************************************************************************
//Private function swap two values in array as part of the QuickSort algorithm *
//******************************************************************************
//THIS SEEMS TO CAUSE THE STACK OVERFLOW WHEN CALLED AS PART OF MY QUICKSORT
void swap(int &val1, int &val2) {
int temp = val1;
val1 = val2;
val2 = temp;
}
public:
//************************************************************
//Default constructor to set arrptr to nullptr and size to 0 *
//************************************************************
RecursiveSortandSearch() {
arrptr = nullptr;
sizeptr = nullptr;
};
//*********************************************************************
//Parameterized constructor to set arrptr and sizeptr to given arguments *
//*********************************************************************
RecursiveSortandSearch(int *aptr, int *sptr) {
arrptr = aptr;
sizeptr = sptr;
}
//*****************************************************
//Function to sort an array using recursive QuickSort *
//*****************************************************
/* Base case: low index and high index are the same
Recursion case: low index is less than high index */
void QuickSort(int lo, int hi) {
int pivotPoint;
if (lo < hi) {
pivotPoint = partition(lo, hi);
//Sort sublist before the pivot
QuickSort(lo, pivotPoint - 1);
//Sort sublist after the pivot
QuickSort(pivotPoint + 1, hi);
}
}
//********************************************************************************
//Function to search recursively on a sorted array using binary search algorithm *
//********************************************************************************
/* Base case: targetValue matches the middle value being searched
Base case: all values have been searched, ie (start subscript + end subscript) / 2 equals either start subscript or end subscript
Recursion case: targetValue is greater than the middle value being searched (search upper part of array)
Recursion case: targetValue is less than the middle value being searched (search lower part of array) */
void BinarySearch(int start_index, int end_index, int targetValue) {
int middle = (start_index + end_index) / 2; //Integer division is desired to maintain whole numbers
//Base case: all values have been searched and no match, ie (start subscript + end subscript) / 2 equals either start subscript or end subscript
if (arrptr[middle] != targetValue && (middle == start_index || middle == end_index)) {
cout << search_cntr << ":-" << endl;
search_cntr = 0; //Reset search counter back to 0
return;
}
//Base case: targetValue matches the middle value being searched
else if (arrptr[middle] == targetValue) {
cout << search_cntr << ":" << targetValue << endl;
search_cntr = 0;
return;
}
//Recursion case: targetValue is greater than the middle value being searched(search upper part of array)
else if (arrptr[middle] < targetValue) {
start_index = middle + 1; //Element after middle becomes new start point in level of recursion
search_cntr++;
BinarySearch(start_index, end_index, targetValue);
}
//Recursion case: targetValue is less than the middle value being searched(search lower part of array)
else if (arrptr[middle] > targetValue) {
end_index = middle - 1; //Element before middle becomes new end point in next level of recursion
search_cntr++;
BinarySearch(start_index, end_index, targetValue);
}
}
//************************************************
//TEST function to display contents of the array *
//************************************************
void display_array()
{
for (int row_num = 0; row_num < *sizeptr; row_num++)
{
//Display both the element value and eleement subscript, using formatting from <iomanip> library
cout << setw(14) << arrptr[row_num] << "(" << row_num << ")";
//Display only 5 elements per line
if (row_num != 0 && (row_num + 1) % 5 == 0)
cout << endl;
}
}
};
#endif
|