how can i prent the array after evry pass in bubble sort

My sort works. unfortunately I don't seem to be able to print the array after evry passes, do I need to pass to another 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
using namespace std;
void bubbleSort(int arr[], int n)
{
	bool flag = true; 
	int c = 0;
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				swap(arr[j], arr[j + 1]);
				flag = false;
			}
			c++;
		}
		if (flag == true)
			break;

	}
	cout << "# of rounds :" << c << endl;
}

void bubbleSortRec(int arr[], int n)
{
	  
	if (n == 1)
		return;

for (int i = 0; i < n - 1; i++)
		if (arr[i] > arr[i + 1])
		swap(arr[i], arr[i + 1]);
	
bubbleSortRec(arr, n - 1);
}


void printArray(int arr[], int n)
{
	for (int i = 0; i < n; i++)
		cout << arr[i] << " ";
	cout << endl;
}



int main()
{

	int arr[] = { 10,20,30,40,50};
	int n = sizeof(arr) / sizeof(arr[0]);
	bubbleSort(arr, n);
	printArray(arr, n);
	return 0;

}
try calling print array before the if(flag) in the sort.
If you define printFunction() before you define bubbleSort() you can insert the following at line 17:
std::cout << '\t'; printArray(arr, n); to get an array listing for each run through the bubble sort.

Your initial array is already sorted, so running it through your bubble sort simply runs four times to confirm the array is sorted.

You are recording each iteration through both loops in the bubble sort as a round. Is that really what you want? Shouldn't you document the number of times the entire array is examined instead of each element?

After a bit of a refactor, including separating the function definitions from the declarations and having an initial array that will be sorted (along with some formatting, etc.):
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
#include<iostream>

void bubbleSort(int[], int);
void printArray(int[], int);

int main()
{
   int arr[] = { 40,20,10,50,30 };

   int n = sizeof(arr) / sizeof(arr[0]);

   bubbleSort(arr, n);

   printArray(arr, n);
}

void bubbleSort(int arr[], int n)
{
   bool flag { true };
   int c { };

   for (int i { }; i < n - 1; i++)
   {
      for (int j { }; j < n - i - 1; j++)
      {
         if (arr[j] > arr[j + 1])
         {
            std::swap(arr[j], arr[j + 1]);
            flag = false;
         }
      }

      c++;

      std::cout << '\t';  printArray(arr, n);

      if (flag == true)
      {
         break;
      }

   }
   std::cout << "\n# of rounds: " << c << '\n';
}

void printArray(int arr[], int n)
{
   for (int i = 0; i < n; i++)
   {
      std::cout << arr[i] << " ";
   }

   std::cout << '\n';
}

        20 10 40 30 50
        10 20 30 40 50
        10 20 30 40 50
        10 20 30 40 50

# of rounds: 4
10 20 30 40 50
mick777, I see you are using std::swap, part of <algorithm>. You should include that header instead of relying on it being included in other headers.

Did you know the size of an regular array can be obtained using std::size since C++17?
https://en.cppreference.com/w/cpp/iterator/size

Unfortunately it can't be used to obtain the size of an array after being passed to a function.

I'd recommend using a C++ container, std::vector for example, instead of a regular array whenever possible. A C++ container doesn't devolve to a pointer when passed into a function, a regular array does. A vector also keeps track of its size automatically. Pass a vector to a function and you can retrieve its size. No need to have a size parameter.
Furry Guy , THANK YOU THAT WAS HELPFUL .

I WILL USE C++ container NEXT TIME
I didn't know whether this was a school assignment or not. And if it was just what you were allowed to do.

Using std::vector and a range-based for loop for output of the vector the code could be rewritten as:
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
#include <iostream>
#include <algorithm>
#include <vector>

void bubbleSort(std::vector<int>&);
void printVector(const std::vector<int>&);

int main()
{
   std::vector<int> vec { 40, 20, 10, 50, 30 };

   bubbleSort(vec);

   printVector(vec);
}

void bubbleSort(std::vector<int>& vec)
{
   bool swap_flag { true };
   int  c         { };

   while (swap_flag)
   {
      swap_flag = false;

      for (size_t i = 0; i < vec.size() - 1; i++)
      {
         if (vec[i] > vec[i + 1])
         {
            std::swap(vec[i], vec[i + 1]);

            swap_flag = true;
            std::cout << '\t'; printVector(vec);
            ++c;
         }
      }
   }
   std::cout << "\n# of rounds: " << c << '\n';
}

void printVector(const std::vector<int>& vec)
{
   for (const auto& itr : vec)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';
}

        20 40 10 50 30
        20 10 40 50 30
        20 10 40 30 50
        10 20 40 30 50
        10 20 30 40 50

# of rounds: 5
10 20 30 40 50

This was a quick and dirty rewrite to show how a vector can be used instead of a regular array. With a bit of time and thought it could be polished and tightened up.
One of the bits of polishing and tightening up I'd do is having a template function that allows printing a std::vector to be as easy as an int.

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
#include <iostream>
#include <algorithm>
#include <vector>

void bubbleSort(std::vector<int>&);

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);

int main()
{
   std::vector<int> vec { 40, 20, 10, 50, 30 };

   bubbleSort(vec);

   std::cout << vec << '\n';
}

void bubbleSort(std::vector<int>& vec)
{
   bool swap_flag { true };
   int  c         { };

   while (swap_flag)
   {
      swap_flag = false;

      for (size_t i = 0; i < vec.size() - 1; i++)
      {
         if (vec[i] > vec[i + 1])
         {
            std::swap(vec[i], vec[i + 1]);

            swap_flag = true;
            std::cout << '\t' << vec << '\n';
            ++c;
         }
      }
   }
   std::cout << "\n# of rounds: " << c << '\n';
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      os << x << ' ';
   }

   return os;
}

Certainly not beginner stuff. :)
Using std::sort and a lambda makes for compact 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
25
26
27
28
29
30
31
32
33
34
35
#include <algorithm>
#include <iostream>
#include <vector>

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);

int main()
{
   std::vector<int> vec { 0, 5, 2, 9, 7, 6, 1, 3, 4, 8 };

   size_t compCounter { };

   std::sort(vec.begin(), vec.end(),
             [&compCounter] (int a, int b) noexcept
             {
                ++compCounter;
                return a < b;
             });

   std::cout << "number of comparisons: " << compCounter << '\n';

   std::cout << vec << '\n';
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      os << x << ' ';
   }

   return os;
}

lambdas were introduced in C++11.
Or even as C++20:

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
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
#include <ranges>

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
	std::ranges::copy(v, std::ostream_iterator<T>(os, " "));
	return os;
}

int main()
{
	std::vector vec {0, 5, 2, 9, 7, 6, 1, 3, 4, 8};
	size_t compCounter { };

	std::ranges::sort(vec, [&compCounter](const auto& a, const auto& b) noexcept
	{
		++compCounter;
		return a < b;
	});

	std::cout << "number of comparisons: " << compCounter << '\n';
	std::cout << vec << '\n';
}


Note L16 with no type specified!
Topic archived. No new replies allowed.