BinarySearch and pointers

Hi,

my problem in this program is that I don't know how to use binary search in this code :
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
#include <iostream>
#include <ctime>

using namespace std;

int* InsertionSort(int* a, int n) 
{
	int x; 
	if (n < 0 || a == NULL)
	{
		return NULL;
	}
	int* p = new int[n];
	if (n == 0)
	{
		return p;
	}
	p[0] = a[0];
	for(int i=0; i<n; i++)
	{
		x = i;
		for(int j=0; j<n; j++)
		{
			if(a[i]<p[j])
			{
				for(int k=i; k>j; k--)
				{
					p[k] = p[k-1];
					x = j;
					break;
				}
			}
			p[x] = a[i];
		}
	}
	return p;
}


int binarySearch(int el, int* a, int n)
{
	int i = 0;
	int j = 0;
	while(i <= n)
	{
		j = (i + n) / 2;
		if(a[j] < el)
			i = j+1;
		else 
			if(a[j] > el)
				n = j - 1;
			else
				return j;
	}
	return -1;
}

void print(int* a, int n)
{
	for (int i=0; i<n; i++)
	{
		cout << a[i] << ",";
	}
}

int main() 
{
	int arr[]={109, 114, 114, 71, 51, 106, 51, 79, 115, 42};
	const int length = sizeof(arr)/sizeof(int);
	int element = 115;

	int* sortedArray = InsertionSort(arr, length);
	print(arr,length);
	//binarySearch(element,sortedArray, length);
	cout << endl;
	system("pause");
	return 0;
}


Problam is that I have elements in binary search : 42,42,42,42,42,42,42,42,42,114, and dont know how to access to elements : 42,51,71,79...114. I know that pointer always return first element of array but I don't know how to access to other elements in my BinarySort. Thank you for help.
Topic archived. No new replies allowed.