Program of Linear Search Technique in C Language
#include<stdio.h>
int main (){
int a[50], n, i, key, flag = 0;
printf("enter the no: of elements");
scanf ("%d",&n);
printf("enter the elements:");
for (i=0; i<n; i++)
scanf( "%d", &a[i]);
printf("enter a key element:");
scanf ("%d", &key);
for (i=0; i<n; i++){
if (a[i] == key){
flag = 1;
break;
if (flag == 1)
printf("search is successful:");
else
printf("search is unsuccessfull:");
return 0;
Output
enter the no: of elements5
enter the elements:12
45
13
67
78
enter a key element:67
search is successful:
Program of Binary Search Technique in C Language (Iterative)
#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
while (start_index <= end_index){
int middle = start_index + (end_index- start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] < element)
start_index = middle + 1;
else
end_index = middle - 1;
return -1;
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 16;
int found_index = iterativeBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
else {
printf("Element found at index : %d",found_index);
return 0;
Output
Element found at index : 4
Program of Binary Search Technique in C Language (Recursively)
#include <stdio.h>
int recursiveBinarySearch(int array[], int start_index, int end_index, int element){
if (end_index >= start_index){
int middle = start_index + (end_index - start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] > element)
return recursiveBinarySearch(array, start_index, middle-1, element);
return recursiveBinarySearch(array, middle+1, end_index, element);
return -1;
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 9;
int found_index = recursiveBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
else {
printf("Element found at index : %d",found_index);
return 0;
Output
Element found at index : 3
Program to see the execution time of a program in C Language
#include <stdio.h>
#include <time.h>
void take_enter() {
printf("Press enter to stop the counter");
while(1) {
if (getchar())
break;
main() {
// Calculate the time taken by take_enter()
clock_t t;
t = clock();
printf("Timer starts");
take_enter();
printf("Timer ends");
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
printf("The program took %f seconds to execute", time_taken);
Output
Timer startsPress enter to stop the counter
Timer endsThe program took 0.000164 seconds to execute
C program for bubble sorting technique
#include<stdio.h>
int main(){
int a[50], i,j,n,t;
printf("enter the No: of elements in the list:");
scanf("%d", &n);
printf("enter the elements:");
for(i=0; i<n; i++){
scanf ("%d", &a[i]);
printf("Before bubble sorting the elements are:\n");
for(i=0; i<n; i++)
printf("%d \t", a[i]);
printf("\n");
for (i=0; i<n-1; i++){
for (j=i+1; j<n; j++){
if (a[i] > a[j]){
t = a[i];
a[i] = a[j];
a[j] = t;
printf ("after bubble sorting the elements are:\n");
for (i=0; i<n; i++)
printf("%d\t", a[i]);
return 0;
Output
enter the No: of elements in the list:5
enter the elements:30
40
20
10
50
Before bubble sorting the elements are:
30 40 20 10 50
after bubble sorting the elements are:
10 20 30 40 50
C program for selection sorting technique
#include <stdio.h>
// function to swap the the position of two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
void selectionSort(int array[], int size) {
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
// To sort in descending order, change > to < in this line.
// Select the minimum element in each loop.
if (array[i] < array[min_idx])
min_idx = i;
// put min at the correct position
swap(&array[min_idx], &array[step]);
// function to print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
// driver code
int main() {
int data[] = {20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
printf("Unsorted array:\n");
for (int ctr=0; ctr < size; ++ctr)
printf("%d ", data[ctr]);
selectionSort(data, size);
printf("\nSorted array in Ascending Order:\n");
printArray(data, size);
Output
Unsorted array:
20 12 10 15 2
Sorted array in Ascending Order:
2 10 12 15 20
C program for insertion sorting technique
#include <stdio.h>
// Function to print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
printf("\n");
void insertionSort(int array[], int size) {
for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;
// Compare key with each element on the left of it until an element smaller than
// it is found.
// For descending order, change key<array[j] to key>array[j].
while (key < array[j] && j >= 0) {
array[j + 1] = array[j];
--j;
array[j + 1] = key;
// Driver code
int main() {
int data[] = {9, 5, 1, 4, 3};
int size = sizeof(data) / sizeof(data[0]);
printf("Unsorted array :\n");
for(int ctr=0; ctr < size; ++ctr)
printf("%d ", data[ctr]);
printf("\n");
insertionSort(data, size);
printf("Sorted array in ascending order:\n");
printArray(data, size);
Output
Unsorted array :
9 5 1 4 3
Sorted array in ascending order:
1 3 4 5 9
C program for quick sorting technique
#include <stdio.h>
// function to swap elements
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
// function to find the partition position
int partition(int array[], int low, int high) {
// select the rightmost element as pivot
int pivot = array[high];
// pointer for greater element
int i = (low - 1);
// traverse each element of the array
// compare them with the pivot
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
// if element smaller than pivot is found
// swap it with the greater element pointed by i
i++;
// swap element at i with element at j
swap(&array[i], &array[j]);
// swap the pivot element with the greater element at i
swap(&array[i + 1], &array[high]);
// return the partition point
return (i + 1);
}
void quickSort(int array[], int low, int high) {
if (low < high) {
// find the pivot element such that
// elements smaller than pivot are on left of pivot
// elements greater than pivot are on right of pivot
int pi = partition(array, low, high);
// recursive call on the left of pivot
quickSort(array, low, pi - 1);
// recursive call on the right of pivot
quickSort(array, pi + 1, high);
// function to print array elements
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
printf("\n");
// main function
int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
int n = sizeof(data) / sizeof(data[0]);
printf("Unsorted Array\n");
printArray(data, n);
// perform quicksort on data
quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n");
printArray(data, n);
}
Output
Unsorted Array
8 7 2 1 0 9 6
Sorted array in ascending order:
0 1 2 6 7 8 9
C program for merge sorting technique
#include <stdio.h>
// Merge two subarrays L and M into arr
void merge(int arr[], int p, int q, int r) {
// Create L ← A[p..q] and M ← A[q+1..r]
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], M[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];
// Maintain current index of sub-arrays and main array
int i, j, k;
i = 0;
j = 0;
k = p;
// Until we reach either end of either L or M, pick larger among
// elements L and M and place them in the correct position at A[p..r]
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = M[j];
j++;
k++;
// When we run out of elements in either L or M,
// pick up the remaining elements and put in A[p..r]
while (i < n1) {
arr[k] = L[i];
i++;
k++;
while (j < n2) {
arr[k] = M[j];
j++;
k++;
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
// m is the point where the array is divided into two subarrays
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Merge the sorted subarrays
merge(arr, l, m, r);
// Print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
// Driver program
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, size - 1);
printf("Unsorted array:\n");
for(int ctr=0; ctr < size; ++ctr)
printf("%d ", arr[ctr]);
printf("\n");
printf("Sorted array: \n");
printArray(arr, size);
Output
Unsorted array:
1 5 6 9 10 12
Sorted array:
1 5 6 9 10 12