I'm writing a program that is supposed to search an array between the values of -1000 and 1000, as well as sort it using the 3 different sorts (bubble, insertion, selection). I have the searching part done, but my bubble sort isn't working right. It seems right in my head, so I'm not sure what's wrong. It doesn't seem to be outputting the sorted array either.

// sorts.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

void search(int, int[], int, bool);
void selection(int[], int);
void bubble(int[], int);
void insertion(int[], int);
void reset(int[], int);

int _tmain(int argc, _TCHAR* argv[])
{
	int choice;
	int number;
	bool cont = true;
	int arr[10]; //= {49, 100, 2, 503, 203, -504, 23, 75, 900, -4};
	const int min = -1000;
	const int max = 1000;
	do 
	{
		cout << "The collection: " << endl;
	
		for (int i = 0; i < 10; i++)
			{
				arr[i] = rand() % (max - min) + min;
				if (i == 5)
					cout << endl;
				cout << arr[i] << "\t";
			}
		cout << endl;
	
		cout << "(1) Search the array" << endl;
		cout << "(2) Sort with the Selection Sort" << endl;
		cout << "(3) Sort with the Bubble Sort" << endl;
		cout << "(4) Sort with the Insertion Sort" << endl;
		cout << "(5) Reset the collection" << endl;
		cout << "(6) Quit" << endl;
		cin >> choice;

		switch (choice)
		{
		case 1: 
			//search the collection
			cout << "Which number would you like to search for? " ;
		cin >> number;
		search(number, arr, 10, false);

		system("pause");
		system("cls");
			break;
		case 2: 
			//sort with the selection sort
			//selection(arr, 10);
			break;
		case 3: 
			//sort with the bubble sort
			bubble(arr, 10);
			break;
		case 4:
			//sort with the insertion sort
			//insertion(arr, 10);
			break;
		case 5: 
			//reset the collection
			//reset(arr, 10);
			break;
		default:
			//quit
			cont = false;
			
			return 0;
			break;
		}
	} while (cont = true);
	system("pause");
	return 0;
}

void search(int number, int arr[], int size, bool found)
{
	
	for (int i = 0; i < 10; i++)
			{
				if (arr[i] == number)
				{
					found = true;
				}
			}
			if (found)
			{
				cout << "This number is in the collection!" << endl;
			}
			else 
				cout << "This number is not in the collection!" << endl;
}

void selection(int arr[], int size)
{
	
}

void bubble(int arr[], int size)
{
	int storage;
	for (int i = 0; i < size - 1; i++)
	{
		if (arr[i + 1] < arr[i])
		{
			storage = arr[i];
			arr[i] = arr[i + 1];
			arr[i + 1] = storage;
		}
		cout << arr[i] << "\t";
		if (i == 5)
			cout << endl;
	}
	cout << endl;
}

void insertion(int arr[], int size)
{
	 
}

There are two loops in the bubble sort. With one loop, essentially, you're only doing one pass, sorting the highest value to the top, but neglecting the others.
This may prove useful: http://www.codecodex.com/wiki/Bubble_sort


Also, you may want to change that i==5 to i==4. When i equals 5, you'll be starting your second line, but you're putting in the line break after you've printed arr[5]. Since you 10-element array goes from 0...9, you'll be printing something like
0 1 2 3 4 5
6 7 8 9

Hope this was helpful.

Happy coding!

Edited 4 Years Ago by DeanMSands3: n/a

There are two loops in the bubble sort. With one loop, essentially, you're only doing one pass, sorting the highest value to the top, but neglecting the others.
This may prove useful: http://www.codecodex.com/wiki/Bubble_sort


Also, you may want to change that i==5 to i==4. When i equals 5, you'll be starting your second line, but you're putting in the line break after you've printed arr[5]. Since you 10-element array goes from 0...9, you'll be printing something like
0 1 2 3 4 5
6 7 8 9

Hope this was helpful.

Happy coding!

Thanks! I see what you mean about changing i, and I haven't gotten a chance to look at the link fully yet, but it seems to have all the information that i'll need!

I have the searching part done, but my bubble sort isn't working right. It seems right in my head, so I'm not sure what's wrong.

Then get it out of your head and on your desk with pencil and paper. The mind is a very bad analog for the computer.

When programming, the last thing you do is type the code in. Before that you need to sit at a desk and write down your steps, eventually convert that to code, and execute that code line by line on paper to iron out the details and problems you forgot about.

Once it looks good on paper, that's the time to type the program in.


You'll thank me later... :icon_wink:

Then get it out of your head and on your desk with pencil and paper. The mind is a very bad analog for the computer.

When programming, the last thing you do is type the code in. Before that you need to sit at a desk and write down your steps, eventually convert that to code, and execute that code line by line on paper to iron out the details and problems you forgot about.

Once it looks good on paper, that's the time to type the program in.


You'll thank me later... :icon_wink:

I got the whole thing written today, however, there's one thing that I'm not sure of. How exactly can I pass a variable from one function to main? I don't have my code as an example at the moment, but I'll have that later if you need it.

Either use a return value or a pass-by-reference parameter.

//Using a return value
int getLength(char *myString){
    int length;
    for(length=0;myString[length]!=0;length++){}
    return length;
}
//Passing by reference
void getLength(char *myString, int &length){ //notice the '&' before length
    for(length=0;myString[length]!=0;length++){}
}

Edited 4 Years Ago by DeanMSands3: n/a

This question has already been answered. Start a new discussion instead.