DAS  3.1.6 - 18/09/2017
quick.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * This program demonstrates the use of the quick sort algorithm. For
17  * more information about this and other sorting algorithms, see
18  * http://linux.wku.edu/~lamonml/kb.html
19  *
20  */
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 //#include <process.h>
25 
26 //#define NUM_ITEMS 10000
27 
28 void quickSort(double numbers[], int array_size);
29 void q_sort(double numbers[], int left, int right);
30 
31 //int numbers[NUM_ITEMS];
32 
33 /*
34 int main()
35 {
36  int i;
37 
38  //seed random number generator
39  srand(getpid());
40 
41  //fill array with random integers
42  for (i = 0; i < NUM_ITEMS; i++)
43  numbers[i] = rand();
44 
45  //perform quick sort on array
46  quickSort(numbers, NUM_ITEMS);
47 
48  printf("Done with sort.\n");
49  for (i = 0; i < NUM_ITEMS; i++)
50  printf("%i\n", numbers[i]);
51 }
52 
53 */
54 void quickSort(double numbers[], int array_size)
55 {
56  q_sort(numbers, 0, array_size - 1);
57 }
58 
59 
60 
61 void q_sort(double numbers[], int left, int right)
62 {
63  int pivot;
64  double l_hold, r_hold;
65 
66  l_hold = left;
67  r_hold = right;
68  pivot = (int) numbers[left];
69  while (left < right)
70  {
71  while ((numbers[right] >= pivot) && (left < right))
72  right--;
73  if (left != right)
74  {
75  numbers[left] = numbers[right];
76  left++;
77  }
78  while ((numbers[left] <= pivot) && (left < right))
79  left++;
80  if (left != right)
81  {
82  numbers[right] = numbers[left];
83  right--;
84  }
85  }
86  numbers[left] = pivot;
87  pivot = left;
88  left = (int) l_hold;
89  right = (int) r_hold;
90  if (left < pivot)
91  q_sort(numbers, left, pivot-1);
92  if (right > pivot)
93  q_sort(numbers, pivot+1, right);
94 }
void q_sort(double numbers[], int left, int right)
Definition: quick.c:61
void quickSort(double numbers[], int array_size)
Definition: quick.c:54
______________________________________________________________________________________
Generated on Mon Sep 18 2017 11:44:09 for DAS - Rel. 3.1.6 - 18/09/2017.