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

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [chips2/] [examples/] [sort.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 jondawson
/* fft.c */
2
/* Jonathan P Dawson */
3
/* 2013-12-23 */
4
 
5
/* Based on the in-place algorithm on the Wikipedia page */
6
/* http://en.wikipedia.org/wiki/Quicksort#In-place_version */
7
 
8
/*globals*/
9
const unsigned length = 32;
10
 
11
/* partition subarray */
12
unsigned partition(
13
        int array[],
14
        unsigned left,
15
        unsigned right,
16
        unsigned pivot_index)
17
{
18
 
19
    int temp, pivot_value;
20
    unsigned i, store_index;
21
 
22
    store_index = left;
23
    pivot_value = array[pivot_index];
24
 
25
    temp = array[pivot_index];
26
    array[pivot_index] = array[right];
27
    array[right] = temp;
28
 
29
    for(i=left; i<right; i++){
30
        if(array[i] <= pivot_value){
31
            temp = array[store_index];
32
            array[store_index] = array[i];
33
            array[i] = temp;
34
            store_index++;
35
        }
36
    }
37
 
38
    temp = array[store_index];
39
    array[store_index] = array[right];
40
    array[right] = temp;
41
 
42
    return store_index;
43
}
44
 
45
 
46
/* recursive sort */
47
void quick_sort(int array[]){
48
 
49
    /* reccursive functions are not supported */
50
    /* implement a stack explicitly */
51
    unsigned left, right, pivot;
52
    unsigned lefts[length];
53
    unsigned rights[length];
54
    unsigned stack_pointer = 1;
55
 
56
    /* initialy push whole array onto stack */
57
    lefts[0] = 0;
58
    rights[0] = length-1;
59
 
60
    while(stack_pointer){
61
 
62
 
63
        /* pop a sub-array from stack */
64
        stack_pointer--;
65
        left = lefts[stack_pointer];
66
        right = rights[stack_pointer];
67
 
68
 
69
        /* if the subarray has two or more elements */
70
        if (left < right){
71
 
72
            /* partition sub array into two further sub arrays */
73
            pivot = (left + right) >> 1;
74
            pivot = partition(array, left, right, pivot);
75
 
76
            /* push both subarrays onto stack */
77
            lefts[stack_pointer] = left;
78
            rights[stack_pointer] = pivot - 1;
79
            stack_pointer++;
80
            lefts[stack_pointer] = pivot + 1;
81
            rights[stack_pointer] = right;
82
            stack_pointer++;
83
        }
84
 
85
    }
86
 
87
}
88
 
89
 
90
void main(){
91
    int array[length];
92
    unsigned i;
93
 
94
    /* Fill array with zeros */
95
    for(i=0; i<length; i++){
96
        array[i] = 0;
97
    }
98
 
99
    /* Add unsorted data to the array */
100
    array[0] = 16;
101
    array[1] = 15;
102
    array[2] = 14;
103
    array[3] = 13;
104
    array[4] = 12;
105
    array[5] = 11;
106
    array[6] = 10;
107
    array[7] = 9;
108
    array[8] = 8;
109
    array[9] = 7;
110
    array[10] = 6;
111
    array[11] = 5;
112
    array[12] = 4;
113
    array[13] = 3;
114
    array[14] = 2;
115
    array[15] = 1;
116
 
117
    /* Sort the array */
118
    quick_sort(array);
119
 
120
    for(i=0; i<length; i++){
121
        report(array[i]);
122
    }
123
 
124
}

powered by: WebSVN 2.1.0

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