0% found this document useful (0 votes)
5 views6 pages

Search and Sort Algorithms in C

The document contains C code implementations for various search and sorting algorithms including Linear Search, Binary Search, Insertion Sort, Selection Sort, Quick Sort, and Merge Sort. Each algorithm prompts the user to input an array of integers and performs the respective operation, displaying the results. The code is structured with clear input and output statements for user interaction.

Uploaded by

prachutanreddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Search and Sort Algorithms in C

The document contains C code implementations for various search and sorting algorithms including Linear Search, Binary Search, Insertion Sort, Selection Sort, Quick Sort, and Merge Sort. Each algorithm prompts the user to input an array of integers and performs the respective operation, displaying the results. The code is structured with clear input and output statements for user interaction.

Uploaded by

prachutanreddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Linear search

#include <stdio.h>
int main() {
int arr[100], n, key, i, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
for(i = 0; i < n; i++) {
if(arr[i] == key) {
printf("Element found at position %d\n", i + 1);
found = 1;
break;
}
}

if(!found) {
printf("Element not found in the array.\n");
}

return 0;
}

Binary search
#include <stdio.h>
int main() {
int arr[100], n, key, i, low, high, mid, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements in sorted order:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
low = 0;
high = n - 1;
while(low <= high) {
mid = (low + high) / 2;

if(arr[mid] == key) {
printf("Element found at position %d\n", mid + 1);
found = 1;
break;
}
else if(arr[mid] < key) {
low = mid + 1; // search in right half
}
else {
high = mid - 1; // search in left half
}
}

if(!found) {
printf("Element not found in the array.\n");
}

return 0;
}

Insertion sort
#include <stdio.h>
int main() {
int arr[100], n, i, j, key;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}

arr[j + 1] = key;
}
printf("Sorted array (Insertion Sort):\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Selection sort
#include <stdio.h>
int main() {
int arr[100], n, i, j, min, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n - 1; i++) {
min = i;
for(j = i + 1; j < n; j++) {
if(arr[j] < arr[min]) {
min = j;
}
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
printf("Sorted array (Selection Sort):\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
Quick sort
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // choosing last element as pivot
int i = low - 1;
for(int j = low; j < high; j++) {
if(arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if(low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);

quickSort(arr, 0, n - 1);

printf("Sorted array (Quick Sort):\n");


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

Merge sort
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for(int i = 0; i < n1; i++)
L[i] = arr[left + i];
for(int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;
while(i < n1 && j < n2) {
if(L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}

while(i < n1)


arr[k++] = L[i++];
while(j < n2)
arr[k++] = R[j++];
}

void mergeSort(int arr[], int left, int right) {


if(left < right) {
int mid = (left + right) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);

mergeSort(arr, 0, n - 1);

printf("Sorted array (Merge Sort):\n");


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

return 0;
}

You might also like