Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members

tsort.h

00001 00003 // This software is distributed as part of the Improla library. 00004 // Improla is a GUI framework for image processing. 00005 // 00006 // Copyright (c) 2004, B. R. Siva Chandra, India 00007 // 00008 // This program is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU General Public License 00010 // as published by the Free Software Foundation; either version 2 00011 // of the License, or (at your option) any later version. 00012 // 00013 // This program is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with this program; if not, write to the Free Software 00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00021 // 00022 // In case you would like to contact the author, use the following e-mail 00023 // address: sivachandra_br@yahoo.com 00025 00026 #ifndef __T_SORT_H__ 00027 #define __T_SORT_H__ 00028 00029 #include <tnt/tnt_array1d.h> 00030 00031 namespace tutk 00032 { // Start of namespace brace. 00033 00036 enum Order {ASCENDING, DESCENDING}; 00037 00040 template < class T > 00041 void bubbleSort(TNT::Array1D< T > array, Order order) 00042 { 00043 int n = array.dim(); 00044 00045 if (order == ASCENDING) 00046 { 00047 for (int i = n-1; i > 0; i--) 00048 { 00049 for (int j = 0; j < i; j++) 00050 { 00051 if (array[j] > array[j+1]) 00052 { 00053 T temp = array[j]; 00054 array[j] = array[j+1]; 00055 array[j+1] = temp; 00056 } 00057 } 00058 } 00059 } 00060 else if (order == DESCENDING) 00061 { 00062 for (int i = n-1; i > 0; i--) 00063 { 00064 for (int j = 0; j < i; j++) 00065 { 00066 if (array[j] < array[j+1]) 00067 { 00068 T temp = array[j]; 00069 array[j] = array[j+1]; 00070 array[j+1] = temp; 00071 } 00072 } 00073 } 00074 } 00075 00076 return; 00077 } 00078 00081 template < class T > 00082 void mergeSort(TNT::Array1D< T > array, Order order, int first, int last) 00083 { 00084 if (first >= last) 00085 return; 00086 00087 int bound1 = (first + last)/2; 00088 mergeSort(array, order, first, bound1); 00089 mergeSort(array, order, bound1 + 1, last); 00090 00091 int sortSize = last - first + 1, p = first, q = bound1 + 1, mark = 0; 00092 TNT::Array1D< T > subArray(sortSize); 00093 00094 while (mark < sortSize && order == ASCENDING) 00095 { 00096 if (array[p] < array[q] && p <= bound1 && q <= last) 00097 { 00098 subArray[mark] = array[p]; 00099 p++; 00100 } 00101 else if (array[q] <= array[p] && p <= bound1 && q <= last) 00102 { 00103 subArray[mark] = array[q]; 00104 q++; 00105 } 00106 else if (p > bound1) 00107 { 00108 subArray[mark] = array[q]; 00109 q++; 00110 } 00111 else if (q > last) 00112 { 00113 subArray[mark] = array[p]; 00114 p++; 00115 } 00116 00117 mark++; 00118 } 00119 00120 while (mark < sortSize && order == DESCENDING) 00121 { 00122 if (array[p] > array[q] && p <= bound1 && q <= last) 00123 { 00124 subArray[mark] = array[p]; 00125 p++; 00126 } 00127 else if (array[q] >= array[p] && p <= bound1 && q <= last) 00128 { 00129 subArray[mark] = array[q]; 00130 q++; 00131 } 00132 else if (p > bound1) 00133 { 00134 subArray[mark] = array[q]; 00135 q++; 00136 } 00137 else if (q > last) 00138 { 00139 subArray[mark] = array[p]; 00140 p++; 00141 } 00142 00143 mark++; 00144 } 00145 00146 for (int i = first; i <= last; i++) 00147 array[i] = subArray[i - first]; 00148 00149 00150 return; 00151 } 00152 00153 } // End if namespace brace. 00154 00155 #endif // __T_SORT_H__ 00156

Generated on Mon Nov 22 11:12:41 2004 for ImprolaLib by doxygen 1.3.7