/* LightCollege.ir Learc C Programing*/
#include<stdio.h>
#include<stdlib.h>

#define SZ 5000

void ShowArray(int arr[],int size);
void BubbleSort(int arr[],int size);
int binarySearch(int arr[],int min,int max,int num);

void main(){
	int n[SZ];
	int i = 0;
	for(i = 0;i<SZ;i++){
		n[i]=rand()%1000;
	}
	printf("Before Sort : \n");
	ShowArray(n,SZ);
	printf("After Sort : \n");
	BubbleSort(n,SZ);
	ShowArray(n,SZ);
	
	const int num = 998;
	/*
	for(i = 0;i<SZ;i++){
		if(n[i] == num)printf("Index : %d\n",i);
		//break;
	}
	time : 1.099
	*/
	int rslt = binarySearch(n,0,SZ,num);
	printf("Rslt : %d",rslt);
}

int binarySearch(int arr[],int min,int max,int num){
	if(min > max) return -1;
	int middle = (min + max)/2;
	if(arr[middle] == num) return middle;
	if(arr[middle] > num) return binarySearch(arr,min,middle -1,num);
	else return binarySearch(arr,middle +1, max,num);
}


void ShowArray(int arr[],int size){
	int i;
	for(i = 0; i<size;i++){
		printf("%7d",arr[i]);
		if((i+1)%10 == 0)printf("\n");
	}
	printf("\n");
}

void BubbleSort(int arr[],int size){
	int i,j;
	for(i=0;i<size;i++){
		// sort
		for(j=0;j<size-1-i;j++){
			if(arr[j] > arr[j+1]){
				int hold = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = hold;
			}
		}
	}
}
/*power by LightCompany.ir*/
