0% found this document useful (0 votes)
2 views

Sorting_and_Searching_Algorithms

Uploaded by

hemanshausali8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Sorting_and_Searching_Algorithms

Uploaded by

hemanshausali8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sorting and Searching Algorithms in C

1. Binary Search Algorithm


Binary search is an efficient algorithm for finding an item from a sorted list of items. It
works by repeatedly dividing in half the portion of the list that could contain the item, until
you've narrowed the possible locations to just one.

Working Procedure:

1. Start with a sorted list of items.


2. Find the middle point of the list.
3. Compare the middle element with the target element.
4. If the target element is equal to the middle element, return the index of the middle
element.
5. If the target element is less than the middle element, repeat the process with the left half
of the list.
6. If the target element is greater than the middle element, repeat the process with the right
half of the list.

Implementation in C:

#include <stdio.h>

int binarySearch(int arr[], int n, int target) {


int left = 0;
int right = n - 1;

while (left <= right) {


int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid;
}

if (arr[mid] < target) {


left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}

int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 23;

int result = binarySearch(arr, n, target);

if (result == -1) {
printf("Element not found in the array");
} else {
printf("Element found at index %d", result);
}

return 0;
}

Output: Element found at index 5

2. Bubble Sort Algorithm


Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order.

Working Procedure:

1. Start at the beginning of the list.


2. Compare the first two elements.
3. If the first element is greater than the second element, swap them.
4. Move to the next pair of elements and repeat steps 2-3.
5. Continue this process until the end of the list is reached.
6. Repeat the process until no more swaps are needed.

Implementation in C:

#include <stdio.h>

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);

return 0;
}

Output: Sorted array: 11 12 22 25 34 64 90

You might also like