105 lines
2.7 KiB
C
105 lines
2.7 KiB
C
|
#include "lib/lib.h"
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <time.h>
|
||
|
|
||
|
void swap(int *a, int *b) {
|
||
|
int t = *a;
|
||
|
*a = *b;
|
||
|
*b = t;
|
||
|
}
|
||
|
|
||
|
int partition(int arr[], int low, int high) {
|
||
|
int pivot = arr[high];
|
||
|
int i = (low - 1);
|
||
|
|
||
|
for (int j = low; j <= high - 1; j++) {
|
||
|
if (arr[j] < pivot) {
|
||
|
i++;
|
||
|
swap(&arr[i], &arr[j]);
|
||
|
}
|
||
|
}
|
||
|
swap(&arr[i + 1], &arr[high]);
|
||
|
return (i + 1);
|
||
|
}
|
||
|
|
||
|
void q_sort(int arr[], int low, int high) {
|
||
|
if (low < high) {
|
||
|
int p = partition(arr, low, high);
|
||
|
q_sort(arr, low, p - 1);
|
||
|
q_sort(arr, p + 1, high);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int bsearch_iterative(int arr[], int key, int len) {
|
||
|
int low = 0;
|
||
|
int high = len;
|
||
|
while (low < high) {
|
||
|
int mid = (low + high) / 2;
|
||
|
if (key == arr[mid]) {
|
||
|
return mid;
|
||
|
} else if (key > arr[mid]) {
|
||
|
low = mid + 1;
|
||
|
} else {
|
||
|
high = mid - 1;
|
||
|
}
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int bsearch_recursive(int arr[], int key, int low, int high) {
|
||
|
if (low > high) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int mid = (low + high) / 2;
|
||
|
if (key == arr[mid]) {
|
||
|
return mid;
|
||
|
} else if (key > arr[mid]) {
|
||
|
return bsearch_recursive(arr, key, mid + 1, high);
|
||
|
} else {
|
||
|
return bsearch_recursive(arr, key, low, mid - 1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
print_header();
|
||
|
printf("Enter 5 elements:\n");
|
||
|
int arr_len = 5;
|
||
|
int arr[arr_len];
|
||
|
for (int i = 0; i < arr_len; i++) {
|
||
|
scanf("%d", &arr[i]);
|
||
|
}
|
||
|
printf("\nEnter element to search for:\n");
|
||
|
int search_element;
|
||
|
scanf("%d", &search_element);
|
||
|
q_sort(arr, 0, arr_len);
|
||
|
|
||
|
clock_t tic, toc;
|
||
|
printf("\n\n--------\n=> Binary Search (Iterative Approach)\n");
|
||
|
tic = clock();
|
||
|
int b_iter_index = bsearch_iterative(arr, search_element, arr_len);
|
||
|
toc = clock();
|
||
|
if (b_iter_index == -1) {
|
||
|
printf("Unable to find the element '%d'!\n", search_element);
|
||
|
} else {
|
||
|
printf("Element found at index '%d', position '%d'\n", b_iter_index, b_iter_index + 1);
|
||
|
}
|
||
|
printf("Total time taken by CPU (End Time - Start Time)/clock per sec: %f\n--------\n",
|
||
|
(double)(toc - tic) / CLOCKS_PER_SEC);
|
||
|
|
||
|
printf("\n--------\n=> Binary Search (Recursive Approach)\n");
|
||
|
tic = clock();
|
||
|
int b_rec_index = bsearch_recursive(arr, search_element, 0, arr_len);
|
||
|
toc = clock();
|
||
|
if (b_iter_index == -1) {
|
||
|
printf("Unable to find the element '%d'!\n", search_element);
|
||
|
} else {
|
||
|
printf("Element found at index '%d', position '%d'\n", b_rec_index, b_rec_index + 1);
|
||
|
}
|
||
|
printf("Total time taken by CPU (End Time - Start Time)/clock per sec: %f\n--------\n",
|
||
|
(double)(toc - tic) / CLOCKS_PER_SEC);
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|