Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MAX 100
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- #include <iomanip>
- #include <time.h>
- #include <algorithm>
- using namespace std;
- int x;
- int y;
- int main()
- {
- srand(time(NULL));
- int tab[MAX];
- for (int i = 0; i < MAX; i++) //uzupełnianie tablicy losowymi wartościami
- {
- tab[i] = rand() % 1999;
- }
- cout <<"Posortowana tablica: " << endl << endl;
- sort(tab,tab+MAX); //sortowanie tablicy
- for (int i = 0;i < MAX;i++) {
- cout << tab[i] << " "; // wyświetlenie tablicy
- }
- cout << endl << endl;
- int stan;
- cout << "Wybierz rodzaj wyszukiwania (1 naturalne, 2 binarne) : " << endl;
- cin >> stan;
- if (stan==1){
- cout<< "podaj liczbe:"<<endl;
- cin>> x;
- bool znaleziono = false;
- int licznik = 0;
- for (int i = 0; i<MAX; i++)
- {
- licznik++;
- if ( x == tab[i]){
- cout << "Indeks elementu: " << i << endl;
- znaleziono = true;
- cout << "Ilosc iteracji: " << licznik << endl;
- break;
- }
- }
- if (znaleziono == false)
- {
- cout << "nie znaleziono elementu w tablicy" << endl;
- }
- }
- if (stan==2){
- int liczba, n = MAX, l, p, s,x=0;
- cout << "Zawartosc tablicy:\n";
- cout << endl;
- for (int i = 0; i < n; i++)
- cout << "tab [" << i << "] = " << tab[i] << endl;
- cout << "Podaj jaki element znalezc: ";
- cin >> liczba;
- l = 0;
- p = n-1;
- int licznik = 0;
- while (true)
- {
- licznik++;
- if (l > p)
- {
- cout << "Nie odnaleziono szukanego elementu" << endl;
- break;
- }
- s = (l+p)/2;
- if (tab[s] == liczba)
- {
- cout << "Odnaleziono liczbe " << liczba << " pod indeksem " << s << endl;
- break;
- }
- if (tab[s] < liczba)
- l = s+1;
- else
- p = s-1;
- }
- cout << "Ilosc iteracji: " << licznik << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement