Skip to content
Snippets Groups Projects
Commit 4cb74637 authored by Kai Renz's avatar Kai Renz
Browse files

Feature: Added a possible Solution

Refactored the Method fl to show
what is actually going on.
parent 4c304b18
No related branches found
No related tags found
No related merge requests found
//
// Created by Kai Renz on 02.11.23. static const int UNUSED = -1;
//
bool canSwap(const int *array, int length, int valueAtPosition);
int fl(int* i,int l) { int doSwap(int *array, int valueAtPosition);
for (int k=0;k<l;k++) {
if (i[k]<0||i[k]>=l) { void doStuffAtPosition(int *array, int length, int position);
i[k]=-1;
} else { void handleValidValueAtPosition(int *array, int length, int position);
if (i[k]!=k) {
int v=i[k]; bool hasUnallowedValueAtPosition(const int *array, int length, int position);
if (i[k]!=k) {
i[k]=-1; int findeLueckeInArray(int* array, int length) {
} for (int position=0; position < length; position++) {
while (v>=0&&v<l&&i[v]!=v) { doStuffAtPosition(array, length, position);
int t=i[v]; }
i[v]=v; int positionLuecke=0;
v=t; for (; positionLuecke < length && array[positionLuecke] != UNUSED; positionLuecke++) {}
} return positionLuecke;
} }
}
void doStuffAtPosition(int *array, int length, int position) {
if (hasUnallowedValueAtPosition(array, length, position)) {
array[position]= UNUSED;
return;
} }
int m=0;
for (;m<l&&i[m]!=-1;m++) {} if (array[position] == position) {
return m; return;
} }
\ No newline at end of file
handleValidValueAtPosition(array, length, position);
}
bool hasUnallowedValueAtPosition(const int *array, int length, int position) {
return array[position] < 0 || array[position] >= length; }
void handleValidValueAtPosition(int *array, int length, int position) {
int valueAtPosition=array[position];
array[position]= UNUSED;
while (canSwap(array, length, valueAtPosition)) {
valueAtPosition = doSwap(array, valueAtPosition);
}
}
int doSwap(int *array, int valueAtPosition) {
int valueAtFuturePosition=array[valueAtPosition];
array[valueAtPosition]=valueAtPosition;
return valueAtFuturePosition;
}
bool canSwap(const int *array, int length, int valueAtPosition) {
return valueAtPosition >= 0 && valueAtPosition < length
&& array[valueAtPosition] != valueAtPosition;
}
#include <iostream> #include <iostream>
int fl(int *i,int l); int findeLueckeInArray(int *array, int length);
void expect(bool b, std::string text) { void expect(bool b, std::string text) {
if (!b) { if (!b) {
...@@ -23,15 +23,15 @@ int main() { ...@@ -23,15 +23,15 @@ int main() {
(int[]) {4, -1,1,2,3} (int[]) {4, -1,1,2,3}
}; };
expect(fl( v[0],3) == 3,"Expected to be 3"); expect(findeLueckeInArray(v[0], 3) == 3, "Expected to be 3");
expect(fl( v[1],2) == 0,"Expected to be 0"); expect(findeLueckeInArray(v[1], 2) == 0, "Expected to be 0");
expect(fl( v[2],3) == 1,"Expected to be 1"); expect(findeLueckeInArray(v[2], 3) == 1, "Expected to be 1");
expect(fl( v[3],5) == 4,"Expected to be 4"); expect(findeLueckeInArray(v[3], 5) == 4, "Expected to be 4");
expect(fl( v[4],5) == 4,"Expected to be 4"); expect(findeLueckeInArray(v[4], 5) == 4, "Expected to be 4");
expect(fl( v[5],5) == 0,"Expected to be 0"); expect(findeLueckeInArray(v[5], 5) == 0, "Expected to be 0");
expect(fl( v[6],5) == 4,"Expected to be 4"); expect(findeLueckeInArray(v[6], 5) == 4, "Expected to be 4");
expect(fl( v[7],5) == 5,"Expected to be 5"); expect(findeLueckeInArray(v[7], 5) == 5, "Expected to be 5");
expect(fl( v[8],5) == 0,"Expected to be 0"); expect(findeLueckeInArray(v[8], 5) == 0, "Expected to be 0");
std::cout << "Success!"; std::cout << "Success!";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment