Last change
on this file since 1525 was
1525,
checked in by ayache, 9 years ago
|
D2.2: function pointers using JMP.
|
File size:
1.0 KB
|
Line | |
---|
1 | |
---|
2 | #define SIZE 5 |
---|
3 | |
---|
4 | void swap (int a[], int i, int j) { |
---|
5 | int t; |
---|
6 | t = a[i] ; a[i] = a[j] ; a[j] = t; |
---|
7 | } |
---|
8 | |
---|
9 | int partition (int a[], int l, int r) { |
---|
10 | int pivot, i, j; |
---|
11 | pivot = a[l]; |
---|
12 | i = l; j = r+1; |
---|
13 | |
---|
14 | while (1) { |
---|
15 | while (i <= r && a[i] <= pivot) ++i; |
---|
16 | do --j; while (a[j] > pivot); |
---|
17 | if (i >= j) break; |
---|
18 | swap(a, i, j); |
---|
19 | } |
---|
20 | swap(a, l, j); |
---|
21 | return j; |
---|
22 | } |
---|
23 | |
---|
24 | void quicksort (int a[], int l, int r) { |
---|
25 | int j; |
---|
26 | |
---|
27 | if (l < r) { |
---|
28 | j = partition(a, l, r); |
---|
29 | quicksort(a, l, j-1); |
---|
30 | quicksort(a, j+1, r); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | /* |
---|
35 | void print_tab (int tab[], int size) { |
---|
36 | int i; |
---|
37 | |
---|
38 | for (i = 0 ; i < size ; i++) { |
---|
39 | print_sint(tab[i]); |
---|
40 | space(); |
---|
41 | } |
---|
42 | newline(); |
---|
43 | } |
---|
44 | */ |
---|
45 | |
---|
46 | int is_sorted (int tab[], int size) { |
---|
47 | int i, res = 1; |
---|
48 | |
---|
49 | for (i = 0 ; i < size-1 ; i++) res = res && (tab[i] <= tab[i+1]); |
---|
50 | |
---|
51 | return res; |
---|
52 | } |
---|
53 | |
---|
54 | int main () { |
---|
55 | int tab[SIZE] = {26, -21, 43, -62, 8}; |
---|
56 | |
---|
57 | quicksort(tab, 0, SIZE-1); |
---|
58 | // print_tab(tab, SIZE); |
---|
59 | // print_sint(is_sorted(tab, SIZE)); |
---|
60 | // newline(); |
---|
61 | |
---|
62 | return (is_sorted(tab, SIZE)); |
---|
63 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.