Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <stdlib.h>
- int cmp(const void *a, const void *b){
- int *x = (int*)a;
- int *y = (int*)b;
- return *x - *y;
- }
- int cmpBsearch(const void *a, const void *b){
- int *x = (int*)a;
- int *y = (int*)b;
- if(*x>*y){
- return 1;
- }
- else if(*x<*y){
- return -1;
- }
- else{
- return 0;
- }
- }
- int main(){
- int niz[] = {5, 4, 12 ,15, 3, 1, 7};
- int x=12;
- int n=7;
- int i;
- for(i=0; i<7; i++){
- printf("%d ",niz[i]);
- }
- printf("\n");
- qsort(niz, 7, sizeof(int), cmp);
- for(i=0; i<7; i++){
- printf("%d ",niz[i]);
- }
- int *y= bsearch(&x, niz, n, sizeof(int), &cmpBsearch);
- if(y==NULL){
- printf("Ne postoji element 12 u nizu\n");
- }
- else{
- printf("\n%d",y-niz );
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement