From 4cb746370ae3a7e58ddc4be73215d2837816fffc Mon Sep 17 00:00:00 2001 From: Kai Renz <kai.renz@h-da.de> Date: Fri, 3 Nov 2023 09:06:01 +0100 Subject: [PATCH] Feature: Added a possible Solution Refactored the Method fl to show what is actually going on. --- fl.cpp | 80 ++++++++++++++++++++++++++++++++++++++------------------ main.cpp | 20 +++++++------- 2 files changed, 65 insertions(+), 35 deletions(-) diff --git a/fl.cpp b/fl.cpp index bfa6105..9670016 100644 --- a/fl.cpp +++ b/fl.cpp @@ -1,26 +1,56 @@ -// -// Created by Kai Renz on 02.11.23. -// - -int fl(int* i,int l) { - for (int k=0;k<l;k++) { - if (i[k]<0||i[k]>=l) { - i[k]=-1; - } else { - if (i[k]!=k) { - int v=i[k]; - if (i[k]!=k) { - i[k]=-1; - } - while (v>=0&&v<l&&i[v]!=v) { - int t=i[v]; - i[v]=v; - v=t; - } - } - } + +static const int UNUSED = -1; + +bool canSwap(const int *array, int length, int valueAtPosition); +int doSwap(int *array, int valueAtPosition); + +void doStuffAtPosition(int *array, int length, int position); + +void handleValidValueAtPosition(int *array, int length, int position); + +bool hasUnallowedValueAtPosition(const int *array, int length, int position); + +int findeLueckeInArray(int* array, int length) { + for (int position=0; position < length; position++) { + doStuffAtPosition(array, length, position); + } + int positionLuecke=0; + 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++) {} - return m; -} \ No newline at end of file + + if (array[position] == position) { + return; + } + + 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; +} diff --git a/main.cpp b/main.cpp index c9366d2..f81bc32 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,6 @@ #include <iostream> -int fl(int *i,int l); +int findeLueckeInArray(int *array, int length); void expect(bool b, std::string text) { if (!b) { @@ -23,15 +23,15 @@ int main() { (int[]) {4, -1,1,2,3} }; - expect(fl( v[0],3) == 3,"Expected to be 3"); - expect(fl( v[1],2) == 0,"Expected to be 0"); - expect(fl( v[2],3) == 1,"Expected to be 1"); - expect(fl( v[3],5) == 4,"Expected to be 4"); - expect(fl( v[4],5) == 4,"Expected to be 4"); - expect(fl( v[5],5) == 0,"Expected to be 0"); - expect(fl( v[6],5) == 4,"Expected to be 4"); - expect(fl( v[7],5) == 5,"Expected to be 5"); - expect(fl( v[8],5) == 0,"Expected to be 0"); + expect(findeLueckeInArray(v[0], 3) == 3, "Expected to be 3"); + expect(findeLueckeInArray(v[1], 2) == 0, "Expected to be 0"); + expect(findeLueckeInArray(v[2], 3) == 1, "Expected to be 1"); + expect(findeLueckeInArray(v[3], 5) == 4, "Expected to be 4"); + expect(findeLueckeInArray(v[4], 5) == 4, "Expected to be 4"); + expect(findeLueckeInArray(v[5], 5) == 0, "Expected to be 0"); + expect(findeLueckeInArray(v[6], 5) == 4, "Expected to be 4"); + expect(findeLueckeInArray(v[7], 5) == 5, "Expected to be 5"); + expect(findeLueckeInArray(v[8], 5) == 0, "Expected to be 0"); std::cout << "Success!"; -- GitLab