Need a little assistance counting a sort

I am writing a program that will sort two separate arrays of 20 integers one as a bubble sort and the other as a selection sort. The program works great but, now I need to add in a way to count the number times the exchange happens and also display that.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  #include<iostream>

using namespace std;

void bubbleSort(int [], int);
void bubbleShow(const int[], int);
void selectSort(int [], int);
void selectShow(const int[], int);

int main()
{
	const int SIZE = 20;
	int values[SIZE] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,10,11};
	int values2[SIZE] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,10,11};

	cout<<"The unsorted values of the Bubble sort array are\n";
	bubbleShow(values, SIZE);

	bubbleSort(values, SIZE);

	cout<<"The sorted values of the Bubble sort array are\n";
	bubbleShow(values, SIZE);

	cout<<"The unsorted values of the Selection sort array are\n";
	selectShow(values2, SIZE);

	selectSort(values2, SIZE);

	cout<<"The sorted values of the Selection sort array are\n";
	selectShow(values2, SIZE);

	system("pause");
	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

void bubbleSort(int array[], int size)
{
	bool swap;
	int temp;

	do
	{
		swap=false;
		for(int count =0; count < (size -1); count++)
		{
			if (array[count] > array[count +1])
			{
				temp = array[count];
				array[count] = array[count +1];
				array[count+1] = temp;
				swap = true;
			}
		}
	}
	while (swap);
}

/////////////////////////////////////////////////////////////////////////////////////////////

void bubbleShow(const int array[], int size)
{
	for(int count = 0; count < size; count++)
			
		cout<<array[count]<<" ";
	cout<<endl;
}

/////////////////////////////////////////////////////////////////////////////////////////////

void selectSort(int array[], int size)
{
	int startScan, minIndex, minValue;

	for (startScan =0; startScan< (size -1); startScan++)
	{
		minIndex = startScan;
		minValue = array[startScan];
		for(int index = startScan +1; index <size; index++)
		{
			if (array[index] <minValue)
			{
				minValue = array[index];
				minIndex = index;
			}
		}
		array[minIndex] = array[startScan];
		array[startScan] = minValue;
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////

void selectShow(const int array[], int size)
{
	for (int count = 0; count < size; count++)
		cout<< array[count] <<" ";
	cout<<endl;
}
Last edited on
add a count to your swap functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int bubbleSort(int array[], int size)
{
        int count = 0;  //here
	bool swap;
	int temp;

	do
	{
		swap=false;
		for(int count =0; count < (size -1); count++)
		{
			if (array[count] > array[count +1])
			{
                                count++;
				temp = array[count];
				array[count] = array[count +1];
				array[count+1] = temp;
				swap = true;
			}
		}
	}
	while (swap);
        return count;
}

Remember that the prototype has changed from (void to int)
So turn both my sorts from voids into int and then add in the counters?
Since count i already initiated inside the for loop should I use a different variable?
Last edited on
So turn both my sorts from voids into int and then add in the counters?
YES
Since count i already initiated inside the for loop should I use a different variable?
NO.

Since count i already initiated inside the for loop should I use a different variable?

Yes. You need to use different variables for the for loop and for counting the number of times you swap. I suggest you change the for loop to use i, simply because it's common.
To follow up on dhayden's response to shadowCode, do you see where you are trying to use two different variables with the same name?

You do need a different variable to count a different thing. The 'count' on line 14 should be the same 'count' as line 3 (but it isn't, because a loop 'count' on line 10 eclipses it).

The loop does the sorting.

The 'number of swaps count' counts the number of times a swap was made.

@guglaak
Remember that in selection sort you need to swap values. Right now you are finding a small value and simply copying it over the older value.

Hope this helps.
Thanks to everyone for the help!
Topic archived. No new replies allowed.