Insertion swap not swapping

I'm trying to pass an array into insertion sort to swap it, but I always get zero swaps when I run it. (This is not the same as my searching benchmark program).


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
126
127
128
129
130
131
132
133
134
135
136
137
138
void insertionSort (int arr[], int SIZE)
#include <iostream>

using namespace std;

const int SIZE=20;

void bubbleSort(int numbers[], int SIZE);
void selectionSort(int numbers[], int SIZE);
void insertionSort(int numbers[], int SIZE);

int main()
{
int numbers[SIZE]= {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};
int arr[SIZE]= {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};

int value=0;
bool found;

cout << "Today we are going to be searching for values." << endl;
cout << "These are the values you have to choose from" << endl;

for (int i=0; i<SIZE; i++)
cout << numbers[i]<<"; ";

do
{
cout << "Make sure to enter a value that's in the list." << endl;
cin >> value;
found=false;
for (int i=0; i<SIZE; i++)
{
if (value==numbers[i])
{
found=true;
break;
}
}
if (!found)
cout << "Enter a valid value !" << endl;
}
while (!found);

bubbleSort(numbers, SIZE);
selectionSort(numbers, SIZE);
insertionSort(numbers, SIZE);

return 0;
}

void bubbleSort (int numbers[], int SIZE)
{
cout<<"\nOriginal order: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
int maxElement;
int index,counter=0;

for(maxElement=SIZE-1; maxElement>=0; maxElement--)
{
for(index=0;index<=maxElement-1;index++)
{
if(numbers[index]>numbers[index+1])
{
swap(numbers[index], numbers[index+1]);
counter++;//increments counter everytime swap occurs
}
}
}
cout<<"\nBubble Sorted: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
cout<<"\nNumbers of location swap: "<<counter<<endl;
}

int swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}

void selectionSort(int numbers[], int SIZE)
{ cout<<"\nOriginal order: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
int startScan;
int index;
int miniIndex;
int miniValue;
int counter=0;

for(startScan=0;startScan<(SIZE-1);startScan++)
{
miniIndex=startScan;
miniValue=numbers[startScan];

for(index=startScan+1;index<SIZE;index++)
{
if(numbers[index]<miniValue)
{
miniValue=numbers[index];
miniIndex=index;
}

}
swap(numbers[miniIndex], numbers[startScan]);
counter++;
}
cout<<"\nSelection Sorted: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
cout<<"\nNumbers of location swap: "<<counter<<endl;
cout << endl;
}


void insertionSort (int arr[], int SIZE)
{
int j, temp, swap = 0;
cout<<"Original order: ";
for(int i = 0; i < SIZE; i++)
cout<< arr[i] << ' ';
for (int i = 0; i < SIZE; i++){
j = i;

while (j > 0 && arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--; swap++;
}
}
cout <<"\nThe number of location swaps is: "<< swap << endl;
return;
}
Last edited on
When you call insertionSort the array has already been sorted, by bubbleSort and selectionSort, so no swaps are necessary.
http://prntscr.com/c03vnz

The output said the number of swaps was zero when the number of swaps for the insertion function is supposed to be exactly 19.

But if you noticed, I called a new array to insertionSort so it could sort the array since the other array has already been sorted. : )

That's what I'm trying to do.
No, you are calling all three sorts with the SAME array. The array is already sorted by the bubble and selection sorts, therefore there are 0 swaps when you go to insertion sort.

Even though the parameter for your insertionSort function says int arr[], you still passed the array 'numbers' to it, just like the other two functions. Check it out:

1
2
3
bubbleSort(numbers, SIZE);
selectionSort(numbers, SIZE);
insertionSort(numbers, SIZE);
Last edited on
Well that was a stupid mistake. However, I'm still getting the same output. Reasons?
But if you noticed, I called a new array to insertionSort so it could sort the array since the other array has already been sorted. : )
1
2
3
bubbleSort(numbers, SIZE);
selectionSort(numbers, SIZE);
insertionSort(numbers, SIZE);

Did you post some old code ?
Output
1
2
3
4
5
6
7
8
9
10
11
12

Original order: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51
Bubble Sorted: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93
Numbers of location swap: 89

Original order: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93
Selection Sorted: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93
Numbers of location swap: 19

Original order: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93
The number of location swaps is: 0
Press any key to continue . . .
Nope. Just made a mistake. I apologize. Didn't even notice it and it didn't give me a warning.

Topic archived. No new replies allowed.