//...........HEAPSORT............................

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 5
#define MAX 15
void 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;
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 elements
5
Enter 5 elements one by one
20
10
40
50
61

The sorted list is
10
20
40
50
61
//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();
}
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 elements
8
Elements are
67
45
33
11
12
43
56
78
Sorted list
     11
     12
     33
     43
     45
     56
     67
     78
//     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 : 5
Enter the elements of array :
2
4
3
5
1
Enter element after sorting:
1
2
3
4
5
//     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 data
12
45
67
34
52
*******Sorted Array*******

12
34
45
52
67
//     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;
}
output:->
Enter the number of elements:
5
{Allocating memory}
Reading the data:
2
5
4
3
1
*******Sorted Array*******
5
4
3
2
1
//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");
for(i=0;i<n;i++)
printf("n%d",*(x+i));
free(x);
getch();
}




output:



Enter elements:5
Allocate memory.Reading the data.12
43
11
56
76

2
1
1
1
Elements after sorting
11
12
43
56
76
Sorting programs
Sorting programs

Sorting programs

  • 1.
    //...........HEAPSORT............................ #include<stdio.h> #include<conio.h> #define MAXSIZE 5 #defineMAX 15 void 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.
    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 elements 5 Enter 5 elements one by one 20 10 40 50 61 The sorted list is 10 20 40 50 61
  • 3.
    //quick sort…………………………… #include<stdio.h> #include<conio.h> int lower[20],upper[20],top=-1; inta[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.
    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; gotoleft1; } 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 elements 8 Elements are 67 45 33 11 12 43 56
  • 5.
    78 Sorted list 11 12 33 43 45 56 67 78
  • 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 : 5 Enter the elements of array : 2 4 3 5 1 Enter element after sorting: 1 2 3 4 5
  • 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 data 12 45 67 34
  • 8.
  • 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.
    output:-> Enter the numberof elements: 5 {Allocating memory} Reading the data: 2 5 4 3 1 *******Sorted Array******* 5 4 3 2 1
  • 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.