Trouble with bubble sort and pointers

In the code below, everything I've done is working as expected, except for the
function named selectionSort. With that function, it is my intent to use pointers to access the array "rents", and sort the values in ascending order. I'll be honest, I don't really understand the bubble sort algorithm very well, nor do I have a very good grasp on pointers yet.

When I run the program, enter in values for rents, and then select 3 from the menu to sort them and display them, all I get is the values entered in the exact same order that I typed them.

P.S. I've seen in various posts where it's been mentioned the faults of system("pause"), having namespace up at the top etc.. This is in no way production level code.

Any help is much appreciated.
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 #include <iostream>

using namespace std;

int getMenuItem();
void enterRents(int*, int);
void displayRents(int*, int);
int sumRents(int*, int);
void displayMemoryLocations(int*, int);
void selectionSort(int*, int);

int main()
{
	int size = 7;
	bool loop = true;
	int rents[7] = { 1200, 740, 660, 1000, 550, 920, 1350 };
	int *ptr = rents;

	while (loop)
	{
		int choice = getMenuItem();

		switch (choice)
		{
		case 1: enterRents(ptr, size);
			break;

		case 2: displayRents(ptr, size);
			break;

		case 3: selectionSort(ptr, size);
			break;

		case 4: sumRents(ptr, size);
			break;

		case 5: displayMemoryLocations(ptr, size);
			break;

		case 6: cout << "Exiting program..." << endl;
			loop = false;
			break;
		default: cout << "You didn't enter a valid 1 - 5 choice. Please try again" << endl;
			break;
		}
	}

	system("pause");
	return 0;
}

//Function definitions

//Brings up menu, prompts for selection, validates, and returns choice
int getMenuItem()
{
	int selection;
	cout << endl << "Sinclair Property Management Company" << endl << endl;
	cout << "Select from the following menu:" << endl;
	cout << "1: Enter rent amounts" << endl;
	cout << "2: Display Rents" << endl;
	cout << "3: Sort rent amounts" << endl;
	cout << "4: Total all rents" << endl;
	cout << "5: Display rent array memory locations" << endl;
	cout << "6: Exit Program" << endl;
	cout << "Enter a  number selection 1 - 5: ";
	cin >> selection;
	
	return selection;
}

//input rent amounts in an array
void enterRents(int *ptr, int size)
{
	cout << endl << "Enter the rent amounts." << endl;

	for (int i = 0; i < size; i++)
	{
		cout << "Rent " << i + 1 << ": ";
		cin >> *ptr;
		ptr++;
	}
}

//displays the rent amounts
void displayRents(int *rent, int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << "Property " << i + 1 << ": " << *rent << endl;
		rent++;
	}
}

//add the rent values together
int sumRents(int *ptr, int size)
{
	int total = 0;
	for (int i = 0; i < size; i++)
	{
		total = total + *(ptr);
		ptr++;
	}
	cout << total << endl;
	return 0;
}

//displays the memory location of array items
void displayMemoryLocations(int *ptr, int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << ptr << endl;
		ptr++;
	}
}

//sort rents in ascending order THIS IS THE FUNCTION IN QUESTION****
void selectionSort(int *ptr, int size)
{
	bool swap;
	int temp;

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

	
	for(int i = 0; i < size; i++)
	{
		cout << *ptr << endl;
		ptr++;
	}
	
}
> function named selectionSort. (...) I'll be honest, I don't really understand
> the bubble sort algorithm very well
¿do you want «selection sort» or «bubble sort»?

selection sort:
Say that you want to sort the elements of container `A'
Create an empty container `B'. At the end of the algorithm, B will be sorted.
At each step you remove the smallest element from `A' and put it at the end of B.
Repeat until `A' is empty.
note that at each step the element extracted is bigger than the last one, so putting it at the end of B would give you the sequence sorted.
(it may be implemented with only one container, but I think this one may be easier to understand)

bubble sort:
at each step you traverse the container comparing two adjacent elements, if they are not in order (the left is bigger than the right) you swap them.
repeat until `A' is sorted.
note that each swap would fix the relative order of two elements. So after comparing all against all, the container would end sorted.

After you understand the algorithm, let's talk about the implementation.



> it is my intent to use pointers to access the array "rents" (...)
> nor do I have a very good grasp on pointers yet.
lets leave that for later. Do it with array notation if you find it easier.
I guess that you don't intend to translate v[K] -> *(v+K), but threat it as forward iterators.

To traverse a container
1
2
3
4
5
6
7
//range [)
int *begin = array;
int *end = array+size;

for(int *current = begin; current not_eq end; ++current){
	//...
}
++current would be «advance to the next element»
*current would be «access the element»
My apologies. The algorithm I'm attempting is the bubble sort and not selection sort, so sorry about the confusion in the function naming convention. It's terrible. You mentioned trying to do this with array notation. The code in the selectionSort function at one time was done in that manner and worked perfectly. It's the converting it over to pointer notation that is giving me the difficulty.

I think what you mean by array notation is like this, correct?
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
void selectionSort(int rent[], int size)
{
	bool swap;
	int temp;

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

	for (int i = 0; i < size; i++)
	{
		cout << rent[i] << endl;
	}
}
Last edited on
yes, that seems fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	do
	{
		swap = false;
		for (int count = 0; count < (size - 1); count++) //traverse the array
		{
			if (rent[count] > rent[count + 1]) //compare adjacent elements
			{
				temp = rent[count]; //swap the elements
				rent[count] = rent[count + 1];
				rent[count + 1] = temp;
				swap = true;
			}
		}
	}while (swap); //check if the list is sorted 

now look at the code with pointer notation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	do
	{
		swap = false;
		for (int count = 0; count < (size - 1); count++) //¿?
		{
			if (*ptr > *(ptr + 1)) //compare adjacent elements
			{
				temp = *ptr;
				*ptr = *(ptr + 1);
				*(ptr + 1) = temp;
				swap = true;
				ptr++; //¿?
			}
		}
	} while (swap); //check if the list is sorted 
suppose that the if condition fails, you'll keep comparing the same elements over and over again.
suppose that the condition succeed, you'll keep advancing the pointer, and you'll go over the range of the array.

so two things to fix:
- ptr++ should be outside the conditional
- ptr should reset to the beginning of the array for the next traversal

1
2
3
4
5
6
7
8
9
void selectionSort(int array[], int size){
	//...
		for(int *ptr = array; ptr not_eq array+size-1; ++ptr){
			if(*ptr < *(ptr+1){
				//swap
			}
		}
	//...
}
note that `ptr' would make the same work that `count' was doing on the "array code"
Hmmm... So what I am now wondering is what other strategy I could use to do your same approach because in my classes, I am limited to what we have learned so far, and I see not_eq in your alteration. Would that be as simple as !=.. or no? I can't use what you did as I'm not introduced to it yet. Some of the many qualms of these college courses. There are so many ways to answer a question, a handful of ways to efficiently answer a question, yet usually 1 or if you are lucky, 2 that a professor wants to see it done as lol.

In the meantime, this is what I have come up with that works:

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
void selectionSort(int *ptr, int size)
{
	int *i, *j, swap;
	int *end = NULL;

	if (size < 2 || ptr == NULL)
		return;

	end = ptr + size - 1;

	for (i = ptr; i < end; i++)
	{
		for (j = i + 1; j <= end; j++)
		{
			if (*j < *i)
			{
				swap = *i;
				*i = *j;
				*j = swap;
			}
		}
	}
	//jkh
	for (int i = 0; i < size; i++)
	{
		cout << *ptr << endl;
		ptr++;
	}
}
Last edited on
not_eq is semantically equivalent to !=.
however, that's not bubble sort.
1
2
3
4
5
6
7
8
9
		for (j = i + 1; j <= end; j++)
		{
			if (*j < *i)
			{
				swap = *i;
				*i = *j;
				*j = swap;
			}
		}
that would put the minimum element of the range [i+1; end] on the position pointed by `i'
it's actually a swap-a-lot version of selection sort.
Note that you are not comparing adjacent elements, but `i' against all the others.


by the way, you should limit the scope of your variables.
Topic archived. No new replies allowed.