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

What's hot (20)

programs
programsprograms
programs
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligence
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Ada file
Ada fileAda file
Ada file
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
week-21x
week-21xweek-21x
week-21x
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Data structure output 1
Data structure output 1Data structure output 1
Data structure output 1
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
Basic program in java
Basic program in java Basic program in java
Basic program in java
 
Cquestions
Cquestions Cquestions
Cquestions
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 

Viewers also liked

How to use Social Media for Educational Organizations?
How to use Social Media for Educational Organizations?How to use Social Media for Educational Organizations?
How to use Social Media for Educational Organizations?Deva Singh
 
ds and algorithm session
ds and algorithm sessionds and algorithm session
ds and algorithm sessionVarun Garg
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartRabin BK
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1luhkahreth
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and FlowchartsDeva Singh
 

Viewers also liked (8)

Excel07 l1 ch1
Excel07 l1 ch1Excel07 l1 ch1
Excel07 l1 ch1
 
How to use Social Media for Educational Organizations?
How to use Social Media for Educational Organizations?How to use Social Media for Educational Organizations?
How to use Social Media for Educational Organizations?
 
ds and algorithm session
ds and algorithm sessionds and algorithm session
ds and algorithm session
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 

Similar to Sorting programs

Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)Ankit Gupta
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdfanujmkt
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207Syed Tanveer
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C CodeSyed Ahmed Zaki
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ Eli Diaz
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ Eli Diaz
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptxSanthiya S
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfapexelectronices01
 

Similar to Sorting programs (20)

C programs
C programsC programs
C programs
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Pnno
PnnoPnno
Pnno
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
week-20x
week-20xweek-20x
week-20x
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
Vcs16
Vcs16Vcs16
Vcs16
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++
 
Vcs29
Vcs29Vcs29
Vcs29
 
array.ppt
array.pptarray.ppt
array.ppt
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
 
Lab Question
Lab QuestionLab Question
Lab Question
 

Recently uploaded

Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Serviceritikaroy0888
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdfRenandantas16
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...lizamodels9
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechNewman George Leech
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis UsageNeil Kimberley
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service JamshedpurVIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service JamshedpurSuhani Kapoor
 
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsCash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsApsara Of India
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...lizamodels9
 
GD Birla and his contribution in management
GD Birla and his contribution in managementGD Birla and his contribution in management
GD Birla and his contribution in managementchhavia330
 
rishikeshgirls.in- Rishikesh call girl.pdf
rishikeshgirls.in- Rishikesh call girl.pdfrishikeshgirls.in- Rishikesh call girl.pdf
rishikeshgirls.in- Rishikesh call girl.pdfmuskan1121w
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageMatteo Carbone
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communicationskarancommunications
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfpollardmorgan
 

Recently uploaded (20)

Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Service
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman Leech
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service JamshedpurVIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
 
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsCash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
 
GD Birla and his contribution in management
GD Birla and his contribution in managementGD Birla and his contribution in management
GD Birla and his contribution in management
 
rishikeshgirls.in- Rishikesh call girl.pdf
rishikeshgirls.in- Rishikesh call girl.pdfrishikeshgirls.in- Rishikesh call girl.pdf
rishikeshgirls.in- Rishikesh call girl.pdf
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communications
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
Best Practices for Implementing an External Recruiting Partnership
Best Practices for Implementing an External Recruiting PartnershipBest Practices for Implementing an External Recruiting Partnership
Best Practices for Implementing an External Recruiting Partnership
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
 

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