Skip to content

Instantly share code, notes, and snippets.

Created May 25, 2017 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ab3e1b67d6fdfb281c139bb2cd856c53 to your computer and use it in GitHub Desktop.
Save anonymous/ab3e1b67d6fdfb281c139bb2cd856c53 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <fstream>
#include <windows.h>
using namespace std;
const int TEN = 10000;
const int TWENTY = 20000;
const int THIRTY = 30000;
const int FORTY = 40000;
const int FIFTY = 50000;
const int SIXTY = 60000;
const int SEVENTY = 70000;
const int EIGHTY = 80000;
const int NINETY = 90000;
const int HUNDRED = 100000;
clock_t start,stop;
fstream plik;
void liniowo(int tab[], int n, int x) //tab- tablica, n - ilość elemantów, x - poszukiwana liczba
{
int stan = 0;
for(int i=0; i<n; i++)
{
if(tab[i]==x)
{
cout<<"Odnaleziono element na pozycji: "<<i<<endl;
stan++;
break;
}
}
if (stan==0)
{
cout<<"nie odnaleziono elementu"<<endl;
}
}
int binarnie(int *tab, int n, int x){
int l = 0,p = n - 1,s;
while (l <= p)
{
s = (l + p) / 2;
if (tab[s] == x) return s;
if (tab[s] < x)
l = s + 1;
else
p = s - 1;
}
}
int main()
{
int n=0;
cout<<"Podaj liczbe do odszukania: ";
cin>>n;
int one[TEN];
for(int i=0; i<n; i++) one[i]=i+1;
cout<<"linowo: "<<endl;
liniowo(one,TEN,n);
cout<<"Binarnie: "<<endl;
int dodatkowy=binarnie(one, TEN, n);
cout<<"Funkcja zwraca: "<<dodatkowy<<endl;
if (binarnie(one, TEN, n)==-1) cout<<"Nie odnaleziono elementu"<<endl;
else if (binarnie(one, TEN, n)!=-1) cout<<"Odnaleziono element na miejscu "<<binarnie(one, TEN, n)<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment