c++ problems with printing bubble, insertion, and selection sort program?

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[i] << ' ';
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
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#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;
}
I only survey your bubble sort, the results are ok if you alternate your Swap function
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//this should pass by reference not by value
template < class ItemType > void Swap(ItemType& r, ItemType& l)
{
  ItemType tmp;
  tmp = r;
  r = l;
  l = tmp;
}

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;
}

void bubbleTest()
{
  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);

  std::copy(int_array, int_array + 10, std::ostream_iterator<int>(cout, ", "));
  cout<<endl;
  std::copy(float_array, float_array + 10, std::ostream_iterator<float>(cout, ", "));
}
Last edited on
I check the insertion sort. I did not using function "insertItem", and directly write code in function "InsertionSort"

this is my code, please check, thanks
1
2
3
4
5
6
7
8
9
10
for(int i=1;i<numValues;i++)
{
	int temp=values[i]; //comparison
	int j;
	for(j=i-1;j>=0 && values[j]>=values[i];j--)
	{
		values[j+1]=values[j]; //copy the array
	}
	values[j+1]=temp; //copy the comparison to the last position which value larger than comparison
}

Topic archived. No new replies allowed.