I want to put numbers in order without using libraries or functions. I want to do it manually. I'm using arrays and made this test program but it doesn't work. When I run it no numbers print out in sequence. Nothing.
BTW: What function DO you use to sort numbers? Forgot.
No way!! I used the VERY SAME EXACT freaking method as bubble sort. I went to your link and even the example is the same!!! I use a variable called temp to hold value xD.
Really just stop with that attitude...
I know I how to do all the things I do in C++11 ways in the C++98 ways.
All my problem was, was that if statement.
Thanks for the help though.
@cute apple
I was using o just because it was a test program for another program.
BTW I noticed the more functions I use and the the longer an outside function is the more memory it takes. I checked. So I am reluctant to use more than one function for simple things.
If I used multiple functions I would have less mistakes.
> BTW: What function DO you use to sort numbers? Forgot.
std::sort in <algorithm>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <algorithm>
int main()
{
int test[5] {5, 3, 4, 1, 2};
std::sort( test , test + 5 );
for( int i = 0 ; i < 5 ; i++ ) {
std::cout << test[i] << std::endl;
}
return 0;
}
> Why didn't my code work if it was using the same logic?
Infinite loop caused by Line 6.
Your code is not really using the same logic upon closer examination.
For example, you try to set o to zero when o equals 4, but remember that for-loop will increment it to 1 anyways.
#include <iostream>
#include <algorithm>
int main()
{
int test[5] {5, 3, 4, 1, 2};
// If a swap occurred, loop must continue to run
// Initialize the boolean variable to true
bool swapped = true;
// Bubble Sort
for( ; swapped ; ) {
swapped = false;
for( int i = 0 ; i < 4 ; i++ ) {
if( test[i] > test[i + 1] ) {
std::swap( test[i] , test[i + 1] );
swapped = true;
}
}
}
for( int i = 0 ; i < 5 ; i++ )
{
std::cout << test[i] << std::endl;
}
return 0;
}
As you can see, the loop runs at least once even when given numbers that are already sorted (Because the the program must check that no swap occurred before declaring the container completely sorted). The bubble sort algorithm has the worst case complexity of O(n ^ 2) when the last element is the smallest number. So not really suitable for large datasets, but suitable for small sets like your little array of 5.
#include <iostream>
// ((test[0] > test[1]) || (test[1] > test[2]) || (test[2] > test[3]) || (test[3] > test[4]))
// with a loop:
bool isBad( int * arr, int n ) {
for ( int i {1}; i < n; ++i ) {
if ( test[i-1] > test[i] ) returntrue;
}
returnfalse;
}
int main()
{
int test[5] {5, 3, 4, 1, 2};
int temp;
for ( int o{0}; (o < 5) && isBad(test, 5); o++ )
{
if (test[o] > test[o + 1])
{
temp = test[o];
test[o] = test[o + 1];
test[o + 1] = temp;
}
if ( (o == 4) && isBad(test, 5) )
{
o = 0;
}
}
for ( auto x : test ) // range-based for
{
std::cout << x << ", ";
}
return 0;
}
Now we can look at what is bad in:
1 2 3 4 5 6 7 8
for ( int o{0}; (o < 5) && isBad(test, 5); o++ )
{
if (test[o] > test[o + 1])
{
temp = test[o];
test[o] = test[o + 1];
test[o + 1] = temp;
}
What is the value of o during the last iteration? 4
How much is o + 1? 5
What is test[5]? An out of range error. That was one error.
The second error is that at the end of iteration you do reset o to 0 if anything isBad.
The for-loop increments the o to 1. Therefore, only on the first iteration can you swap
something to test[0].
You have marked the thread solved, so I assume that you did figure these out.
Just mentioning for posterity.
BTW: What function DO you use to sort numbers? Forgot.