C Program for Quick Sort Arrange a list of Integers in Ascending order

Estudies4you

1 #include<stdio.h>
2 void Quick_Sort(int a[],int left,int right);
3 int main()
4 {
5 int a[10];
6 int i,j,temp,n,key;
7 printf("Enter the no.of elements : ");
8 scanf("%d",&n);
9 for(i=0;i<n;i++)
10 {
11 printf("Enter a[%d] element : ",i);
12 scanf("%d",&a[i]);
13 }
14 printf("Before Sorting array elements are : ");
15 for(i=0;i<n;i++)
16 {
17 printf("%d ",a[i]);
18 }
19 Quick_Sort(a,0,n-1);
20 printf("\nafter Sorting array elements are : ");
21 for(i=0;i<n;i++)
22 {
23 printf("%d ",a[i]);
24 }
25 }
26 void Quick_Sort(int a[],int left,int right)
27 {
28 int pivot,i,j,temp;
29 i=left;
30 j=right;
31 pivot=left;
32 while(i<j)
33 {
34 while(a[i]<=a[pivot]&&i<=right)
35 i++;
36 while(a[j]>=a[pivot]&&j>left)
37 j--;
38 if(i<j)
39 {
40 temp=a[i];
41 a[i]=a[j];
42 a[j]=temp;
43 }
44 }
45 if(i>j)
46 {
47 temp=a[pivot];
48 a[pivot]=a[j];
49 a[j]=temp;
50 pivot=j;
51 }
52 if(left<pivot)
53 Quick_Sort(a,left,pivot-1);
54 if(right>pivot)
55 Quick_Sort(a,pivot+1,right);
56 }
OUTPUT:
Enter the no.of elements : 5
Enter a[0] element : 8
Enter a[1] element : 4
Enter a[2] element : 6
Enter a[3] element : 2
Enter a[4] element : 3
Before Sorting array elements are : 8 4 6 2 3
after Sorting array elements are : 2 3 4 6 8
To Top