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)
{
}