I have a function which basically sorts an array of 0's and 1's.
The array initially starts off as: 00110011
and I want the array to be sorted to look like: 00001111
One of my functions does this sorting through the lawnmower algorithm which is a basic sequential sort, but when it reaches the end of the array, it'll begin the sorting again immediately from right to left like a wrap around.
My problem is this:
I have a simple variable called passes, and this variable increments whenever a sort happens, so it just simulates how many "passes" the sorting algorithm needs to successfully sort this array
The problem I have is that, this passes will always say 0 instead of like 2 or 3.
> sorts an array of 0's and 1's.
a simply count may suffice.
> sorted == false
comparing a bool will yield a bool. To be consistent, you ought to write (sorted==false) == true == true == true ... ad infinitum
> if (total == 0) //check to see if half of disks are black
that makes no sense, it is not a prerequisite that the array should be filled with equal amount of 0 and 1
> cout << "Disks succesfully alternated!" << endl;
your function shouldn't print anything
1 2 3
for (int i = 0; i < size; i++)
{
if (disk[i] == 1 && disk[i + 1] == 0)
out of bounds, undefined behaviour.
1 2 3 4
if (i == size - 1) //useless
{
for (int j = size; j > 0; j--) { //this shouldn't be inside the other loop
if (disk[i] == 0 && disk[i - 1] == 1) // checking the same elements over and over again
I fixed the issue, my array was already sorted from calling a previous sort function. I just had to re-populate the array to begin sorting with my lawnmower function