I have been working on a program that requires me to use bubble,insertion, and selection sort. My program runs without any compiler errors. I have printed out the total number of comparisons but I am having trouble trying to print out the array values for bubble,insertion, and selection sort. I tried different ways to do it but it does not show anything on the compiler screen. I was wondering whether you guys could help me come up with an effective way to print out all the float array and int array values for bubble, insertion, and selection.

At first what I did was something like this in the main:
ex.
BubbleSort(int_array, numValues);

cout << "Here is sorted integer array: ";
for(int i=0; i<10; i++)
cout << float_array << ' ';
cout << endl;

That printed nothing to the output screen.

Any kind of help on how to actually print array values will be very helpful. Thanks

#include <iostream>
using namespace std;

template < class ItemType > void Swap(ItemType r, ItemType l)
{

    ItemType tmp;
    tmp = r;
    r = l;
    l = tmp;
}



template < class ItemType > void SelectionSort(ItemType values[], int numValues)
// Post: The elements in the array values are sorted by key.
{
    int SelComp = 0;
    int endIndex = numValues - 1;
    for (int current = 0; current < endIndex; current++) {
	Swap(values[current], values[MinIndex(values, current, endIndex)]);
	SelComp++;
    }

    cout << "These integers were sorted using Selection Sort and the number of comparisons made were " << SelComp << "." << endl;
}


template < class ItemType > int MinIndex(ItemType values[], int start, int end)
//  Post: Function value = index of the smallest value in
//             values [start]  . . values [end].
{
    int indexOfMin = start;
    for (int index = start + 1; index <= end; index++)
	if (values[index] < values[indexOfMin])
	    indexOfMin = index;
    return indexOfMin;
}

template < class ItemType > void InsertItem(ItemType values[], int startIndex, int endIndex)
// Post: values[0]..values[endIndex] are now sorted.
{
    bool finished = false;
    int current = endIndex;
    bool moreToSearch = (current != startIndex);

    while (moreToSearch && !finished) {
	if (values[current] < values[current - 1]) {
	    Swap(values[current], values[current - 1]);
	    current--;
	    moreToSearch = (current != startIndex);
	} else
	    finished = true;
    }
}

template < class ItemType > void InsertionSort(ItemType values[], int numValues)
{
    int InsertComp = 0;
    for (int count = 0; count < numValues; count++) {
	InsertItem(values, 0, count);
	InsertComp++;
    }
    cout << "These integers were sorted using Insertion Sort and the number of comparisons made were " << InsertComp << "." << endl;

}

template < class ItemType > void BubbleUp(ItemType values[], int start, int end)
//  Post: Neighboring elements that were out of order have been
//            swapped between values [start] and  values [end],
//            beginning at values [end].
{

    for (int index = end; index > start; index--)
	if (values[index] < values[index - 1])
	    Swap(values[index], values[index - 1]);



}

template < class ItemType > void BubbleSort(ItemType values[], int numValues)
//  Post: Sorts array values[0 . . numValues-1 ] into ascending
//            order by key
{
    int BubbleComp = 0;

    int current = 0;

    while (current < numValues - 1) {

	BubbleUp(values, current, numValues - 1);
	current++;
	BubbleComp++;
    }
    cout << "These integers were sorted using Bubble Sort and the number of comparisons made were " << BubbleComp << "." << endl;
}

int main()
{
    int int_array[10] = { 43, 7, 10, 23, 18, 4, 19, 5, 66, 14 };
    float float_array[10] = { 43.2, 7.1, 10.5, 23.9, 18.7, 4.2, 19.3, 5.7, 66.8, 14.4 };
    int numValues = 10;
    cout << "Here is unsorted integer array: ";
    for (int i = 0; i < 10; i++)
	cout << int_array[i] << ' ';
    cout << endl;

    cout << "Here is unsorted double array: ";
    for (int i = 0; i < 10; i++)
	cout << float_array[i] << ' ';
    cout << endl;

    BubbleSort(int_array, numValues);
    BubbleSort(float_array, numValues);
    InsertionSort(int_array, numValues);
    InsertionSort(float_array, numValues);
    SelectionSort(int_array, numValues);
    SelectionSort(float_array, numValues);


    system("pause");
    return 0;
}

The algorithm for displaying an array doesn't change from one scope to another. You already do it in your main() with your (what I will call) "preview" loops. All you have to do is replicate that structure in the appropriate location for where you want the array displayed.

To make it even simpler, you could move the structure to a templated "displayArray()" function and just call that function instead of replicating the structure over and over.

Edited 6 Years Ago by Fbody: n/a

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