Help with integer array bubble sort

Hi, everyone, this is my first time posting here, and I was hoping to get some advice for a homework assignment I have.
My assignment is to make a bubble sort array in ascending order using user input for up to 20 integers.

so far I've been pretty good writing the code up to this point, using google and other resources. But now I'm lost as to why my code is not actually sorting the array when it is run and all the values are put in by the user.

I would really appreciate it if anyone could give me a nudge in the right direction or a link to a good tutorial on this kind of stuff. I don't want anyone to just solve it for me as that won't help me learn what I'm doing wrong. thanks in advance

Main.cpp
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
  #include <iostream>
#include<string.h>
#include <stdlib.h>

#include"bubble.h"

using namespace std;

int main()
{
	const int max_size =20;
	int numbers[max_size];
	int input;
	int output;
	int i;
	
	cout << "Enter 20 whole numbers for an array! " << endl;
	for (input = 0; input < 20; input++)
	{
		cin >> numbers[input];
	}

	cout << "You entered the numbers: " << endl;
	for (output = 0; output < 20; output++)
	{
		cout << numbers[output];
	}

	cout << "Your sorted numbers are: " << endl;
	int bubblesort(numbers[max_size]);

	
	for (i = 0; i < 20; i++)
	{
		cout << numbers[i];
	}
	

}


bubble.h
1
2
3
4
5
6
7
#ifndef BUBBLE_H
#define BUBBLE_H

void bubblesort(int numbers[], int max_size);


#endif 


bubble.cpp
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
#include "bubble.h"
#include<string.h>
#include<iostream>

void bubblesort(int numbers[],int max_size)
{
	int j;
	int Num;
	char temp;
	bool IsSorted;

	
	do {
		IsSorted = true;
		Num--; // tells the loop that the last item does not need to be looked at again since that item is already sorted from the previous loop
		for (j = 0; j < Num; j++) //loops each time for how many elements of the aray we have
		{
			if (numbers[j] > numbers[j + 1]) 
			{
				temp = numbers[j]; 
				numbers[j] = numbers[j + 1]; 
				numbers[j + 1] = temp;
				IsSorted = false;
				
			}
			else;
		}
	} while (!IsSorted); //as long as IsSorted is false it will loop and swap
	
}

Last edited on
num isn't initialized anywhere.

not a major problem but its <cstring> and <cstdlib>, you are using the C versions.


Last edited on
Topic archived. No new replies allowed.