Searching and Sorting Codes
Searching and Sorting Codes
Searching and Sorting Codes
Binary search
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
2.Linear search
#include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
int arr[num];
scanf("%d", &arr[i]);
printf("\nEnter the key element that you would like to be searched: ");
scanf("%d", &key);
if (key == arr[i] )
element_found = 1;
break;
if (element_found == 1)
else
printf("we haven’t got element at any index in the array\n");
3.bubble sort:
#include <stdio.h>
int main(){
int a[50], n, i, j, temp;
printf("Please Enter the Number of Elements : ");
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++){
for(j = 0; j < n - i - 1; j++){
if(a[j] > a[j + 1]){
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("Array after implementing bubble sort: ");
for(i = 0; i < n; i++){
printf("%d ", a[i]);
}
return 0;
}