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

Tools/Math/Permutation.cpp

Go to the documentation of this file.
00001 /**
00002 * @file Math/Permutation.cpp
00003 * Implemets class Permutation
00004 *
00005 * @author <A href=mailto:sebastian.schmidt@udo.edu>Sebastian Schmidt</A>
00006 */
00007 
00008 #include "Permutation.h"
00009 
00010 bool Permutation::perm(int data[], const int& length) {
00011   int i = length;
00012   int max = -1;
00013   int posMax = -1;
00014   bool found = false;
00015   do {
00016     if ((max > data[--i]) && (posMax != -1)) {
00017       found = true;
00018     } else {
00019       max = data[i];
00020       posMax = i;
00021     }
00022   } while ((!found) && (i > 0));
00023   if (!found) {
00024     return false;
00025   }
00026   int posMin = posMax;
00027   int min = max;;
00028   for(int a=i+1; a<length; a++) {
00029     if ((data[a] < min) && (data[a] > data[i])) {
00030       posMin = a;
00031       min = data[a];
00032     }
00033   }
00034   data[posMin] = data[i];
00035   data[i] = min;
00036   sort(data, i+1, length-1);
00037   return true;
00038 }
00039 
00040 void Permutation::sort(int data[], const int& start, const int& end) {
00041   if (start == end) {
00042     return;
00043   }
00044   int pivot = data[(start+end)/2];
00045   int left = start;
00046   int right = end;
00047   do {
00048     while (data[left] < pivot) {
00049       left++;
00050     }
00051     while (data[right] > pivot) {
00052       right--;
00053     }
00054     if (left < right) {
00055       int tmp = data[left];
00056       data[left] = data[right];
00057       data[right] = tmp;
00058     }
00059     if (left <= right) {
00060       left++;
00061       right--;
00062     }
00063   } while (left <= right);
00064   if (start < right) {
00065     sort(data, start, right);
00066   }
00067   if (end > left) {
00068     sort(data, left, end);
00069   }
00070 }
00071 
00072 /*
00073 * Change log :
00074 *
00075 * $Log: Permutation.cpp,v $
00076 * Revision 1.1.1.1  2004/05/22 17:37:13  cvsadm
00077 * created new repository GT2004_WM
00078 *
00079 * Revision 1.1  2003/10/07 10:13:24  cvsadm
00080 * Created GT2004 (M.J.)
00081 *
00082 * Revision 1.1.1.1  2003/07/02 09:40:28  cvsadm
00083 * created new repository for the competitions in Padova from the 
00084 * tamara CVS (Tuesday 2:00 pm)
00085 *
00086 * removed unused solutions
00087 *
00088 * Revision 1.2  2003/03/12 14:47:58  schmidt
00089 * Removed a warning.
00090 *
00091 * Revision 1.1  2003/02/27 10:07:00  schmidt
00092 * Added three variants of a SensorFusionPlayersLocator.
00093 *
00094 *
00095 *
00096 */

Generated on Thu Sep 23 19:57:39 2004 for GT2004 by doxygen 1.3.6