forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.c
More file actions
81 lines (74 loc) · 1.13 KB
/
quick_sort.c
File metadata and controls
81 lines (74 loc) · 1.13 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
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
int common=0;
void swap (int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
return;
}
int partition(int *a,int start,int end)
{
int i=0;
int pivot_index=0;
int pivot=a[end];
if (start==end)
return pivot_index;
for (i=0;i<end;i++)
{
if (a[i]<pivot)
{
swap(&a[i],&a[pivot_index]);
pivot_index++;
}
}
int temp=pivot_index;
for (i=0;i<end;i++)
{
if (a[i]==pivot)
{
swap(&a[i],&a[pivot_index]);
pivot_index++;
}
}
common=pivot_index-temp;
swap(&a[pivot_index],&a[end]);
return pivot_index;
}
void quicksort (int * a, int start,int end)
{
if (start>=end)
return;
int pivot=partition(a,start,end);
int temp=common;
common=0;
quicksort(a,start,pivot-temp-1);
quicksort(a,pivot+1,end);
}
void print(int *a,int n)
{
int i=0;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
if(i==n-1)
printf(". \n");
else printf(", ");
}
}
int main(void)
{
int i=0;
printf("Please enter the size of array:\n");
int n;
scanf("%d",&n);
int a[n];
printf("Please enter the elements of array:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
print(a,n);
quicksort(a,0,n-1);
print(a,n);
return 0;
}