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

C Programs for Searching and Sorting

The document provides definitions and C programs for various searching and sorting algorithms, including Linear Search, Binary Search, Bubble Sort, Insertion Sort, and Selection Sort. Each algorithm is accompanied by a sample program that demonstrates its implementation. The document serves as a guide for understanding and coding these fundamental algorithms in C.
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 views8 pages

C Programs for Searching and Sorting

The document provides definitions and C programs for various searching and sorting algorithms, including Linear Search, Binary Search, Bubble Sort, Insertion Sort, and Selection Sort. Each algorithm is accompanied by a sample program that demonstrates its implementation. The document serves as a guide for understanding and coding these fundamental algorithms in C.
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

Searching and Sorting Programs in C

Linear Search
Definition:

Linear search checks each element one by one until the key element is found.

Program:

#include <stdio.h>

int main() {

int a[50], n, key, i, flag = 0;

printf("Enter size of array: ");

scanf("%d", &n);

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

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

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

printf("Enter element to search: ");

scanf("%d", &key);

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

if(a[i] == key) {

flag = 1;

break;
}

if(flag == 1)

printf("Element found at position %d\n", i+1);

else

printf("Element not found\n");

return 0;

Binary Search
Definition:

Binary search works on sorted arrays and divides the array into halves to find the key.

Program:

#include <stdio.h>

int main() {

int a[50], n, key, low, high, mid, i;

printf("Enter size of array: ");

scanf("%d", &n);

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

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


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

printf("Enter element 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 position %d\n", mid + 1);

return 0;

else if(key < a[mid])

high = mid - 1;

else

low = mid + 1;

printf("Element not found\n");

return 0;

}
Bubble Sort
Definition:

Bubble sort compares adjacent elements and swaps them if they are in the wrong order.

Program:

#include <stdio.h>

int main() {

int a[50], n, i, j, temp;

printf("Enter size of array: ");

scanf("%d", &n);

printf("Enter array elements:\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("Sorted array:\n");

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

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

return 0;

Insertion Sort
Definition:

Insertion sort inserts each element into its correct position in the sorted part of the array.

Program:

#include <stdio.h>

int main() {

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

printf("Enter size of array: ");

scanf("%d", &n);

printf("Enter array 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;

Selection Sort
Definition:

Selection sort selects the smallest element and places it at the correct position.

Program:

#include <stdio.h>

int main() {

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


printf("Enter size of array: ");

scanf("%d", &n);

printf("Enter array 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;

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;

You might also like