-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicksort.c
94 lines (76 loc) · 1.94 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
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
#define SWAP(a,b) do { \
int t=0; \
t = a; \
a = b; \
b = t; \
} while(0)
void PrintElement(int A[], int n)
{
int i = 0;
for(i=0; i<n; i++)
{
printf("%d\t",A[i]);
}
printf("\n");
}
int FindPartition(int A[], int low, int high)
{
int key = 0;
int i,j;
key = A[low];
i = low; j = high;
printf("FindPartition: key: %d, i: %d, j: %d\n",key,i,j);
while(i <= j) {
while(key >= A[i]) i++;
while(key < A[j]) j--;
if(i < j) {
printf("(i < j) : SWAP - (%d, %d)\n",A[i],A[j]);
SWAP(A[i], A[j]);
}
else {
printf("(i >= j) : SWAP - (%d, %d)\n",A[low],A[j]);
SWAP(A[low], A[j]);
}
}
return j;
}
int Quicksort(int A[], int n, int low, int high)
{
int partition = 0;
if(low < high) {
printf("-> low: %d , high: %d\n",low,high);
partition = FindPartition(A,low,high);
//printf("Partition Element: (%d) ->\n",A[j]);
printf("partition: %d\n", partition);
PrintElement(A,n);
printf("\n==========================================\n");
Quicksort(A,n,low,partition-1);
//printf("1 -> low: %d , high: %d\n",low,high);
PrintElement(A,n);
printf("-----------------------------------------\n");
Quicksort(A,n,partition+1,high);
//printf("2 -> low: %d , high: %d\n",low,high);
PrintElement(A,n);
printf("-----------------------------------------\n");
}
}
int main()
{
int i=0; int n=0;
int A[MAXSIZE];
int low,high;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements: \n");
for(i=0; i<n; i++)
scanf("%d",&A[i]);
low = 0; high = (n-1);
Quicksort(A,n,low,high);
printf("The sorted elements are: \n");
PrintElement(A, n);
return 0;
}