Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sorting programs

1,424 views

Published on

Published in: Business, Technology
  • Be the first to comment

Sorting programs

  1. 1. //...........HEAPSORT............................#include<stdio.h>#include<conio.h>#define MAXSIZE 5#define MAX 15void main(){ int a[MAX],n,i,s,f,item,value; clrscr(); printf("Enter the number of elementsn"); scanf("%d",&n); printf("Enter %d elements one by onen",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=1;i<n;i++) { item=a[i]; s=i; f=(s-1)/2; while(s>0 && a[f]<item) { a[s]=a[f]; s=f; f=(s-1)/2; } a[s]=item; } for(i=n-1;i>0;i--) { value=a[i]; a[i]=a[0]; f=0; if(i==1) s=-1; else s=1; if(i>2 && a[2]>a[1]) s=2; while(s>=0 && value<a[s]) { a[f]=a[s]; f=s;
  2. 2. s=2*f+1; if(s+1<=i-1 &&(a[s]<a[s+1])) s=s+1; if(s>i-1) s=-1; } a[f]=value; } printf("nThe sorted list is n"); for(i=0;i<n;i++) { printf("%dn",a[i]); }getch();}output:->Enter the number of elements5Enter 5 elements one by one2010405061The sorted list is1020405061
  3. 3. //quick sort……………………………#include<stdio.h>#include<conio.h>int lower[20],upper[20],top=-1;int a[20],n,beg,end,loc;void quick();void main(){int i;clrscr();printf("How many elementsn");scanf("%d",&n);printf("Elements are n");for(i=0;i<n;i++)scanf("%d",&a[i]);if(n>1){top=top+1;lower[0]=0;upper[0]=n-1;}while(top!=-1){beg=lower[top];end=upper[top];top=top-1;quick();if(beg<loc-1){top=top+1;lower[top]=beg;upper[top]=loc-1;}if(loc+1<end){top=top+1;lower[top]=loc+1;upper[top]=end;}}printf("Sorted listn");for(i=0;i<n;i++)printf("t %dn",a[i]);getch();}
  4. 4. void quick(){int left,right,temp;right1:left=beg;right=end;loc=beg;while((a[loc]<=a[right])&&(loc!=right))right=right-1;if(loc==right)return;if(a[loc]>a[right]){temp=a[loc];a[loc]=a[right];a[right]=temp;loc=right;goto left1;}left1:while((a[left]<=a[loc])&&(left!=loc))left=left+1;if(loc==left)return;if(a[left]>a[loc]){temp=a[loc];a[loc]=a[left];a[left]=temp;loc=left;goto right1;}}output:How many elements8Elements are67453311124356
  5. 5. 78Sorted list 11 12 33 43 45 56 67 78
  6. 6. // INSERTION SORT....................#include<stdio.h>#include<conio.h>void main(){ int a[100],n,k,i,j,temp; printf("How many elements : "); scanf("%d",&n); printf("Enter the elements of array : n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } for(k=1;k<n;k++) { temp=a[k]; j=k-1; while((temp<a[i])&&(j>=0)) { a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } printf("Enter element after sorting:n"); for(i=0;i<n;i++) { printf("%dn",a[i]); }}output:->How many elements : 5Enter the elements of array :24351Enter element after sorting:12345
  7. 7. // BUBBLE SORT..........#include<stdio.h>#include<conio.h>#include<stdlib.h>int main(){ int *x,n,i,j,key,found,temp; clrscr(); printf("nt Enter no. of elements: "); scanf("%d",&n); printf("tAllocating Memory"); x=(int *)malloc(n*sizeof(int)); printf("tReading of data n"); for(i=1;i<=n;++i) scanf("%d",x+i); for(i=1;i<=n;++i) { for(j=1;j<=n-i;++j) { if(*(x+j)>*(x+j+1)) { temp=*(x+j); *(x+j)=*(x+j+1); *(x+j+1)=temp; } } } printf("*******Sorted Array*******n"); for(i=1;i<=n;++i) { printf("n%d",*(x+i)); } getch();}OUTPUT:-> Enter no. of elements: 5 Allocating Memory Reading of data12456734
  8. 8. 52*******Sorted Array*******1234455267
  9. 9. // SELECTION SORT..................#include<stdio.h>#include<conio.h>int *mi(int *a,int n){ int i, *p=a; for(i=1;i<n;i++) if(*p<*(a+i)) p=a+i; return(p);}void swap(int *a, int *b){ int t=*a; *a=*b; *b=t;}void selectionsort(int *a, int n){ int i; for(i=0;i<n-1;i++) { swap(a+i,mi(a+i,n-i)); }}int main(){ int *x,n,i; clrscr(); printf("Enter the number of elements: n"); scanf("%d",&n); printf("{Allocating memory} n"); x=(int *) malloc(n*sizeof(int)); printf("Reading the data: n"); for(i=0;i<n;i++) { scanf("%d",x+i); } selectionsort(x,n); printf("*******Sorted Array******* n"); for(i=0;i<n;i++) printf("%dn",*(x+i)); free(x); return 0;}
  10. 10. output:->Enter the number of elements:5{Allocating memory}Reading the data:25431*******Sorted Array*******54321
  11. 11. //MERGED SORT.......................#include<stdio.h>#include<conio.h>void merge(int *a,int n,int *b,int m){int i,j,k,*c;c=(int *)malloc((m+n)*sizeof(int));i=0;j=0;k=0;while(i<n&&j<m)c[k++]=(a[i]<b[j])?a[i++]:b[j++];while(i<n)c[k++]=a[i++];while(j<m)c[k++]=b[j++];for(i=0;i<k;i++)a[i]=c[i];free(c);}void mergesort(int *arr,int n){int mid;if((n==1))return;mid=n/2;printf("n%d",mid);mergesort(arr,mid);mergesort(arr+mid,n-mid);merge(arr,mid,arr+mid,n-mid);}void main(){int *x,i,n;clrscr();printf("Enter elements:");scanf("%d",&n);printf("Allocate memory.");x=(int*)malloc(n*sizeof(int));printf("Reading the data.");for(i=0;i<n;i++)scanf("%d",x+i);mergesort(x,n);printf("nElements after sorting");
  12. 12. for(i=0;i<n;i++)printf("n%d",*(x+i));free(x);getch();}output:Enter elements:5Allocate memory.Reading the data.12431156762111Elements after sorting1112435676

×