0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 #include <stdlib.h>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 #include <stdio.h>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 /* // quickSort
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 //
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 // This public-domain C implementation by Darel R. Finley.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 //
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 // * Returns true if sort was successful, or false if the nested
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 // pivots went too deep, in which case your array will have
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 // been re-ordered, but probably not sorted correctly.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 //
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 // * This function assumes it is called with valid parameters.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 //
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 // * Example calls:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 // quickSort(&myArray[0],5); // sorts elements 0, 1, 2, 3, and 4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 // quickSort(&myArray[3],5); // sorts elements 3, 4, 5, 6, and 7*/
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 #define MAX_LEVELS 1000
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 int quickSort(double *arr, int elements)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 double piv;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 int beg[MAX_LEVELS], end[MAX_LEVELS];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 int i=0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 int L, R;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 beg[0]=0; end[0]=elements;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 while (i>=0)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 L=beg[i]; R=end[i]-1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 if (L<R)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 piv=arr[L];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 if (i==MAX_LEVELS-1) return -1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 while (L<R)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 while (arr[R]>=piv && L<R)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 R--;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 if (L<R)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 arr[L++]=arr[R];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 while (arr[L]<=piv && L<R)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 L++;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 if (L<R)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 arr[R--]=arr[L];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 i--;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 return 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 }
|