-
Notifications
You must be signed in to change notification settings - Fork 0
/
binSearch.cpp
78 lines (66 loc) · 1.64 KB
/
binSearch.cpp
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 <iomanip>
#include <time.h.>
#include <cstdlib>
using namespace std;
int partition(int arr[], int left, int right)
{
int x=arr[left]; //element dzielacy
int i=left-1;
int j=right+1;
while(true)
{
do {i++;} while(arr[i]<x);
do {j--;} while(arr[j]>x); //wyszukiwanie elementow do zamiany
if(i<j) swap(arr[i], arr[j]);
else return j; //zwraca indeks elementu dzielacego
}
}
void qSort(int arr[], int left, int right)
{
if(left<right)
{
int x=partition(arr, left, right);
qSort(arr, left, x);
qSort(arr, x+1, right);
}
}
bool binSearch(int arr[], int value, int n)
{
int left=0;
int right=n-1;
int m=(n-1)/2;
while(left<right&&(arr[m]!=value))
{
m=(left+right)/2;
if(arr[m]<value) left=m+1;
else right=m;
}
return arr[m]==value;
}
int main() //Program g³ówny//
{
int i;
int size;
cout<<"Rozmiar tablicy: ";
cin>>size;
int t[size];
cout<<"Tablica przed sortowaniem:\n\n";
// Wype³niam tablicê t[] liczbami pseudolosowymi i wyœwietlam jej zawartoœæ
srand((unsigned)time(NULL));
for(i=0; i<size; i++) t[i]=rand()%100; //liczba pseudolosowa z zakresu 0-100//
for(i=0; i<size; i++) cout<<setw(4)<<t[i];
cout<<endl;
// Sortowanie
qSort(t, 0, size-1);
// Wynik sortowania
cout<<"Po sortowaniu:\n\n";
for(i=0; i<size; i++) cout<<setw(4)<<t[i];
cout<<endl;
cout<<"Szukana wartosc: ";
int toBeFound;
cin>>toBeFound;
if(binSearch(t, toBeFound, size)) cout<<"TAK";
else cout<<"NIE";
return 0;
}