Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
So, the algorithm works by sorting in the end, but not ouputting correctly line by line. pls help, i do not understand what is wrong with my code.

What I have tried:

#include <iostream>

using namespace std;
int main() {
	int temp;
	int end = 10;//each time loop completes for i,the end will have max val

	int arr[] = { 44,9,237,1,5,44,534,17,21,819 };
	for (int m = 0; m < 10; m++) {
		for (int i = 0; i < end; i++) {

			if (arr[i] > arr[i + 1]) {
				temp = arr[i + 1];
				arr[i + 1] = arr[i];
				arr[i] = temp;
			}
			for (int index = 0; index < 10; index++) {
				cout << arr[index] << endl;

			}

		}
		end--;
	}

	
}
Posted
Updated 18-Nov-18 21:10pm
Comments
KarstenK 19-Nov-18 2:26am    
what about using the deugger and working with some simple test data.

Because you are comparing element at index i and the next one
C++
if (arr[i] > arr[i + 1]) {

you need to pay attention to the end value of loop. i+1 must not go beyond the end of arr.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
I think your loops should be written like this:
C++
#define CountOf(a)   sizeof(a)/sizeof(a[0])
const int arraySize = CountOf( arr );
for( int m = 0; m < arraySize - 1; ++m )
{
   for( int i = m + 1; i < arraySize; ++i )
   {
   }
}
The outer loop stops one from the end because the inner loop starts one past the outer and goes to the end. Also, there is a swap function in the std namespace that is very handy. Your loops could be written like this:
C++
#define CountOf(a)   sizeof(a)/sizeof(a[0])
const int arraySize = CountOf( arr );
for( int m = 0; m < arraySize - 1; ++m )
{
   for( int i = m + 1; i < arraySize; ++i )
   {
      if( arr[m] > arr[i] )
          std::swap( arr[m], arr[i] );
   }
}
I used the CountOf macro so you can change the size of the array and no other code has to change. It adjusts itself automatically. There is no need for the end variable and I would use a separate loop after the sort to display the array.
 
Share this answer
 
v4
Comments
Patrice T 19-Nov-18 0:10am    
I fear your code is not bubble sort anymore.
Try
C++
#include <iostream>
#include <array>

using namespace std;

int main() 
{
  array<int, 10> arr{ 44,9,237,1,5,44,534,17,21,819 };

  for ( size_t m = 0; m < arr.size() - 1; ++m)
  {
    for ( size_t n = m + 1; n < arr.size(); ++n)
    {
      if ( arr[m] > arr[n] )
        swap( arr[m], arr[n]);
      for (auto x : arr)
        cout << x << ", ";
      cout << "\n";
    }
  }
}
 
Share this answer
 
Comments
Patrice T 19-Nov-18 4:40am    
I fear your code is not bubble sort anymore.
CPallini 19-Nov-18 6:31am    
Why not?
Patrice T 19-Nov-18 8:40am    
Bubble sort compares adjacent pairs.
if (arr[i] > arr[i + 1]) {

Bubble sort - Wikipedia[^]
CPallini 19-Nov-18 9:24am    
I stand corrected, you are right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900