-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.h
92 lines (75 loc) · 1.8 KB
/
quickSort.h
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
void swap ( int* a, int* b )
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int *dataQuickSort, int l, int h)
{
int j;
int x = dataQuickSort[h];
int i = (l - 1);
for (j = l; j <= h- 1; j++)
{
if (dataQuickSort[j] <= x)
{
i++;
swap (&dataQuickSort[i], &dataQuickSort[j]);
}
}
swap (&dataQuickSort[i + 1], &dataQuickSort[h]);
return (i + 1);
}
// l : Starting index,
// h : Ending index
void quickSort(int *dataQuickSort, int l, int h)
{
// Create an auxiliary stack
int stack[ h - l + 1 ];
int top = -1;
// push initial values of l and h to stack
stack[ ++top ] = l;
stack[ ++top ] = h;
// Keep popping from stack while is not empty
while ( top >= 0 )
{
// Pop h and l
h = stack[ top-- ];
l = stack[ top-- ];
// Set pivot element at its correct position
// in sorted array
int p = partition( dataQuickSort, l, h );
// If there are elements on left side of pivot,
// then push left side to stack
if ( p-1 > l )
{
stack[ ++top ] = l;
stack[ ++top ] = p - 1;
}
// If there are elements on right side of pivot,
// then push right side to stack
if ( p+1 < h )
{
stack[ ++top ] = p + 1;
stack[ ++top ] = h;
}
}
}
void displayQuickSort(int *dataQuickSort,int n )
{
int i;
for ( i = 0; i < n;i++ )
{
printf( "[%d]",dataQuickSort[i] );
}
}
void ReadArrayQuickSort(int *dataQuickSort,int n)
{
int i;
for(i=0;i<n;i++)
{
dataQuickSort[i] = rand()*rand() % 100000+1;
printf("[%d]",dataQuickSort[i]);
// scanf("%d",&data[i]);
}
}