Skip to content

Commit d354b03

Browse files
committed
Implemented Quick Sort Algo in C
1 parent 2ce3d3a commit d354b03

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

quick_sort/clang/quick_sort.c

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
1-
//add your code here
1+
#include<stdio.h>
2+
void swap(int *a,int *b)
3+
{
4+
int temp=*a;
5+
*a=*b;
6+
*b=temp;
7+
}
8+
int partition(int a[],int low,int high)
9+
{
10+
int pivot = a[high];
11+
int i = low-1;
12+
for(int j=low;j<=high-1;j++)
13+
{
14+
if(a[j]<=pivot)
15+
{
16+
i++;
17+
swap(&a[i],&a[j]);
18+
}
19+
}
20+
swap(&a[i+1],&a[high]);
21+
return i+1;
22+
}
23+
void quickSort(int a[],int low,int high)
24+
{
25+
if(low<high)
26+
{
27+
int pi=partition(a,low,high);
28+
quickSort(a,low,pi-1);
29+
quickSort(a,pi+1,high);
30+
}
31+
}
32+
33+
int main()
34+
{
35+
int a[10],n;
36+
printf("Enter the number of elements you want to enter in the array:");
37+
scanf("%d",&n);
38+
for(int i=0;i<n;i++)
39+
{
40+
printf("a[%d]:",i);
41+
scanf("%d",&a[i]);
42+
}
43+
quickSort(a,0,n-1);
44+
printf("\nThe sorted array is:\n");
45+
for(int i=0;i<n;i++)
46+
{
47+
printf("%d\t",a[i]);
48+
}
49+
return 0;
50+
}
251

0 commit comments

Comments
 (0)