insertion sort problem

the problem here is at " insertionSort in_sort[9]; "
error:no matching function for call to "insertionSort::insertionSort()"
what is the solution for that ?



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
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/* Head ends here */

class insertionSort{
private:
     int temp ;
   int i,j,k,x;
public:
        insertionSort(vector <int>  ar) {

for(i = 1;i<ar.size();i++)
{
    for(j = 0;j<i;j++)
    {

        if(ar[i]<ar[j])
        {
            temp = ar[i];
            for(k = i;k>=j;k--)
            {
                ar[k] = ar[k-1];

                if(ar[k-1]<temp&&ar[k+1]>temp)
                {
                    ar[j] = temp;

                }
                }

            }
            }
            for(j = 0;j<=ar.size()-1 ;j++)
        {
            cout<<ar[j]<<" ";
        }
        cout<< endl;

        }

}
};

/* Tail starts here */
int main(void) {
 insertionSort in_sort[9];
}

Last edited on
Look at the way you've defined your constructor for insertionSort:

insertionSort(vector <int> ar)

When you create objects of that class, you need to pass in a vector as the argument to the constructor.
 
insertionSort in_sort[9];

Here you are just trying to create an array of 9 objects of class insertionSort. You are not providing the vector of int needed by the constructor.

Try,
1
2
std::vector<int> myVec( 9 );
insertSort in_sort( myVec );
Last edited on
line 49:error expected at ";" before "in_sort"
what is the solu ?

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
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/* Head ends here */

class insertionSort{
private:
     int temp ;
   int i,j,k,x;
public:
        insertionSort(vector <int>  ar) {

for(i = 1;i<ar.size();i++)
{
    for(j = 0;j<i;j++)
    {

        if(ar[i]<ar[j])
        {
            temp = ar[i];
            for(k = i;k>=j;k--)
            {
                ar[k] = ar[k-1];

                if(ar[k-1]<temp&&ar[k+1]>temp)
                {
                    ar[j] = temp;

                }
                }

            }
            }
            for(j = 0;j<=ar.size()-1 ;j++)
        {
            cout<<ar[j]<<" ";
        }
        cout<< endl;

        }

}
};

/* Tail starts here */
int main(void) {
 std::vector<int> myVec(9);
insertSort in_sort(myVec);
}

You've made a typo. Your class name is insertionSort, not insertSort.
thanks man :)
However in any case your code is invalid. Consider the loop

1
2
3
            for(k = i;k>=j;k--)
            {
                ar[k] = ar[k-1];


Let assume that j is equal to 0. In this case for k == j you will try to execute

ar[0] = ar[0-1];
Last edited on
p.s.
insertionSort(vector <int> ar) {
to
insertionSort(vector <int>& ar) {
you will have a better perf, and make the class meaningful, otherwise, you do not have any output...
You should watch the video here
http://en.wikipedia.org/wiki/Insertion_sort

and then adjust your function accordingly.

(You have one loop more than you need.)

Andy

PS All you member variables look like they should be locals; apart from x, which isn't used at all.
Last edited on
Topic archived. No new replies allowed.