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 mainQuick 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 itemelements 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
0 Comments