source: tags/release-1.2.2/src/Utils/sort.cc

Last change on this file was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

File size: 5.0 KB
Line 
1// -----------------------------------------------------------------------
2// sort.cc: Alternative sorting functions, including one that takes a
3//          pair of arrays, sorts one but keeps pairs associated.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <functional>
30#include <duchamp/Utils/utils.hh>
31// #include <algorithm>
32// #include <iterator>
33// #include <iostream>
34
35template <class T> void sort(T *array, int begin, int end)
36{
37  T pivot = array[begin];
38  int i = begin + 1, j = end, k = end;
39  T t;
40 
41  while (i < j) {
42    if (array[i] < pivot) i++;
43    else if (array[i] > pivot) {
44      j--; k--;
45      t = array[i];
46      array[i] = array[j];
47      array[j] = array[k];
48      array[k] = t; }
49    else {
50      j--;
51      std::swap(array[i], array[j]);
52    }  }
53  i--;
54  std::swap(array[begin], array[i]);       
55  if (i - begin > 1)
56    sort(array, begin, i);
57  if (end - k   > 1)
58    sort(array, k, end);                     
59}
60template void sort<int>(int *array, int begin, int end);
61template void sort<long>(long *array, int begin, int end);
62template void sort<float>(float *array, int begin, int end);
63template void sort<double>(double *array, int begin, int end);
64//--------------------------------------------------------------------
65 
66template <class T1,class T2>
67void sort(T1 *array, T2 *matchingArray, int begin, int end)
68{
69  T1 pivot = array[begin];
70  int i = begin + 1, j = end, k = end;
71  T1 t;
72  T2 tt;
73
74  while (i < j) {
75    if (array[i] < pivot) i++;
76    else if (array[i] > pivot) {
77      j--; k--;
78      t = array[i];
79      array[i] = array[j];
80      array[j] = array[k];
81      array[k] = t;
82      tt = matchingArray[i];
83      matchingArray[i] = matchingArray[j];
84      matchingArray[j] = matchingArray[k];
85      matchingArray[k] = tt;
86    }
87    else {
88      j--;
89      std::swap(array[i], array[j]);
90      std::swap(matchingArray[i], matchingArray[j]);
91    } 
92  }
93  i--;
94  std::swap(array[begin], array[i]);       
95  std::swap(matchingArray[begin], matchingArray[i]);
96  if (i - begin > 1)
97    sort(array, matchingArray, begin, i);
98  if (end - k   > 1)
99    sort(array, matchingArray, k, end);                     
100}
101template void sort<int,int>(int *array, int *arrayB, int begin, int end);
102template void sort<int,float>(int *arrayA, float *arrayB, int begin, int end);
103template void sort<float,int>(float *array, int *arrayB, int begin, int end);
104template void sort<float,float>(float *array, float *arrayB, int begin, int end);
105//--------------------------------------------------------------------
106 
107
108
109
110// template< typename BidirectionalIterator, typename Compare >
111// void quick_sort( BidirectionalIterator first, BidirectionalIterator last, Compare cmp ) {
112//   if( first != last ) {
113//     BidirectionalIterator left  = first;
114//     BidirectionalIterator right = last;
115//     BidirectionalIterator pivot = left++;
116
117//     while( left != right ) {
118//       if( cmp( *left, *pivot ) ) {
119//          ++left;
120//       } else {
121//          while( (left != --right) && cmp( *pivot, *right ) )
122//            ;
123//          std::iter_swap( left, right );
124//       }
125//     }
126
127//     --left;
128//     std::iter_swap( first, left );
129
130//     quick_sort( first, left, cmp );
131//     quick_sort( right, last, cmp );
132//   }
133// }
134
135// template< typename BidirectionalIterator >
136// inline void quick_sort( BidirectionalIterator first, BidirectionalIterator last ) {
137//   quick_sort( first, last,
138//     std::less_equal< typename std::iterator_traits< BidirectionalIterator >::value_type >()
139//   );
140// }
141
142
143
144
145// void sort(float *arr, int beg, int end)
146// {
147//   if (end > beg + 1)
148//   {
149//     float piv = arr[beg];
150//     int l = beg + 1, r = end;
151//     while (l < r)
152//     {
153//       if (arr[l] <= piv)
154//         l++;
155//       else
156//         swap(arr[l], arr[--r]);
157//     }
158//     swap(arr[--l], arr[beg]);
159//     sort(arr, beg, l);
160//     sort(arr, r, end);
161//   }
162// }
163
Note: See TracBrowser for help on using the repository browser.