-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample008.c
57 lines (49 loc) · 1.04 KB
/
sample008.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
#include "ptuclib.h"
/* Program quicksort */
/* Type declaration: */
/* Variable declaration: */
int cnt, size;
int numbersgiven[5];
/* Function declaration: */
void QSort(int numbers[5], int left, int right)
{
int pivot, l_ptr, r_ptr;
l_ptr = left;
r_ptr = right;
pivot = numbers[left];
while ((left < right))
{
while (((numbers[right] >= pivot) && (left < right))) right = right - 1;
if ((left != right))
{
numbers[left] = numbers[right];
left = left + 1;
}
while (((numbers[left] <= pivot) && (left < right))) left = left + 1;
if ((left != right))
{
numbers[right] = numbers[left];
right = right - 1;
}
}
numbers[left] = pivot;
pivot = left;
left = l_ptr;
right = r_ptr;
if ((left < pivot)) QSort(numbers,left,pivot - 1);
if ((right > pivot)) QSort(numbers,pivot + 1,right);
}
/* Main: */
int main() {
writeString("How many numbers?\n");
scanf("%d", &size);
cnt = 0;
while ((cnt < size))
{
writeString("Give number\n");
scanf("%d", &numbersgiven[cnt]);
cnt = cnt + 1;
}
QSort(numbersgiven,0,size - 1);
return 0;
}