Help with binary serch!!


Hey, I've been asked to write a program with various family members that I would then sort of order. I have succeeded with the program, but now I have to
supplement my program with a function for binary search looking for the age of a person vector. The function must have a recursive algorithm, it shall call itself.

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
#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 


class person // Class: Contains one person 
{ 
public: 

        string name; 
        int years; 
     
    void print() 
    { 
        cout << "Name: " << name <<", "<< years << " years." << endl; // Tells what to write out
    } 
    void setInfo(string _name, int _years) // Info that is needed 
    { 
        name = _name; 
        years = _years; 
    } 
}; 
int search(person p[], int n, int a) // Linear search by age 
{ 
    for (int i = 0; i < n; i++)    // Goes through the list 
    { 
        if (p[i].years == a)   // Find what we are looking for, or...
        { 
            return i; 
        } 
    } 
    return -1;    // ... the person isn't found 
}; 
void bubblesort(person p[], int n) 
{ 
    int i = 0; 
     
    for ( i = 0; i < n; i++) // Outer loop scans the entire list 
    { 
         
        int nrLeft = n - i; // Inner loop, element by element 

        for (int j = 0; j < nrLeft; j++) 
        { 
            if (p[j].years > p[j+1].years) // Compare elements 
            { 
                // Switch places 
                person temp = p[j]; 
                p[j] = p[j+1]; 
                p[j+1] = temp; 
            } 
        } 
    } 

} 

int main() { 

    // create a list of people - name / years 
    cout << "--Unordered list --" << endl << endl; // The title 

    person family[4]; 
    family[0].setInfo("Erik", 5); // Person 1. Start from 0! 
    family[0].print(); // Calls void print() 
    family[1].setInfo("Eva", 7); 
    family[1].print();  
    family[2].setInfo("Eliot", 10);  
    family[2].print();  
    family[3].setInfo("Christy", 3);  
    family[3].print(); 

    cout << endl << "-- Ordered List --" << endl << endl; // The title 

    bubblesort(family, 3); //Calls the bubblesort to sort the vector Family.
                            
    for (int i = 0; i < 4; i++) //The ordered list prints out 
        cout << "Name: " << family[i].name << ", " << family[i].years << " years." << endl; // tells what to write out
         

    int index = search(family, 4, 5); //Looking for a specific person in the Family vector containing four elements, and will be 5 years old
                               
    if (index == -1) 
        cout << "The person is not found!"; // Either not found the person or ...
    else 
        cout << endl << family[index].name << " is on place " << index << " in the list."; // ...so the program finds the person and the information is printed. 
     
    cin.get(); 
    return 0; 
}  
Last edited on
the pseudo code for binary serch is like this:

IF middle value of the array is equal to the searched value
value found
ELSE IF vector consists of only one element
searched value is not in the vector
ELSE IF searched value is less than the middle value
search in the bottom half of the vector
ELSE
search in the top half of the vector

1
2
3
4
5
6
7
8
9
10
11
int binarySearch(person p[], int lowerIndex, int upperIndex, int age){ 
  int middleIndex = (lowerIndex + upperIndex)/2; 
  if (p[middleIndex].years == age) 
    return middleIndex; //Age found 
  else if (lowerIndex == upperIndex) 
    return -1; //Age not found 
  else if (p[middleIndex].years > age) 
    return binarySearch(p, lowerIndex, middleIndex-1, age); //Check lower half 
  else if (p[middleIndex].years < age) 
    return binarySearch(p, middleIndex+1, upperIndex, age); //Check upper half 
} 


where do i put it in the code and how do i make the program call it as i asked in the first question???
Last edited on
Topic archived. No new replies allowed.