SlideShare a Scribd company logo
//...........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

More Related Content

DOCX
Solutionsfor co2 C Programs for data structures
PDF
design and analysis of algorithm Lab files
DOCX
DOCX
DAA Lab File C Programs
PDF
Data Structures Practical File
DOCX
DOCX
Data Structures Using C Practical File
DOC
Daa practicals
Solutionsfor co2 C Programs for data structures
design and analysis of algorithm Lab files
DAA Lab File C Programs
Data Structures Practical File
Data Structures Using C Practical File
Daa practicals

What's hot (20)

PDF
programs
DOCX
DataStructures notes
PPSX
C programming array & shorting
DOCX
Artificial intelligence
PPTX
Double linked list
PPTX
Double linked list
DOCX
Os lab file c programs
DOC
Ada file
PDF
C Prog - Array
PPTX
Single linked list
PPTX
Circular linked list
DOCX
ADA FILE
DOCX
PDF
Data structures lab manual
DOCX
Data structure output 1
DOCX
Numerical Method Assignment
PDF
Basic program in java
PPT
Cquestions
DOC
Basic c programs updated on 31.8.2020
programs
DataStructures notes
C programming array & shorting
Artificial intelligence
Double linked list
Double linked list
Os lab file c programs
Ada file
C Prog - Array
Single linked list
Circular linked list
ADA FILE
Data structures lab manual
Data structure output 1
Numerical Method Assignment
Basic program in java
Cquestions
Basic c programs updated on 31.8.2020
Ad

Viewers also liked (8)

PPT
Excel07 l1 ch1
PPTX
How to use Social Media for Educational Organizations?
PPS
ds and algorithm session
PPTX
Algorithm and flowchart
PPT
Lect 1. introduction to programming languages
PPT
Algorithmsandflowcharts1
PPTX
Algorithms and Flowcharts
PPTX
Algorithm and flowchart
Excel07 l1 ch1
How to use Social Media for Educational Organizations?
ds and algorithm session
Algorithm and flowchart
Lect 1. introduction to programming languages
Algorithmsandflowcharts1
Algorithms and Flowcharts
Algorithm and flowchart
Ad

Similar to Sorting programs (20)

DOC
Ds program-print
PPTX
LectureSlidData_sturcture_algorithm_v2.pptx
DOCX
DAA Lab Work.docx
PDF
Sorting Algorithms and their implementations
PPT
Unit6 C
PDF
DSA.pdf
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
PPT
search_sort Search sortSearch sortSearch sortSearch sort
PPT
C Language Unit-6
PDF
C programs
PPTX
PPT
Unit6 jwfiles
DOC
Daapracticals 111105084852-phpapp02
DOCX
Write a complete C programme to implement the following sorting algor.docx
PDF
C++ Searching & Sorting5. Sort the following list using the select.pdf
PPTX
PPT.pptx Searching and Sorting Techniques
DOCX
Write a C program that implements a simple array-based insertion sort-.docx
Ds program-print
LectureSlidData_sturcture_algorithm_v2.pptx
DAA Lab Work.docx
Sorting Algorithms and their implementations
Unit6 C
DSA.pdf
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
search_sort Search sortSearch sortSearch sortSearch sort
C Language Unit-6
C programs
Unit6 jwfiles
Daapracticals 111105084852-phpapp02
Write a complete C programme to implement the following sorting algor.docx
C++ Searching & Sorting5. Sort the following list using the select.pdf
PPT.pptx Searching and Sorting Techniques
Write a C program that implements a simple array-based insertion sort-.docx

Recently uploaded (20)

PPTX
Amazon (Business Studies) management studies
DOCX
unit 1 COST ACCOUNTING AND COST SHEET
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
PDF
MSPs in 10 Words - Created by US MSP Network
PDF
Reconciliation AND MEMORANDUM RECONCILATION
PDF
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
PDF
Dr. Enrique Segura Ense Group - A Self-Made Entrepreneur And Executive
PPT
340036916-American-Literature-Literary-Period-Overview.ppt
PDF
Business model innovation report 2022.pdf
PPTX
job Avenue by vinith.pptxvnbvnvnvbnvbnbmnbmbh
PDF
Katrina Stoneking: Shaking Up the Alcohol Beverage Industry
PDF
Roadmap Map-digital Banking feature MB,IB,AB
PDF
Power and position in leadershipDOC-20250808-WA0011..pdf
PPTX
Lecture (1)-Introduction.pptx business communication
DOCX
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
PPTX
5 Stages of group development guide.pptx
PDF
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
PDF
Deliverable file - Regulatory guideline analysis.pdf
PPTX
Business Ethics - An introduction and its overview.pptx
PDF
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
Amazon (Business Studies) management studies
unit 1 COST ACCOUNTING AND COST SHEET
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
MSPs in 10 Words - Created by US MSP Network
Reconciliation AND MEMORANDUM RECONCILATION
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
Dr. Enrique Segura Ense Group - A Self-Made Entrepreneur And Executive
340036916-American-Literature-Literary-Period-Overview.ppt
Business model innovation report 2022.pdf
job Avenue by vinith.pptxvnbvnvnvbnvbnbmnbmbh
Katrina Stoneking: Shaking Up the Alcohol Beverage Industry
Roadmap Map-digital Banking feature MB,IB,AB
Power and position in leadershipDOC-20250808-WA0011..pdf
Lecture (1)-Introduction.pptx business communication
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
5 Stages of group development guide.pptx
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
Deliverable file - Regulatory guideline analysis.pdf
Business Ethics - An introduction and its overview.pptx
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...

Sorting programs

  • 1. //...........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;
  • 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; 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. 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
  • 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
  • 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 number of 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. 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