here is my current code:

#include <iostream>
using namespace std;

int bubbleSort(int [], int);
int selectionSort(int [], int);
void showArray(int [], int);
const int SIZE = 20;

int main()
{
	//The array with unsorted numbers
	int numbers[SIZE] = {23, 44, 2, 6, 15, 70, 45, 80, 46, 30,
						0, 11, 89, 53, 78, 77, 90, 72, 52, 64};
	int results1;		//loops for bubbleSort
	int results2;		//loops for selectionSort

	//display the values
	cout << "The unsorted values are:\n";
	showArray(numbers, SIZE);

	//sort with the bubble method
	results1 = bubbleSort(numbers, SIZE);

	//sort with the selection method
	results2 = selectionSort(numbers, SIZE);

	cout << "With the bubble sort method it took " << results1 << " loop(s) to sort the numbers.\n";
	cout << "With the selection sort method it took " << results2 << " loop(s) to sort the numbers.";
	return 0;
}

//*****************************************************************
//bubbleSort takes in the array numbers and sorts the numbers from*
//smallest to the largest. The number returned will be the number *
//of times it had to loop to sort the numbers.					  *
//*****************************************************************

int bubbleSort(int array[], int size)
{
	bool swap;
	int temp;
	int count;

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

//*********************************************************************
//selectionSort takes in the array numbers and sorts the numbers from *
//smallest to the largest. The number returned will be the number     *
//of times it had to loop to sort the numbers.					      *
//*********************************************************************


int selectionSort(int array[], int size)
{
	int startScan, minIndex, minValue, index;

	for (startScan = 0; startScan < (size - 1); startScan++)
	{
		minIndex = startScan;
		minValue = array[startScan];
		for(index = startScan + 1; index < size; index++)
		{
			if (array[index] < minValue)
			{
				minValue = array[index];
				minIndex = index;
			}
		}
		array[minIndex] = array[startScan];
		array[startScan] = minValue;
	}
	startScan++;
	return startScan;
}

void showArray(int array[], int size)
{
	for (int count = 0; count < size; count++)
		cout << array[count] << " ";
	cout << endl;
}

during selectionSort() how many times it that suppose to loop? when i run the code it gives me a value of 20, but i had the understanding that selcetion took fewer loops than bubble yet i get the same value for both.

Selection sort should loop until it reaches the end of the list. It will start at the first element and find the smallest value in the rest of the list and swap their positions in the list. Then move to the second element, find the smallest value remaining in the list and swap them. This should continue until the end of the list is reached.

This article has been dead for over six months. Start a new discussion instead.