# Example of quick sort and binary search algorithm in C language

• 2020-06-01 10:46:38
• OfStack

The example of this article describes the C language 2 sorting and searching algorithm. I will share it with you for your reference as follows:

Title: first generate random number, then do quicksort, and then do 2 point search.

Implementation code:

``````
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void quiksort(int a[],int low,int high)
{
int i = low;
int j = high;
int temp = a[i];
if( low < high)
{
while(i < j)
{
while((a[j] >= temp) && (i < j))
{
j--;
}
a[i] = a[j];
while((a[i] <= temp) && (i < j))
{
i++;
}
a[j]= a[i];
}
a[i] = temp;
quiksort(a,low,i-1);
quiksort(a,j+1,high);
}
else
{
return;
}
}
int search(int x, int v[], int n){
int low, high, mid;
low = 0;
high = n - 1;
while ( low <= high ) {
mid = (low + high) / 2;
if(x < v[mid]){
high = mid - 1;
}
else if(x > v[mid]){
low = mid + 1;
}
else{
return mid;
}
}
return -1;
}
int main(){
int arry ;
int i,j;
srand((unsigned)time(NULL)); // Generate different random Numbers
for(i=0;i<100;i++)
{
j = rand()%100;
//if(i%2==0) // Generate negative
//j =-j;
arry[i]=j;
}
int location;
int number ;
quiksort(arry,0,99);// The random Numbers are sorted first
for(i=0;i<100;i++)
{
printf("%d ",arry[i]);
}
printf("\n");
printf(" Please enter the number you are looking for ");
scanf("%d",&number);
location = search(number, arry, 100);
printf("%d\n", location);
return 0;
}

``````

Experience and experience:

2 points search is easy to understand, that is, half search method, so the data must be an ordered sequence. Keep comparing the middle.

Quicksort is the application of recursion, first to determine a key data, and then put the larger one in the back, the smaller one in the front, and then do the same for both parts.