Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
114 views

C Program For Sorting and Searching

Binary search is faster than linear search for searching sorted lists, as it has O(log n) time complexity compared to linear search's O(n) time complexity. The C program examples shown implement binary search, linear search, and other sorting algorithms like bubble sort, selection sort, insertion sort, merge sort, and quick sort to sort arrays before searching. Various sorting algorithms are also presented with their C code implementations.

Uploaded by

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

C Program For Sorting and Searching

Binary search is faster than linear search for searching sorted lists, as it has O(log n) time complexity compared to linear search's O(n) time complexity. The C program examples shown implement binary search, linear search, and other sorting algorithms like bubble sort, selection sort, insertion sort, merge sort, and quick sort to sort arrays before searching. Various sorting algorithms are also presented with their C code implementations.

Uploaded by

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

| Binary search | Linear search | 

------------------------------------------------------------------------------------------------------------------------- 
1).Data must be in a sorted order | 1).Data any order 
2).Time complexity is O(log n) | 2).Time complexity is O(n). 
3).Only 1 "When" condition used | 3).Any no. of "When" condition used 
4).Only "=" relation operator is used | 4).any relation operator is used 
5).Access is faster | 5).Access is slow 
6).Only single dimensional array used | 6).single/multi dimensional array used 

C program for binary search


C program for binary search: This code implements binary search in c language. It can only
be used for sorted arrays, but it's fast as compared to linear search. If you wish to use binary
search on an array which is not sorted then you must sort it using some sorting technique
say merge sort and then use binary search algorithm to find the desired element in the list.
If the element to be searched is found then its position is printed.

The code below assumes that the input numbers are in ascending order.

C programming code for binary search


#include <stdio.h>
 
int main()
{
int c, first, last, middle, n, search, array[100];
 
printf("Enter number of elements\n");
scanf("%d",&n);
 
printf("Enter %d integers\n", n);
 
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
 
printf("Enter value to find\n");
scanf("%d",&search);
 
first = 0;
last = n - 1;
middle = (first+last)/2;
 
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
 
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
 
return 0;
}
C program for linear search

Download Binary search program.

Output of program:

Binary search is faster than linear search but list should be sorted, hashing is faster than binary
search and perform searches in constant time.

/* Program to Search an Element in the Array using Linear Search */

#include <stdio.h>

main()

int a[10], i, item;

printf("\nEnter elements of an array:\n");

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


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

printf("\nEnter item to search: ");

scanf("%d", &item);

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

if (item == a[i])

printf("\nItem found at location %d", i+1);

break;

if (i > 9)

printf("\nItem doesnot exist.");

getch();

***** Program to Sort an Array using Bubble Sort *****/

#include <stdio.h>

void bubble_sort();

int a[50], n;

main()

int i;
printf("\nEnter size of an array: ");

scanf("%d", &n);

printf("\nEnter elements of an array:\n");

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

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

bubble_sort();

printf("\n\nAfter sorting:\n");

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

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

getch();

void bubble_sort()

int j, k, temp;

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

for(k=0; k<(n-1)-j; k++)

if(a[k] > a[k+1])

temp = a[k];
a[k] = a[k+1];

a[k+1] = temp;

/***** Program to Sort an Array using Selection Sort *****/

#include <stdio.h>

void selection_sort();

int a[50], n;

main()

int i;

printf("\nEnter size of an array: ");

scanf("%d", &n);

printf("\nEnter elements of an array:\n");

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

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

selection_sort();

printf("\n\nAfter sorting:\n");

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


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

getch();

void selection_sort()

int i, j, min, temp;

for (i=0; i<n; 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;

}
BUBBLE SORT USING C PROGRAM

Source code of simple bubble sort implementation using


array ascending order in c programming language

#include<stdio.h>

int main(){

  int s,temp,i,j,a[20];

  printf("Enter total numbers of elements: ");

  scanf("%d",&s);

  printf("Enter %d elements: ",s);

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

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

  //Bubble sorting algorithm

  for(i=s-2;i>=0;i--){

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

           if(a[j]>a[j+1]){
               temp=a[j];

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

              a[j+1]=temp;

           }

      }

  }

  printf("After sorting: ");

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

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

  return 0;

Output:

Enter total numbers of elements: 5

Enter 5 elements: 6 2 0 11 9

After sorting:  0 2 6 9 11
C PROGRAM FOR INSERTION SORT

Source code of simple insertion sort implementation


using array in ascending order in c programming
language

#include<stdio.h>

int main(){

  int i,j,s,temp,a[20];

  printf("Enter total elements: ");

  scanf("%d",&s);

  printf("Enter %d elements: ",s);

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

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

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

      temp=a[i];

      j=i-1;
      while((temp<a[j])&&(j>=0)){

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

          j=j-1;

      }

      a[j+1]=temp;

  }

  printf("After sorting: ");

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

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

  return 0;

Output:

Enter total elements: 5

Enter 5 elements: 3 7 9 0 2

After sorting:  0 2 3 7 9

SELECTION SORT USING C PROGRAM


Source code of simple Selection sort implementation
using array ascending order in c programming language
#include<stdio.h>

int main(){

  int s,i,j,temp,a[20];

  printf("Enter total elements: ");

  scanf("%d",&s);

  printf("Enter %d elements: ",s);

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

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

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

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

           if(a[i]>a[j]){

               temp=a[i];

              a[i]=a[j];

              a[j]=temp;

           }

      }

  }

  printf("After sorting is: ");

  for(i=0;i<s;i++)
      printf(" %d",a[i]);

  return 0;

Output:

Enter total elements: 5

Enter 5 elements: 4 5 0 21 7

The array after sorting is:  0 4 5 7 21

QUICK SORT USING C PROGRAM

Source code of simple quick sort implementation using


array ascending order in c programming language

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){

  int x[20],size,i;
  printf("Enter size of the array: ");

  scanf("%d",&size);

  printf("Enter %d elements: ",size);

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

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

  quicksort(x,0,size-1);

  printf("Sorted elements: ");

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

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

  return 0;

void quicksort(int x[10],int first,int last){

    int pivot,j,temp,i;

     if(first<last){

         pivot=first;

         i=first;

         j=last;
         while(i<j){

             while(x[i]<=x[pivot]&&i<last)

                 i++;

             while(x[j]>x[pivot])

                 j--;

             if(i<j){

                 temp=x[i];

                  x[i]=x[j];

                  x[j]=temp;

             }

         }

         temp=x[pivot];

         x[pivot]=x[j];

         x[j]=temp;

         quicksort(x,first,j-1);

         quicksort(x,j+1,last);

    }

Output:

Enter size of the array: 5


Enter 5 elements: 3 8 0 1 2

Sorted elements: 0 1 2 3 8

Merge sort program in c

Source code of simple merge sort implementation using


array in ascending order in c programming language

#include<stdio.h>

#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);

void partition(int arr[],int low,int high);

int main(){

   

    int merge[MAX],i,n;

    printf("Enter the total number of elements: ");

    scanf("%d",&n);
    printf("Enter the elements which to be sort: ");

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

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

    }

    partition(merge,0,n-1);

    printf("After merge sorting elements are: ");

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

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

    }

   return 0;

void partition(int arr[],int low,int high){

    int mid;

    if(low<high){

         mid=(low+high)/2;

         partition(arr,low,mid);

         partition(arr,mid+1,high);

         mergeSort(arr,low,mid,high);
    }

void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];

    l=low;

    i=low;

    m=mid+1;

    while((l<=mid)&&(m<=high)){

         if(arr[l]<=arr[m]){

             temp[i]=arr[l];

             l++;

         }

         else{

             temp[i]=arr[m];

             m++;

         }

         i++;

    }
    if(l>mid){

         for(k=m;k<=high;k++){

             temp[i]=arr[k];

             i++;

         }

    }

    else{

         for(k=l;k<=mid;k++){

             temp[i]=arr[k];

             i++;

         }

    }

   

    for(k=low;k<=high;k++){

         arr[k]=temp[k];

    }

Sample output:

Enter the total number of elements: 5

Enter the elements which to be sort: 2 5 0 9 1


After merge sorting elements are: 0 1 2 5 9

SUBTRACTION OF TWO MATRICES USING C PROGRAM


#include<stdio.h>

int main(){

  int a[3][3],b[3][3],c[3][3],i,j;

  printf("Enter the First matrix->");

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

      for(j=0;j<3;j++)

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

  printf("\nEnter the Second matrix->");

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

      for(j=0;j<3;j++)

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

  printf("\nThe First matrix is\n");

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

      printf("\n");

      for(j=0;j<3;j++)

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

  }

  printf("\nThe Second matrix is\n");

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

      printf("\n");
      for(j=0;j<3;j++)

      printf("%d\t",b[i][j]);

   }

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

       for(j=0;j<3;j++)

            c[i][j]=a[i][j]-b[i][j];

   printf("\nThe Subtraction of two matrix is\n");

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

       printf("\n");

       for(j=0;j<3;j++)

            printf("%d\t",c[i][j]);

   }

   return 0;

Write a c program to find out transport of a matrix

C program for transpose of a matrix


C program to find transpose of given matrix
#include<stdio.h>

int main(){

  int a[10][10],b[10][10],i,j,k=0,m,n;

  printf("\nEnter the row and column of matrix");

  scanf("%d %d",&m,&n);

  printf("\nEnter the First matrix->");

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

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

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

  printf("\nThe matrix is\n");

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

      printf("\n");

      for(j=0;j<m;j++){

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

      }

  }

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

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

           b[i][j]=0;

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

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

           b[i][j]=a[j][i];

           printf("\n%d",b[i][j]);
      }

  }

  printf("\n\nTraspose of a matrix is -> ");

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

      printf("\n");

      for(j=0;j<m;j++){

           printf("%d\t",b[i][j]);

      }

  }

  return 0;

You might also like