Number Sort

Pages: 12
Hello Spiderman30,

Let us look at what is happening in the bubble sort function.

Line 24 the for loop starts at zero.

Line 26 you are comparing (array[0] > array[0]) always will be false. Then you add one to count before you leave the if statement. As the for loop starts over you add one to count before checking the condition. Second iteration of the loop you are now comparing (array[2] > array[2]) again false. Add one to count and again in the for loop and you have skipped elements one and three of the array.

I think what you meant to do was "count + 1" not "count +".

I am not familiar with this type of bubble sort, so I will need some time to test it.

Hope that helps,

Andy

P.S. Lone 20 is missing a semicolon.
Last edited on
I got the code to work, I just need to get it to output and add the count

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

const int MAXSIZE{ 8 };

int BubbleSort(int array[], int N);
void SelectionSort(int array[], int size);

int main()
{
	int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
	int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };

	SelectionSort(A, MAXSIZE - 1);
	BubbleSort(B, 8);

}
int BubbleSort(int array[], int N)
{
	int temp;
	bool Swap;
    do
    {
        Swap = false;
        for (int count = 0; count < (N - 1); count++)
        {
            if (array[count] > array[count++])
            {
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                Swap = true;
            }
        }
    }while (Swap);
}
void SelectionSort(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;
	}
}
I tried a bunch of different things to get an output and everything I tried either did not give an output or resulted in an error. This was the last method I tried.

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

const int MAXSIZE{ 8 };

int BubbleSort(int A[], int N);
void SelectionSort(int A[], int Size);

int main()
{
	int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
	int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };

	return SelectionSort(A, MAXSIZE - 1);
	return BubbleSort(B, 8);

}
int BubbleSort(int A[], int N)
{
	int temp;
	bool Swap;
    do
    {
        Swap = false;
        for (int Count = 0; Count < (N - 1); Count++)
        {
            if (A[Count] > A[Count++])
            {
                temp = A[Count];
                A[Count] = A[Count + 1];
                A[Count + 1] = temp;
                Swap = true;
            }
        }
     }while (Swap);
}
void SelectionSort(int A[], int Size)
{
	int startScan, minIndex, minValue;

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

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}
}
Hello Spiderman30,

Line the proto type returns an "int", but the function returns nothing both lines 5 and 17 should be "void" not "int".

Lines 13 and 14 are an improper use of return in addition to this you are trying to return something from a function that returns nothing. Remove the "return"s. line 13 the use of "MAXSIZE - 1" is incorrect. You just need to leave this at 8 here.

Line 24 the "(n - 1)" is OK here because of the use of "Count + 1" in the if statement. This keeps the for loop from going past the end of the array. Also the () are OK, but not needed.

Line 26 you do not want to add one to "Count" here. What you need is "Count + 1".

The rest of the function works as is.

The "SelectionSort" function works. It does not need any changes.

Here is you latest code revised. Read the comments:
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
#include <iostream>

const int MAXSIZE{ 8 };

void BubbleSort(int (&A)[MAXSIZE], int N);  // <--- Changed to void. Passed by reference for testing. Not necessary for normal use, but OK if you leave it.
void SelectionSort(int A[], int Size);

int main()
{
	int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
	int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };

	SelectionSort(A, MAXSIZE);  // <--- Removed - 1 and the return.
	BubbleSort(B, MAXSIZE);  // <--- Removed the return. Should use "MAXSIZE" here.

	//  Added these lines.
	std::cout << "\n Lowest number is: " << A[0] << std::endl;
	std::cout << "\n Lowest number is: " << B[0] << std::endl;  // <--- Because the array is sorted with the lowest in element zero.

	return 0;  // <--- Added this. Not needed, but good programming.
}
void BubbleSort(int (&A)[MAXSIZE], int N)  // <--- Passed by reference for testing. Not necessary for normal use, but OK if you leave it.
{
	int temp;
	bool Swap;
	do
	{
		Swap = false;
		for (int Count = 0; Count < N - 1; Count++)  // <--- () not needed, but OK if you want.
		{
			if (A[Count] > A[Count + 1])  // <--- Changed the "++" to "+ 1".
			{
				temp = A[Count];
				A[Count] = A[Count + 1];
				A[Count + 1] = temp;
				Swap = true;
			}
		}
	} while (Swap);
}

//  This funcction works as is.
void SelectionSort(int A[], int Size)
{
	int startScan, minIndex, minValue;

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

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}
}


Hope that helps,

Andy
It has come to my attention that the question asks for me to sort the array using those functions. Will changing A[0] and B[0] to A[MAXSIZE] and B[MAXSIZE] do that, or are there a series of things that need to be changed up?
Hello Spiderman30,

Will changing A[0] and B[0] to A[MAXSIZE] and B[MAXSIZE] do that

When you refer to "A" and "B" which line or lines are you talking about?

When defining the array what is between the [] needs to be a constant number, in this case it is 8. Defining const int MAXSIZE{ 8 }; solves this problem. Now when you write int A[MAXSIZE] it is the same as int A[8].

In this program defining "MAXSIZE" as a global variable it can be used anywhere in the entire file. It also has the advantage of if you want to change the size of the array you only have to change that value in one place. Then everywhere in the file where "MAXSIZE" is used the new value will be used saving you from going through the whole program making changes.

Defining the arrays as you have will work, but you have no easy way to determine the size of the array without a line of code and some math. Easier to use one variable.

Not only does this make the program easier to write, but easier to change in the future.

Hope that helps,

Andy
I understand what I have to do, but I don't understand exactly how though.
Hello Spiderman30,

Two posts ago I gave yo code that works with comments. So all I can ask is which part do you not understand? Or which part are you having a problem with?

Andy
I don't understand how I'm supposed to change what I have to output a sorted version of the array.
Hello Spiderman30,

What you will need is a for loop to step through each array. This would be done after you sort the arrays. You could print the arrays before they are sorted to show the difference.

The for loop is simple, but if you have any problem let me know.

Andy
This was the last of the few things I attempted, I know it's wrong because of my lack of coding comprehension.

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

const int MAXSIZE{ 8 };

void BubbleSort(int (&A)[MAXSIZE], int N);
void SelectionSort(int A[], int Size);

int main()
{
	int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
	int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };

	SelectionSort(A, MAXSIZE);
	BubbleSort(B, MAXSIZE);

	std::cout << "\n Lowest number is: " << A[0] << std::endl;
	std::cout << "\n Lowest number is: " << B[0] << std::endl;

	return 0;
}
void BubbleSort(int (&A)[MAXSIZE], int N)
{
	int temp;
	bool Swap;
	do
	{
		Swap = false;
		for (int Count = 0; Count < N - 1; Count++)
		{
			if (A[Count] > A[Count + 1])
			{
				temp = A[Count];
				A[Count] = A[Count + 1];
				A[Count + 1] = temp;
				Swap = true;
			}
		}
		for (Swap == true)
		{
		    swap A[Count];
		}
	} while (Swap);
}
void SelectionSort(int A[], int Size)
{
	int startScan, minIndex, minValue;

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

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}
}
Hello Spiderman30,

I think you need to read this:

http://www.cplusplus.com/doc/tutorial/control/#for

Lines 38 - 41: What is this It is not a for loop nor does it make any sense and it is in the wrong place. If you use it in the "BubbleSort" it should be after the do/while loop. Or put in main. With what is there it is hard to figure out what you want to do.

Line 38 is not written correctly, so it is not likely to even work. When I tried it would not even compile.

You have three for loops that do work. That should be enough to figure out how to print the arrays.

Hope that helps,

Andy
I think I might be a little closer with it:

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

const int MAXSIZE{ 8 };

void BubbleSort(int (&A)[MAXSIZE], int N);
void SelectionSort(int A[], int Size);

int main()
{
	int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
	int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };


	for (int i=0; i > 0; i++)
    {
        i <= MAXSIZE;
        SelectionSort(A, MAXSIZE);
        BubbleSort(A, MAXSIZE);
    }
    std::cout << "\n The array: " << A[0] << std::endl;

	return 0;
}
void BubbleSort(int (&A)[MAXSIZE], int N)
{
	int temp;
	bool Swap;
	do
	{
		Swap = false;
		for (int Count = 0; Count < N - 1; Count++)
		{
			if (A[Count] > A[Count + 1])
			{
				temp = A[Count];
				A[Count] = A[Count + 1];
				A[Count + 1] = temp;
				Swap = true;
			}
		}
	} while (Swap);
}
void SelectionSort(int A[], int Size)
{
	int startScan, minIndex, minValue;

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

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}
}
Hello Spiderman30,

Yes a bit closer, but completely wrong. What you want to end up with is:

1
2
3
4
5
6
7
8
std::cout << "\n The array: \n";

for (int i = 0; i < MAXSIZE; i++)
{
	std::cout << A[i] << std::endl;
}

std::cout << "\n Lowest number is: " << A[0] << std::endl;


This would come after the the sort calls and you could use a second for loop for array "B".

Hope that helps,

Andy
I don't quite understand the error with this

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

const int MAXSIZE{ 8 };

void BubbleSort(int (&A)[MAXSIZE], int N);
void SelectionSort(int A[], int Size);

int main()
{
    int N;
    int Size;
	int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
	int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };

     BubbleSort((&A)[MAXSIZE], N);
     SelectionSort(A[MAXSIZE], Size);
     
	std::cout << "\n The array: \n";

for (int i = 0; i < MAXSIZE; i++)
{
	std::cout << A[i] << std::endl;
}

std::cout << "\n Lowest number is: " << A[0] << std::endl;

	return 0;
}
void BubbleSort(int (&A)[MAXSIZE], int N)
{
	int temp;
	bool Swap;
	do
	{
		Swap = false;
		for (int Count = 0; Count < N - 1; Count++)
		{
			if (A[Count] > A[Count + 1])
			{
				temp = A[Count];
				A[Count] = A[Count + 1];
				A[Count + 1] = temp;
				Swap = true;
			}
		}
	} while (Swap);
}
void SelectionSort(int A[], int Size)
{
	int startScan, minIndex, minValue;

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

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}
}
Fixed the error, but I don't understand how I'm not getting any output

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
#include <iostream>
const int MAXSIZE{ 8 };
void BubbleSort(int (&A)[MAXSIZE], int N);
void SelectionSort(int A[], int Size);
int main()
{
    int N;
    int Size;
	int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
	int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };

     BubbleSort((&A)[MAXSIZE], N);
     SelectionSort((&B)[MAXSIZE], Size);

	std::cout << "\n The array: \n";

for (int i = 0; i < MAXSIZE; i++)
{
    BubbleSort((&A)[MAXSIZE], N);
	std::cout << A[i] << std::endl;
}

std::cout << "\n Lowest number is: " << A[0] << std::endl;

	std::cout << "\n The array: \n";

for (int i = 0; i < MAXSIZE; i++)
{
    SelectionSort((&B)[MAXSIZE], Size);
	std::cout << B[i] << std::endl;
}

std::cout << "\n Lowest number is: " << B[0] << std::endl;

	return 0;
}
void BubbleSort(int (&A)[MAXSIZE], int N)
{
	int temp;
	bool Swap;
	do
	{
		Swap = false;
		for (int Count = 0; Count < N - 1; Count++)
		{
			if (A[Count] > A[Count + 1])
			{
				temp = A[Count];
				A[Count] = A[Count + 1];
				A[Count + 1] = temp;
				Swap = true;
			}
		}
	} while (Swap);
}
void SelectionSort(int A[], int Size)
{
	int startScan, minIndex, minValue;

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

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}
}
Hello Spiderman30,

First I will apologize for defining the original and then "BubbleSort" function to pass the array by reference. I should have changed that to be less confusing. I first did that when testing the function so I could see the entire array not just the first element of the array. In a function definition an array that is passed by value generally degrades to a pointer, so when debugging a program all I can see is the first element pointed to in the array. Passing by reference allows me to see every element of the array, but this is not necessary for your program.

In the following code I added some blank lines to make the code easier to read. A good habit to get into.

Line 3 defines MAXSIZE. The reason for doing this here is so the entire file can have access to this variable because it is a global variable. The second reason for doing this here is so that only one place has to be changed to change the size of the array and so you do not have to go through the entire program changing values because the size of the array has changed.

Lines 16 and 17 are the proper way to call the functions. Your way part may work, but using "[MXSIZE]" does not work. The use of the []s is incorrect and not needed in a function call.

The for loops with the function call is unnecessary. you are calling the sort functions eight times sorting a sorted array seven more times than needed. The point of the for loop is to print out the sorted array and nothing else.

Line 42 is the proper way to define the function. Using the pass by reference that I used is not needed. But it is OK if you leave it. It does not make any difference

What you call the variables in the function makes no difference, but it is better to use a name that reflects which array it is using. Looking at your two sort functions one would think that they are sorting the same array, but the function call says different. It still works the way it is because a function can use a different name because it is local to that function. Also different functions can use the same names for the same reason being local to that function.

The following code is your corrected code. I have made notes i the code and commented out what should not be there. I redid the "BubbleSort" function definition and proto type to what it should be just to keep it simple for now. You can delete any commented line if you need to or keep a copy of this for reference.

I would suggest that you understand how this code works before you try changing it trying for something different.

Should you have a need for a search function I would reefer bck to your original "FindIndexOfSmallest" function for ideas.

This code does compile and works. Be sure to read the comments and if you have any questions let me know.

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

const int MAXSIZE{ 8 };

void BubbleSort(int A[], int N);
void SelectionSort(int A[], int Size);

int main()
{
	//int N{};  // <--- Not used in main. Also should initialize the variables. I used {}.
	//int Size{};  // <--- Not used in main. Also should initialize the variables. I used {}.

	int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
	int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };

	BubbleSort(A, MAXSIZE);  // <--- You call the function this way.
	SelectionSort(B, MAXSIZE);  // <--- You call the function this way.

	std::cout << "\n The array: \n";

	for (int i = 0; i < MAXSIZE; i++)
	{
		//BubbleSort((&A)[MAXSIZE], N);  // <--- Do not use in the for loop.
		std::cout << A[i] << std::endl;
	}

	std::cout << "\n Lowest number is: " << A[0] << std::endl;

	std::cout << "\n The array: \n";

	for (int i = 0; i < MAXSIZE; i++)
	{
		//SelectionSort((&B)[MAXSIZE], Size);  // <--- Do not use in the for loop.
		std::cout << B[i] << std::endl;
	}

	std::cout << "\n Lowest number is: " << B[0] << std::endl;

	return 0;
}

void BubbleSort(int A[], int N)  // <--- Proper function definition. "N" equals the value of MAXSIZE.
{
	int temp;  // <--- Should initialize to be safe, but it works.
	bool Swap;  // <--- Should initialize to be safe, but it works.
	do
	{
		Swap = false;
		for (int Count = 0; Count < N - 1; Count++)
		{
			if (A[Count] > A[Count + 1])
			{
				temp = A[Count];
				A[Count] = A[Count + 1];
				A[Count + 1] = temp;
				Swap = true;
			}
		}
	} while (Swap);
}

void SelectionSort(int A[], int Size)  // <--- Size equals the value of MAXSIZE.
{
	int startScan, minIndex, minValue;

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

		for (int index = startScan + 1; index < Size; index++)
		{
			if (A[index] < minValue)
			{
				minValue = A[index];
				minIndex = index;
			}
		}

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}
}


Hope that helps,

Andy
Topic archived. No new replies allowed.
Pages: 12