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

Search and Sort Algorithms in C

The document contains C code implementations for various algorithms including Linear Search, Binary Search, Bubble Sort, Selection Sort, and Insertion Sort. Each algorithm prompts the user for input, processes the data accordingly, and outputs the results. The code examples demonstrate basic searching and sorting techniques in programming.
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)
10 views6 pages

Search and Sort Algorithms in C

The document contains C code implementations for various algorithms including Linear Search, Binary Search, Bubble Sort, Selection Sort, and Insertion Sort. Each algorithm prompts the user for input, processes the data accordingly, and outputs the results. The code examples demonstrate basic searching and sorting techniques in programming.
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 a[10], n, key, i, found = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

for(i = 0; i < n; i++)

scanf("%d", &a[i]);

printf("Enter key to search: ");

scanf("%d", &key);

for(i = 0; i < n; i++) {

if(a[i] == key) {

printf("Element found at index %d\n", i);

found = 1;

break;

if(found !=1)

printf("Element not found\n");

return 0;

BINARY SEARCH
#include <stdio.h>

int main() {

int a[20], n, key, low, high, mid;


printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter sorted elements:\n");

for(int i = 0; i < n; i++)

scanf("%d", &a[i]);

printf("Enter key to search: ");

scanf("%d", &key);

low = 0;

high = n - 1;

while(low <= high) {

mid = (low + high) / 2;

if(a[mid] == key) {

printf("Element found at index %d\n", mid);

return 0;

} else if(key < a[mid]) {

high = mid - 1;

} else {

low = mid + 1;

printf("Element not found\n");

return 0;

BUBBLE SORT
#include <stdio.h>

int main() {

int a[20], n, i, j, temp, swapped;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

for(i = 0; i < n; i++)

scanf("%d", &a[i]);

for(i = 0; i < n - 1; i++) {

swapped = 0;

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;

swapped = 1;

if(swapped == 0)

break;

printf("Sorted array:\n");

for(i = 0; i < n; i++)


printf("%d ", a[i]);

return 0;

SELECTION SORT
#include <stdio.h>

int main() {

int a[20], n, i, j, min, temp;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

for(i = 0; i < n; i++)

scanf("%d", &a[i]);

for(i = 0; i < n - 1; i++) {

min = i;

for(j = i + 1; j < n; j++) {

if(a[j] < a[min])

min = j;

if(min != i) {

temp = a[i];

a[i] = a[min];

a[min] = temp;

}
}

printf("Sorted array:\n");

for(i = 0; i < n; i++)

printf("%d ", a[i]);

return 0;

INSERTION SORT
#include <stdio.h>

int main() {

int a[20], n, i, j, key;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

for(i = 0; i < n; i++)

scanf("%d", &a[i]);

for(i = 1; i < n; i++) {

key = a[i];

j = i - 1;

while(j >= 0 && a[j] > key) {

a[j + 1] = a[j];

j--;
}

a[j + 1] = key;

printf("Sorted array:\n");

for(i = 0; i < n; i++)

printf("%d ", a[i]);

return 0;

You might also like