-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortBogo.cpp
77 lines (62 loc) · 1.83 KB
/
SortBogo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Bogo Sort Function Header
// Takes in an array and sorts it using a bogo sort algorithm
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <vector>
#include "VECTOR.h"
#include "SortBogo.h"
using namespace std;
// Check if the array is sorted
bool isSorted(NamedVectorObj& vect) {
int n = vect.getSize();
for (int i = 1; i < n; i++) {
if (vect.getIndex(i - 1) > vect.getIndex(i)) {
return false;
}
}
return true;
}
// Shuffling the array
void shuffle(NamedVectorObj& vect) {
int n = vect.getSize();
for (int i = 0; i < n; i++) {
int randomIndex = rand() % 4;
vect.swapIndex(i, randomIndex);
}
}
// Save original cout buffer, global for easy access
streambuf* coutBufBogo = cout.rdbuf();
int i = 0; // counter for number of swaps
void bogoSort(NamedVectorObj& vect, ofstream& logfile) {
// Throw an exception if the array is too large (crashes)
int max = 6;
if (vect.getSize() > max) {
throw max;
}
// Use a flag variable to check if cout has been redirected because of recursion
static bool coutRedirected = false;
// Check if we haven't redirected cout yet
if (!coutRedirected) {
// Redirect cout to logfile
cout.rdbuf(logfile.rdbuf());
coutRedirected = true;
// Log initial array state
cout << "Initial array: ";
vect.printData(vect);
}
// Check if the array is sorted
if (isSorted(vect)) {
cout.rdbuf(coutBufBogo); // Restore the original cout buffer
coutRedirected = false; // Reset the flag
return;
}
else {
shuffle(vect);
// Log the state of the vector after each swap
cout << "After swap " << i << ": ";
vect.printData(vect);
i++;
}
//new skill: recursion
bogoSort(vect, logfile);
}