Putting Numbers In Order

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
int main()
{
	int test[5] {5, 3, 4, 1, 2};
	int temp;
	for (int o{0}; (o < 5) && ((test[0] > test[1]) || (test[1] > test[2]) || (test[2] > test[3]) || (test[3] > test[4])); o++)
	{
		if (test[o] > test[o + 1])
		{
			temp = test[o];
			test[o] = test[o + 1];
			test[o + 1] = temp;
		}
		if ((o == 4) && ((test[0] > test[1]) || (test[1] > test[2]) || (test[2] > test[3]) || (test[3] > test[4])))
		{
			o = 0;
		}
	}
	for (int n{0}; n < 5; n++)
	{
		std::cout << test[n] << ", ";
	}
	return 0;
}


Supposed to print out "1, 2, 3, 4, 5".
Last edited on
@gentleguy

It's supposed to print out the numbers in sequencial order.
In this case 1, 2, 3, 4, 5. The starting order was 5, 4, 3, 2, 1.

Just read the logic in the code. I can't find my mistake.
Last edited on
@gentleguy

Yes I know but I don't want to. I'm doing this to challenge myself.

Please check my code?


EDIT: Lmao! I was using the same method as bubble sort!
Last edited on
@gentleguy

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.

EDIT: Bubble Sort is just a method...
Last edited on
@gentleguy

Why didn't my code work if it was using the same logic?

EDIT: That long line compares if the numbers adjacent to each other are in order.
Last edited on
@gentleguy

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.
Last edited on
@cute apple

Yes a simple mistake. If it gets reset to 0 then when the loop repeats o will be 1.
@gentleguy

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.
Last edited on
> 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.

Here is the bubble sort
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
#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.
Last edited on
The "freaking" is not the problem, so I factorized it to a separate function:
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
#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] ) return true;
  }
  return false;
}

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.

There is std::sort. See http://www.cplusplus.com/reference/algorithm/sort/


The "gentleguy" shows rather trollish behaviour. Ignore.
Last edited on
Took the liberty of fixing your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
int main()
{
	int test[5] {5, 3, 4, 1, 2};
	int temp;
	for (int o{0}; o < 5 ; o++)
	{
		if (test[o] > test[o + 1])
		{
			temp = test[o];
			test[o] = test[o + 1];
			test[o + 1] = temp;
		}
		if ((o == 4) && ((test[0] > test[1]) || (test[1] > test[2]) || (test[2] > test[3]) || (test[3] > test[4])))
		{
			o = -1;
		}
	}
	for (int n{0}; n < 5; n++)
	{
		std::cout << test[n] << ", ";
	}
	return 0;
}


Got rid of unnecessary logical expression at Line 6
Changed o = 0; to o = -1; at Line 16
Last edited on
@gentleguy

It's getting obvious you're being a troll. Others are picking up on it.

Who lies about a stranger being their family member?
Topic archived. No new replies allowed.