00001
00002
00003
00004
00005
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
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096