Binary Search Problem

I am trying to get my binary search to search a sorted array for a key given by the user, but I am not sure how to implement that. This is all I have so far.
I want the program to say yes the key was found or not it was not found. Thanks in advance. No code is necessary I just want to know what to do next.
/* Example output of what I currently have.
Enter size of list (between 1 and 25): 3
Enter one list element: 12
Enter one list element: 5
Enter one list element: 7
Content of list: 12 5 7
Content of list: 5 7 12
Enter a key to search for:
5
//program ends
/*
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
#include <iostream>
using namespace std;

//function declaration
void printArray(int arr[], int size);
int insertion_sort(int arr[], int n);
//int binaryS(int arr[], int key);
// Driver program to test above functions
int main(){
    int key;
    int size;
    cout<<"Enter size of list (between 1 and 25): ";
    cin>>size;
    if (size <= 0 || size > 25)//if user's input is less then zero, an error is given
    {
        cout<<"ERROR: you entered an incorrect value for the array size!"<<endl;
        return 0;
    }
    int *arr = new int[size]; // creating an array of size equal to the input of the user
    
    for(int i=0; i<size; i++)
    {
        cout<<"Enter one list element: ";
        cin>>arr[i];
    }
    cout<<"Content of list: ";
    printArray(arr, size);
    
    int temp = insertion_sort(arr, size); //calling sorting algo
    cout<<"Content of list: ";
    printArray(arr, size);
    
    cout << "Enter a key to search for: " << endl;
    cin >> key;
    //binaryS(arr,size,key);
    

    
    
    
}

/*
}
	binaryS(*arr, size, key);
 
 }
 
 
 int binaryS(int arr[], int key, int left, int right) {
 while (left <= right) {
 int middle = (left + right) / 2;
 if (arr[middle] == key)
 return middle;
 else if (arr[middle] > key)
 right = middle - 1;
 else
 left = middle + 1;
 }
 return -1;
 }

*/

int insertion_sort (int arr[], int length){
    int j,temp;
    
    for (int i = 0; i < length; i++){
        j = i;
        
        while (j > 0 && arr[j] < arr[j-1]){
            temp = arr[j];
            arr[j] = arr[j-1];
            arr[j-1] = temp;
            j--;
        }
    }
    return 0;
}


/* Function to print an array */

void printArray(int arr[], int size){
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
//}



Last edited on
Topic archived. No new replies allowed.