Arrays/Pointers/Functions

I would like to pass the first array into both of the functions. Each function sorts the array. One uses bubble sort, and the other uses selection sort. However, the array is already sorted (by the first function - bubble sort) when it is passed to the second function (selection sort). I'm trying to compare how efficient each way of sorting is. (i.e how many comparisons and exchanges each one makes when sorting the values in the array)

My question would be... How do I reinitialize the values in the array, so the same values (in the same order) are sent to the second function? Would I use a pointer? If so, how?

Here is my 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
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
#include <iostream>
using namespace std;

void bubbleSort(int[], int, int &, int &);
void selectionSort(int[], int, int &, int &);

int main()
{
	const int SIZE = 20;
	int comparisons_bubble = 0;
	int interchanges_bubble = 0;
	int comparisons_selection = 0;
	int interchanges_selection = 0;

	int values_case1[SIZE] = { 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0 };
	int values_case2[SIZE] = { 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95 };
	int values_case3[SIZE] = { 40, 70, 20, 15, 5, 65, 80, 85, 0, 95, 25, 35, 30, 60, 45, 10, 50, 55, 90, 75 };

	bubbleSort(values_case1, SIZE, comparisons_bubble, interchanges_bubble);

	selectionSort(values_case1, SIZE, comparisons_selection, interchanges_selection);
}

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

	cout << "values_case1 before bubble sort is:";
	for (int counter = 0; counter < size; counter++)
		cout << array[counter] << " ";
	cout << endl;

	do
	{
		swap = false;

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

	cout << "values_case1 after bubble sort is:";
	for (int counter = 0; counter < size; counter++)
		cout << array[counter] << " ";
	cout << endl;

	cout << "comparisons_bubble is " << comparisons_bubble << endl;
	cout << "interchanges_bubble is " << interchanges_bubble << endl;
}

void selectionSort(int array[], int size, int &comparisons_selection, int &interchanges_selection)
{
	int startScan, minIndex, minValue;

	cout << "values_case1 before selection sort is:";
	for (int count = 0; count < 20; count++)
	{
		cout << array[count] << " ";
	}
	cout << endl;

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

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

	cout << "comparisons_selection is " << comparisons_selection << endl;
	cout << "interchanges_selection is " << interchanges_selection << endl;

	cout << "values_case1 after selection sort is:";
	for (int count = 0; count < 20; count++)
	{
		cout << array[count] << " ";
	}
	cout << endl;
}
1
2
3
int tmp_values[SIZE];
memcpy(tmp_values, values_case1, SIZE * sizeof(int));
bubbleSort(tmp_values, SIZE, comparisons_bubble, interchanges_bubble);
Topic archived. No new replies allowed.