Skip to content

Commit

Permalink
Merge pull request #9 from JLO64/main
Browse files Browse the repository at this point in the history
added bogo sort
  • Loading branch information
memeToasty authored Jun 24, 2023
2 parents 951f5e7 + bf21808 commit 8b8503d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
31 changes: 31 additions & 0 deletions source/algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
3 changes: 3 additions & 0 deletions source/algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ void bubbleSort(void *arg);
// Selection sort
void selectionSort(void *arg);

// bogo sort
void bogoSort(void *arg);

#endif
15 changes: 13 additions & 2 deletions source/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const std::vector<std::string> ALGO_TEXT = {
"Heap Sort",
"Quick Sort",
"Bubble Sort",
"Selection Sort"};
"Selection Sort",
"Bogo Sort"};

const std::vector<std::string> SETTINGS_TEXT = {
"Back",
Expand All @@ -39,7 +40,8 @@ const std::vector<std::string> 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()
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8b8503d

Please sign in to comment.