From bf2180851328872383b8689e742af32893bea7c1 Mon Sep 17 00:00:00 2001 From: JLO64 <64julianlopez@gmail.com> Date: Fri, 23 Jun 2023 17:30:06 -0700 Subject: [PATCH] added bogo sort --- source/algorithms.cpp | 31 +++++++++++++++++++++++++++++++ source/algorithms.hpp | 3 +++ source/menu.cpp | 15 +++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/source/algorithms.cpp b/source/algorithms.cpp index fa763c3..264876c 100644 --- a/source/algorithms.cpp +++ b/source/algorithms.cpp @@ -213,3 +213,34 @@ void selectionSort(void *arg) doneSorting = true; finishSorting(); } + +void bogoSort(void *arg) +{ + bool isSorted = false; + + while (!isSorted) + { + for (unsigned int i = 0; i < arrayLen; i++) + { + swap(i, rand() % arrayLen); + accessElement(i); + ThreadSleep(delayMs); + } + //check if sorted + bool sorted = true; + for (unsigned int i = 0; i < arrayLen - 1; i++) + { + if (array[i] > array[i + 1]) + { + sorted = false; + break; + } + } + if (sorted) + { + break; + } + } + doneSorting = true; + finishSorting(); +} \ No newline at end of file diff --git a/source/algorithms.hpp b/source/algorithms.hpp index f283572..8fad709 100644 --- a/source/algorithms.hpp +++ b/source/algorithms.hpp @@ -27,4 +27,7 @@ void bubbleSort(void *arg); // Selection sort void selectionSort(void *arg); +// bogo sort +void bogoSort(void *arg); + #endif \ No newline at end of file diff --git a/source/menu.cpp b/source/menu.cpp index de7207e..8ece0a2 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -22,7 +22,8 @@ const std::vector ALGO_TEXT = { "Heap Sort", "Quick Sort", "Bubble Sort", - "Selection Sort"}; + "Selection Sort", + "Bogo Sort"}; const std::vector SETTINGS_TEXT = { "Back", @@ -39,7 +40,8 @@ const std::vector DESCRIPTION_TEXT = { " Heap Sort works by building a binary\n heap from the input array and\n repeatedly extracting the maximum\n element from the heap and placing it\n at the end of the sorted array. The\n heap is updated after each extraction\n to maintain the heap property.", " Quick Sort works by selecting a\n pivot element from the array and\n partitioning the other elements into\n two subarrays, according to whether\n they are less than or greater than\n the pivot. The subarrays are then\n sorted recursively.", " Bubble Sort works by repeatedly\n iterating through the array and\n swapping adjacent elements if they\n are in the wrong order. The algorithm\n gets its name from the way smaller\n elements \"bubble\" to the top of the\n array.", - " Selection Sort works by repeatedly\n finding the minimum element from\n unsorted part and putting it at the\n beginning. The algorithm maintains\n two subarrays in a given array.\n The subarray which is already sorted.\n The subarray which is unsorted." + " Selection Sort works by repeatedly\n finding the minimum element from\n unsorted part and putting it at the\n beginning. The algorithm maintains\n two subarrays in a given array.\n The subarray which is already sorted.\n The subarray which is unsorted.", + " Bogo Sort works by repeatedly\n shuffling the array until it is sorted.\n The algorithm is not guaranteed to\n terminate within any finite number\n of steps, but will almost surely\n terminate eventually." }; void Menu::clearConsole() @@ -201,6 +203,15 @@ void algoMenuHandler() printf("\x1b[16;%iH%s\n", (20 - ALGO_TEXT[6].length()/2), ALGO_TEXT[6].c_str()); printf("\x1b[19;1H%s\n", DESCRIPTION_TEXT[5].c_str()); break; + case 7: + if (newArrayOnStart) + { + initArray(); + } + sortThread = threadCreate(bogoSort, NULL, STACKSIZE, prio - 1, 1, false); + printf("\x1b[16;%iH%s\n", (20 - ALGO_TEXT[7].length()/2), ALGO_TEXT[7].c_str()); + printf("\x1b[19;1H%s\n", DESCRIPTION_TEXT[6].c_str()); + break; default: break;