From 461b5f553505f2b113a6fb6d9efaa2a5ecd4aed3 Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Thu, 26 May 2022 10:14:45 +0200 Subject: [PATCH] Commented InsertionSort --- Uebung 1/insertionSort_commented.cpp | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Uebung 1/insertionSort_commented.cpp diff --git a/Uebung 1/insertionSort_commented.cpp b/Uebung 1/insertionSort_commented.cpp new file mode 100644 index 0000000..efa37f6 --- /dev/null +++ b/Uebung 1/insertionSort_commented.cpp @@ -0,0 +1,40 @@ +void insertionSortBase(int array[], int array_size, bool asc) { + int i = 0; + while(i 0 && array[j-1] > array[j]){ + int currentValue = array[j]; + array[j] = array[j-1]; + array[j-1] = currentValue; + j--; + } + i++; + } +} + +void insertionSortOptimized(int array[], int array_size, bool asc) { + int i = 0; + while(i= 0 && array[j] > x){ + array[j+1] = array[j]; + j--; + } + array[j+1] = x; + i++; + } +} + +void insertionSortRecursive(int array[], int array_size, bool asc) { + if(array_size > 0) { + insertionSortRecursive(array, array_size-1, true); + int x = array[array_size-1]; + int j = array_size-2; + while( j >= 0 && array[j] > x){ + array[j+1] = array[j]; + j--; + } + array[j+1] = x; + } +} \ No newline at end of file