Insertion Sort Question

Hi! I am trying to count the number of comparisons and assignments using insertion sort ONLY but the program I am using is telling me that the count is wrong. Can someone tell me what I am doing wrong? Thanks! I also put in the code for main.cpp if you need it.

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
inline void insertionSort(int list3[], int listLength, int& comp, int& assign)
{
    // write a function using insertion sort to sort the provided array

    // assign "comp" to the number of comparisons required

    // assign "assign" to the number of item assignments
    
    //local declarations of variables

int i, j, temp;

for (i = 1; i < listLength + 1; i++)
    {
        comp++;
            if (list3[i] < list3[i-1])
                {
                    comp++;
                    temp = list3[i];
                    j = i;
                    assign = assign+2;
                }
        do
            {
                list3[j] = list3[j-1];
                assign++;
                j--;
            } 


while(j > 0 && list3[j -1] > temp);
        {
            list3[j] = temp;
            assign++;
        }
    }
}

//main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "functions.cpp"

using namespace std; 

int main()
{
    int list1[5000]; 
    int list2[5000];
    int list3[5000];

    int compBubbleSort = 0, compSelectionSort = 0, compInsertionSort = 0;
    int assignBubbleSort = 0, assignSelectionSort = 0, assignInsertionSort = 0;

    fillArray(list1, 5000);
    copyArray(list1, list2, 5000);
    copyArray(list1, list3, 5000);

    bubbleSort(list1, 5000, compBubbleSort, assignBubbleSort);  
    selectionSort(list2, 5000, compSelectionSort, assignSelectionSort);  
    insertionSort(list3, 5000, compInsertionSort, assignInsertionSort);
    
    cout << "Number of comparisons---" << endl;
    cout << "  Bubble sort: " << compBubbleSort << endl;
    cout << "  Selection sort: " << compSelectionSort << endl;
    cout << "  Insertion sort: " << compInsertionSort << endl << endl;

    cout << "Number of item assignments---" << endl;
    cout << "  Bubble sort: " << assignBubbleSort << endl;
    cout << "  Selection sort: " << assignSelectionSort << endl;
    cout << "  Insertion sort: " << assignInsertionSort << endl << endl;


    return 0;   
}
what are you counting?

for (i = 1; i < listLength + 1; i++)
{
comp++; //counts the loop iterations. This seems wrong unless you consider the loop variable check to be a comparison.

if (list3[i] < list3[i-1])
{
comp++; //counts actual data comparisons.

but you don't count the compares done in your while loop to shuffle the data.
(By the way, that is the BIG ONE, that while loop data move is where all your time is spent, and the bigger the data type being copied, the worse this becomes, eg if copying strings or classes here... at some point you swap to pointers and its the same as integers though).


Last edited on
Oh ok I see now but when I take that out and run it the program still fails either way if it's there or not
Last edited on
more info. does your sort actually work and sort the list? Counting the work is pointless if not :)
jonnin, yes it does sort it.
Topic archived. No new replies allowed.