-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.c
More file actions
71 lines (68 loc) · 1.06 KB
/
Copy pathQuickSort.c
File metadata and controls
71 lines (68 loc) · 1.06 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<stdio.h>
void quickSort(int *a,int start,int end);
int partition(int *a,int start,int end);
int main()
{
int n;
printf("Enter how many numbers you want to sort: ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
quickSort(arr,0,n);
printf("After quick sort:\n");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
void quickSort(int *a,int start,int end)
{
if(start>=end) return;
int mid=partition(a,start,end);
/*for(int i=0;i<end;i++)
{
printf("%d ",a[i]);
}
printf("\n");*/
quickSort(a,start,mid);
/*for(int i=0;i<end;i++)
{
printf("%d ",a[i]);
}
printf("\n");*/
quickSort(a,mid+1,end);
/*for(int i=0;i<end;i++)
{
printf("%d ",a[i]);
}
printf("\n");*/
}
int partition(int *a,int start,int end)
{
int pivot=a[end-1];
int i=start-1;
for(int j=start;j<end-1;j++)
{
if(a[j]<=pivot)
{
i++;
int temp=a[i];
a[i]=a[j];
a[j]=temp;
/*for(int i=0;i<end;i++)
{
printf("%d ",a[i]);
}
printf("\n");*/
}
}
i++;
int temp=a[i];
a[i]=a[end-1];
a[end-1]=temp;
return i;
}