Created
January 31, 2015 19:51
-
-
Save eroltutumlu/0bcbf33041af94ef1a62 to your computer and use it in GitHub Desktop.
Binary Search Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #define SIZE 10 | |
| #define SORTED 1 | |
| #define UNSORTED 0 | |
| int main(void) { | |
| int i,j; | |
| int Arr[SIZE]={10,2,6,9,-8,3,2,4,5,8}; | |
| int cArr[SIZE]; | |
| for(i=0;i<SIZE;++i) | |
| cArr[i]=Arr[i]; | |
| int flag=0; | |
| int value; | |
| int mid; | |
| int low=0; | |
| int high=SIZE-1; | |
| do | |
| { | |
| flag=SORTED; | |
| for(i=0;i<SIZE-1;++i) | |
| { | |
| if(Arr[i]>Arr[i+1]) | |
| { | |
| int temp=Arr[i]; | |
| Arr[i]=Arr[i+1]; | |
| Arr[i+1]=temp; | |
| flag=UNSORTED; | |
| } | |
| } | |
| } | |
| while(flag==UNSORTED); | |
| for(i=0;i<SIZE;i++) | |
| printf("%d ",Arr[i]); | |
| // BINARY SEARCH ALGORITHM | |
| printf("\nAradiginiz eleman: "); | |
| scanf("%d",&value); | |
| while(low<=high) | |
| { | |
| mid=(low+high)/2; | |
| if(Arr[mid]==value) | |
| break; | |
| if(Arr[mid]>value) | |
| high=mid-1; | |
| else | |
| low=mid+1; | |
| } | |
| if(low>high) | |
| printf("Bulunamadi."); | |
| else | |
| printf("Array[%d]=%d",mid+1,value); | |
| printf("\n\nSiralanmamis dizinin ters cevrilmis hali: \n"); | |
| for(i=0;i<SIZE/2;++i) | |
| { | |
| int temp=cArr[i]; | |
| cArr[i]=cArr[SIZE-1-i]; | |
| cArr[SIZE-1-i]=temp; | |
| } | |
| for(i=0;i<SIZE;++i) | |
| printf("%d ",cArr[i]); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment