Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Searching & Sorting Algorithm c programming codes |

Searching & Sorting Algorithm c programming codes |

Searching & Sorting Algorithm

 Bubble sort


#include <stdio.h>

int main()
{
	int a[10],n,i,pass,temp,ccount=0,scount=0;
	
	printf("\nHow many elements you want ? (upto 10) \n");
	scanf("%d",&n);
	printf("\nEnter %d elements :\n",n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	
	//Bubble sort
	for(pass=1;pass<n;pass++)
	{
		for(i=0;i<n-pass;i++)
		{
			ccount++;
			if(a[i] >a[i+1])
			{
				scount++;
				temp=a[i];
				a[i]=a[i+1];
				a[i+1]=temp;
			}
		}
	}

	printf("\nElements after Bubble sort are : \n");
	for(i=0;i<n;i++)
		printf("%d\t",a[i]);
	printf("\nTotal number of comparisions = %d",ccount);
	printf("\nTotal number of swaps = %d",scount);
	return -1;
}//main



insertion Sort


#include <stdio.h>


void main()
{
	int a[10],i,j,n,temp;
	
	printf("How many elements you want ?");
	scanf("%d",&n);
	printf("\nEnter the %d elements :",n);
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);

	//insertion sort
	for(i=1;i<n;i++)
	{
		temp=a[i];
                 j=i-1;
		while(j>=0 && a[j]>temp)
		{	a[j+1]=a[j];
	        	j--;
                }
		a[j+1]=temp;
	
          }
	printf("\nSorted Elements after insertion sort :\n");
	for(i=0;i<n;i++)
	  printf("%d\t",a[i]);
          

}

Selection Sort

#include <stdio.h>
 
void main()
{
	int a[10],i,j,n,temp,min;
	
	printf("How many elements you want ?");
	scanf("%d",&n);
	printf("\nEnter the %d elements :",n);
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
    // selection sort 
    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; 
         }
     }//end of for 
           for(i=0;i<n;i++)
		printf("%d\t",a[i]);
}//end of main

Quick Sort


#include <stdio.h>


void Quicksort(int a[], int lb, int ub)
{
	int loc;
	if(lb < ub)
	{
		loc=partition(a,lb,ub);
		Quicksort(a,lb,loc-1);
		Quicksort(a,loc+1,ub);
	}
}

int partition(int a[],int lb,int ub)
{
	int up,dn,temp,pivot;
	up=ub;
	dn=lb+1;
	pivot=a[lb];
	do
	{
		while((a[dn] < pivot)&&(dn<=ub))
			dn++;
		while((a[up] > pivot)&&(up>lb))
			up--;
		if(dn < up)
		{
			temp=a[dn];
			a[dn]=a[up];
			a[up]=temp;
		}
	}while (dn <up);



	//Interchange pivot & up
	a[lb]=a[up];
	a[up]=pivot;
	return up;
}

void main()
{
	int a[10],i,n;
	
	printf("\n How many elements you want (upto 10) ? ");
	scanf("%d",&n);
	printf("\nEnter the %d unsorted elements : \n",n);
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	Quicksort(a,0,n-1);
	printf("\nSorted elements after QuickSort are : \n");
	for(i=0;i<n;i++)
	printf("%d\t",a[i]);
	
 }

Merge Sort



#include <stdio.h>
#include <conio.h>
void merge(int a[],int low,int mid, int high)
{
	int i,j,k,b[10];  //b is temp array
	i=low;
	j=mid+1;
	k=0;
	while((i <=mid)&&(j<=high))
	{
		if(a[i] <= a[j])
			b[k++]=a[i++];
		else
			b[k++]=a[j++];
	}
	while(i <= mid)
		b[k++]=a[i++];
	while(j <=high)
		b[k++]=a[j++];

	//copy merged elements from a

	for(j=low,k=0;j<=high;j++,k++)
		a[j]=b[k];
}


void Mergesort(int a[], int low, int high)
{
	int mid;
	if(low < high)
	{
		mid=(low+high)/2;
		Mergesort(a,low,mid);
		Mergesort(a,mid+1,high);
		merge(a,low,mid,high);
	}
}


void main()
{
	int a[10],i,n;
	clrscr();
	printf("\n How many elements you want (upto 10) ? ");
	scanf("%d",&n);
	printf("\nEnter the %d unsorted elements : \n",n);
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	Mergesort(a,0,n-1);
	printf("\nSorted elements after MergeSort are : \n");
	for(i=0;i<n;i++)
	printf("%d\t",a[i]);
	getch();
 }

Linear Search



#include<stdio.h>
#include<stdlib.h>

//Linear Search to search item in list of n items.

int lsearch(int list[],int n,int item);

void main()
{
  int list[20],item,n,pos,i;


  printf("How many elements :");
  scanf("%d",&n);

  printf("Enter elements:");
  for(i=0;i<n;i++)
    scanf("%d",&list[i]);

  printf("Enter item to search:");
  scanf("%d",&item);

  pos=lsearch(list,n,item);
  if(pos==-1)
   printf("Item is not found in list");
  else
   printf("Item is found at %d position 	",pos+1);
  
}


int lsearch(int list[],int n, int item)
{ //returns loc of item in list if found
 //return -1 if item is not found
  int loc;
	  for(loc=0;loc<n;loc++)
    if(list[loc]==item)
     return(loc);

  return(-1);
}

Sential Search


#include<stdio.h>
#include<stdlib.h>

//Sential Search to search item in list of n items.
int sensearch(int list[],int n, int item);

void main()
{
  int list[20],item,n,pos,i;


  printf("How many elements :");
  scanf("%d",&n);

  printf("Enter elements:");
  for(i=0;i<n;i++)
    scanf("%d",&list[i]);

  printf("Enter item to search:");
  scanf("%d",&item);

  pos=sensearch(list,n,item);
  if(pos==-1)
   printf("Item is not found in list");
  else
   printf("Item is found at %d position",pos+1);
   printf("\n");
  
}


int sensearch(int list[],int n, int item)
{ //returns loc of item in list if found
 //return -1 if item is not found
  int loc;
	list[n]=item;
         while(list[loc]!=item)
          {   
            loc++;
           }
         return(loc);
  return(-1);
}

Binary Search


//Binary Search to search item in list of n items.

#include <stdio.h>
int bsearch(int[],int,int);

int  main()
{
  int list[20],item,n,pos,i;

  
  printf("How many elements :");
  scanf("%d",&n);

  printf("Enter elements:");
  for(i=0;i<n;i++)
    scanf("%d",&list[i]);

  printf("Enter item to search:");
  scanf("%d",&item);

  pos=bsearch(list,n,item);
  if(pos==-1)
   printf("item is not found in list");
  else
   printf("item is found at %d",pos+1);
   printf("\n");
  return 0;
}


int bsearch(int list[],int n, int item)
{ //returns loc of item in list if found
 //return -1 if item is not found
  int mid,lb,ub;
  lb=0; ub=n-1;
  while(lb<=ub)
  {
   mid=(lb+ub)/2;
   if(list[mid]==item)
    return(mid);
   else
   if(item<list[mid])
    ub=mid-1;
   else
    lb=mid+1;
   }
  return(-1);
 int bsearch (int list[],int n, int item 







elements of Array


// program for Create ,Display,Insert & Delete the elements of Array.

#include<stdio.h>
#include<stdlib.h>



int main()
{
   int a[5],size=0,n,i,pos;

   printf("Enter the size of Array :");
   scanf("%d",&size);
   
   for(i=0;i<size;i++)
    {
      scanf("%d",&a[i]);
     }//end of for  
   printf("Elements of the Array :");
   for(i=0;i<size;i++)
    { 

      printf("%d ",a[i]);
    }// end of for

// insert the element

   printf("\n Enter the element you want to insert :");
   scanf("%d",&n);
   printf("Enter the position which you want to insert :");
   scanf("%d",&pos);
    size++;
   for(i=size-1;i>=pos;i--)
    a[i] =a[i-1];
  
   a[pos-1]=n;
    printf("Elements of the Array :");
   for(i=0;i<size;i++)
    { 

      printf("%d ",a[i]);
    }// end of for

// Delete the element from array
printf("\nEnter the element position for Deletetion:");
 scanf("%d",&pos);
    if(pos>=size+1)
     printf("\nDeletion is not possible");
    else
     {
       for(i=pos-1;i<size-1;i++)
       {
            a[i]=a[i+1];
          }

      size--;
        }// end of else
     for(i=0;i<size;i++)
    { 

      printf("%d ",a[i]);
    }// end of for
    printf("\n");
return 0;
}// end of main

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.