Quick Sort in C Programming Language
Quick Sort in C Programming Language
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
Heap sort
#include<stdio.h>
#include<conio.h>
int main()
{
int TREE[10],N,i,j,K,p,c,temp;
printf("\n\n Enter no of elements..");
scanf("%d",&N);
printf("\n\n Enter the nos..");
for(i=1;i<=N;i++)
scanf("%d",&TREE[i]);
for(i=2;i<=N;i++)
{
K=i;
do
{
if(TREE[K]>TREE[K/2])
Values are inserted in the heap
{
temp=TREE[K];
TREE[K]=TREE[K/2];
TREE[K/2]=temp;
}
//
p=K/2;
K=p;
}
while(K!=0);
}
printf("\n\n\n On inserting values are arranged as \n");
for(i=1;i<=N;i++)
printf("%d\t",TREE[i]);
for(j=N;j>0;j--)
{
temp=TREE[1];
TREE[1]=TREE[j];
TREE[j]=temp;
p=0;
do
{
c=2*p+2;
if((TREE[c][/c]<TREE[c language="+1"][/c]) && c<j1)
c++;
if(TREE[p]<TREE[c][/c] && c<j)
{
temp=TREE[p];
TREE[p]=TREE[c][/c];
TREE[c][/c]=temp;
}
p=c;
}
while(c<(j+1));
}
printf("\n\n\n The sorted nos are..");
for(i=1;i<=N;i++)
printf("%d\t",TREE[i]);
getch();
}
Bucket sort
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
#include<stdio.h>
#include<conio.h>
void Bucket_Sort(int array[], int n)
{
int i, j;
int count[n];
for(i=0; i < n; i++)
{
count[i] = 0;
}
for(i=0; i < n; i++)
{
(count[array[i]])++;
}
for(i=0,j=0; i < n; i++)
{
for(; count[i]>0;(count[i])--)
{
array[j++] = i;
}
}
}
int main()
{
int array[100];
int n,i;
clrscr();
printf("Enter How many Numbers : ");
scanf("%d",&n);
printf("Enter the %d elements to be sorted:\n",n);
for(i = 0; i < n; i++ )
{
scanf("%d",&array[i]);
}
printf("\nThe array of elements before sorting : \n");
for (i = 0;i < n;i++)
{
printf("%d ", array[i]);
}
printf("\nThe array of elements after sorting : \n");
Bucket_Sort(array, n);
for (i = 0;i < n;i++)
43.
44.
45.
46.
47.
48.
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}