Scopira  20080306
sort_imp.h
1 
2 /*
3  * Copyright (c) 2002 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED__SCOPIRA_TOOL_SORT_IMP__
15 #define __INCLUDED__SCOPIRA_TOOL_SORT_IMP__
16 
17 namespace scopira
18 {
19  namespace tool
20  {
21  template <class T> void qsort(T &s, int lo0, int hi0);
22  }
23 }
24 
45 template <class T> void scopira::tool::qsort(T &s, int lo0, int hi0)
46 {
47  int lo = lo0;
48  int hi = hi0;
49  int mid;
50 
51  if ( hi0 > lo0) {
52  // Arbitrarily establishing partition element as the midpoint of
53  // the array.
54  mid = ( lo0 + hi0 ) / 2;
55 
56  // loop through the array until indices cross
57  while( lo <= hi ) {
58  // find the first element that is greater than or equal to
59  // the partition element starting from the left Index.
60  while( ( lo < hi0 ) && ( s.compare_element(lo, mid) < 0 ) )
61  lo++;
62  // find an element that is smaller than or equal to
63  // the partition element starting from the right Index.
64  while( ( hi > lo0 ) && ( s.compare_element(hi, mid) > 0 ) )
65  hi--;
66 
67  // if the indexes have not crossed, swap
68  if( lo <= hi ) {
69  // because we're dealin with indecies (and not the data directly)
70  // we need to see if we swapped out midpoint away
71  if (lo == mid)
72  mid = hi;
73  else if (hi == mid)
74  mid = lo;
75  s.swap_element(lo, hi);
76  lo++;
77  hi--;
78  }
79  }
80 
81  qsort(s, lo0, hi );
82  qsort(s, lo, hi0 );
83  }
84 }//quick_sort
85 
86 #endif
87 
Definition: archiveflow.h:20
void qsort(T &s, int lo0, int hi0)
Definition: sort_imp.h:45