OpenCores
URL https://opencores.org/ocsvn/tcp_socket/tcp_socket/trunk

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [chips2/] [docs/] [source/] [examples/] [example_3.rst] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 jondawson
 
2
 
3
Implement Quicksort
4
-------------------
5
 
6
This example sorts an array of data using the
7
`Quick Sort algorithm `_
8
 
9
The quick-sort algorithm is a recurrsive algorithm, but *Chips* does not
10
support recursive functions. Since the level of recursion is bounded, it is
11
possible to implement the function using an explicitly created stack.
12
 
13
.. code-block:: c
14
 
15
    /* fft.c */
16
    /* Jonathan P Dawson */
17
    /* 2013-12-23 */
18
 
19
    /* Based on the in-place algorithm on the Wikipedia page */
20
    /* http://en.wikipedia.org/wiki/Quicksort#In-place_version */
21
 
22
    /*globals*/
23
    const unsigned length = 32;
24
 
25
    /* partition subarray */
26
    unsigned partition(
27
            int array[],
28
            unsigned left,
29
            unsigned right,
30
            unsigned pivot_index)
31
    {
32
 
33
        int temp, pivot_value;
34
        unsigned i, store_index;
35
 
36
        store_index = left;
37
        pivot_value = array[pivot_index];
38
 
39
        temp = array[pivot_index];
40
        array[pivot_index] = array[right];
41
        array[right] = temp;
42
 
43
        for(i=left; i
44
            if(array[i] <= pivot_value){
45
                temp = array[store_index];
46
                array[store_index] = array[i];
47
                array[i] = temp;
48
                store_index++;
49
            }
50
        }
51
 
52
        temp = array[store_index];
53
        array[store_index] = array[right];
54
        array[right] = temp;
55
 
56
        return store_index;
57
    }
58
 
59
 
60
    /* recursive sort */
61
    void quick_sort(int array[]){
62
 
63
        /* reccursive functions are not supported */
64
        /* implement a stack explicitly */
65
        unsigned left, right, pivot;
66
        unsigned lefts[length];
67
        unsigned rights[length];
68
        unsigned stack_pointer = 1;
69
 
70
        /* initialy push whole array onto stack */
71
        lefts[0] = 0;
72
        rights[0] = length-1;
73
 
74
        while(stack_pointer){
75
 
76
 
77
            /* pop a sub-array from stack */
78
            stack_pointer--;
79
            left = lefts[stack_pointer];
80
            right = rights[stack_pointer];
81
 
82
 
83
            /* if the subarray has two or more elements */
84
            if (left < right){
85
 
86
                /* partition sub array into two further sub arrays */
87
                pivot = (left + right) >> 1;
88
                pivot = partition(array, left, right, pivot);
89
 
90
                /* push both subarrays onto stack */
91
                lefts[stack_pointer] = left;
92
                rights[stack_pointer] = pivot - 1;
93
                stack_pointer++;
94
                lefts[stack_pointer] = pivot + 1;
95
                rights[stack_pointer] = right;
96
                stack_pointer++;
97
            }
98
 
99
        }
100
 
101
    }
102
 
103
 
104
    void main(){
105
        int array[length];
106
        unsigned i;
107
 
108
        /* Fill array with zeros */
109
        for(i=0; i
110
            array[i] = 0;
111
        }
112
 
113
        /* Add unsorted data to the array */
114
        array[0] = 16;
115
        array[1] = 15;
116
        array[2] = 14;
117
        array[3] = 13;
118
        array[4] = 12;
119
        array[5] = 11;
120
        array[6] = 10;
121
        array[7] = 9;
122
        array[8] = 8;
123
        array[9] = 7;
124
        array[10] = 6;
125
        array[11] = 5;
126
        array[12] = 4;
127
        array[13] = 3;
128
        array[14] = 2;
129
        array[15] = 1;
130
 
131
        /* Sort the array */
132
        quick_sort(array);
133
 
134
        for(i=0; i
135
            report(array[i]);
136
        }
137
 
138
    }
139
 
140
The algorithm is tested using an array containing out of order values. The program correctly sorts the array::
141
 
142
 
143
 
144
 
145
 
146
 
147
 
148
 
149
 
150
 
151
 
152
 
153
 
154
 
155
 
156
 
157
 
158
         1 (report at line: 122 in file: sort.c)
159
         2 (report at line: 122 in file: sort.c)
160
         3 (report at line: 122 in file: sort.c)
161
         4 (report at line: 122 in file: sort.c)
162
         5 (report at line: 122 in file: sort.c)
163
         6 (report at line: 122 in file: sort.c)
164
         7 (report at line: 122 in file: sort.c)
165
         8 (report at line: 122 in file: sort.c)
166
         9 (report at line: 122 in file: sort.c)
167
        10 (report at line: 122 in file: sort.c)
168
        11 (report at line: 122 in file: sort.c)
169
        12 (report at line: 122 in file: sort.c)
170
        13 (report at line: 122 in file: sort.c)
171
        14 (report at line: 122 in file: sort.c)
172
        15 (report at line: 122 in file: sort.c)
173
        16 (report at line: 122 in file: sort.c)
174
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.