This repository has been archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.c
85 lines (74 loc) · 1.4 KB
/
quicksort.c
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
82
83
84
85
#include<stdio.h>
#include<stdlib.h>
int partition(int arr[], int front, int end,int len)
{
int pivot = arr[front];
int c1 = front + 1;
int c2 = end;
while(1)
{
while(arr[c1]<pivot && c1<=c2)
{
c1=c1+1;
}
while(arr[c2]>pivot && c1<=c2)
{
c2 = c2 - 1;
}
if(c1>c2)
break;
else
{
int temp = arr[c1];
arr[c1] = arr[c2];
arr[c2] = temp;
c1 = c1 + 1;
c2 = c2 - 1;
int beg;
for(beg= 0;beg<len+1;beg++)
{
printf("%d ",arr[beg]);
}
printf("\n");
}
}
int temp = arr[front];
arr[front] = arr[c2];
arr[c2] = temp;
int beg;
for(beg= 0;beg<len+1;beg++)
{
printf("%d ",arr[beg]);
}
printf("\n");
return c2;
}
void quicksort(int arr[], int front, int end,int len)
{
if ((end - front)>=1)
{
int i = partition(arr,front,end,len);
quicksort(arr,front,i,len);
quicksort(arr,i+1,end,len);
}
}
int main()
{
int n;
int i;
char c[20][100];
scanf("%d\n",&n);
int a[n];
for (i=0;i<n;i++)
{
gets(c[i]);
a[i] = atoi(c[i]);
}
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
quicksort(a,0,n-1,n-1);
return 0;
}