Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
quicksort.h
1 /*
2  * $Id$
3  * genstruct::HashTable class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2004-08, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_GENSTRUCT_QUICKSORT_H_
23 #define ELM_GENSTRUCT_QUICKSORT_H_
24 
25 #include <elm/util/Comparator.h>
26 
27 namespace elm { namespace genstruct {
28 
29 // quick sort
30 template <class T, template <class> class A, class C>
31 void quicksort(A<T>& array) {
32  static const int max = 300;
33  int beg[max], end[max], i = 0, L, R, swap;
34  T piv;
35 
36  beg[0] = 0; end[0] = array.count();
37  while(i >= 0) {
38  L = beg[i]; R = end[i] - 1;
39  if(L < R) {
40  piv = array[L];
41  while(L < R) {
42  while(C::compare(array[R], piv) >= 0 && L < R) R--; if(L < R) array[L++] = array[R];
43  while(C::compare(array[L], piv) <= 0 && L<R) L++; if (L < R) array[R--] = array[L];
44  }
45  array[L] = piv; beg[i+1] = L+1; end[i+1] = end[i]; end[i++] = L;
46  if(end[i] - beg[i] > end[i-1] - beg[i-1]) {
47  swap = beg[i]; beg[i] = beg[i-1]; beg[i-1] = swap;
48  swap = end[i]; end[i] = end[i-1]; end[i-1] = swap;
49  }
50  }
51  else
52  i--;
53  }
54 }
55 
56 } } // elm
57 
58 #endif /* QUICKSORT_H_ */
const T & max(const T &x, const T &y)
Definition: compare.h:92
void swap(T &x, T &y)
Definition: misc.h:27
void quicksort(A< T > &array)
Definition: quicksort.h:31