Trouble sorting a dynamic array of dynamic arrays

Ok, so I've been working on this for a while and I feel stupid for having to ask, but I just can't see it.

My sort function works, but it doesn't sort my data. I'm creating a loop to loop through the size of the array and swap positions through my swap function that I also call within the sort function.

The code I am having issues with is the code below:

**INSERION SORT**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* sort data using the insert sort */
void AMultiList::insert_sort()
{
    for (int i = 1; i <= size(); i++)
    {


        int pos = i;

        while ((pos >= 1) && (pos < pos - 1))
        {

            swap(pos, (pos - 1));
            pos--;
        }
        cout << pos << endl;
    }

}



Now here is the rest project so that you can see it in it's entirety

**MAIN**
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
/*
 * File:   main.cpp
 * Author: Chris C
 *

 * Created on February 1, 2011, 12:32 AM
 * Updated on March 6, 2011, 1:30 PM
 * Updated on March 12, 2011, 12:15 PM
 */

#include "AMultiList.h"
#include <iostream>

using namespace std;

int main(int argc, char** argv) {
    AMultiList x;

    /* Simple for loop to append numbers to the list */
    for(int i = 0; i < 10; i++)
    {
        x.append(i);

    }

    /* Simple for loop to prepend numbers to the list */
    for (int j = 15; j < 20; j++)
    {
        x.prepend(j);
    }

    /* Function call to print list */
    cout << "List: " << endl;
    x.print();
    cout << endl;

    /* Function call to sort list */
    cout << "Sorted List: " << endl;
    x.insert_sort();
    cout << endl;

    /* Function call to set a given position in the list to some given number & print the new list*/
    cout << "nSet Pos 3 to 55" << endl;
    x.set(3, 55);
    x.print();

    /* Function call to get a position in the list and print it's data */
    cout << "nGet Position 12: " << x.get(12) << endl;

    /* Function call to swap the data located at two different positions and print new list */
    cout << "nnSwap Position 3 with Position 8: " << endl;
    x.swap(3, 8);
    x.print();

    /* Function call to remove the data and position from list and print new list */
    cout << "nnRemove Position 8:" << endl;
    x.remove(8);
    x.print();

    /* Function call to clear list */
    x.clear();

    return 0;
}


**HEADER FILE**
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
/*
 * File:   AMultiList.h
 * Author: Chris C
 *
 *
 * An ordered, "multi-list" of integers... a dynamic array of dynamic arrays.
 */

#ifndef AMultiLIST_H
#define	AMultiLIST_H

class AMultiList {

public:

    /* creates an empty list */
    AMultiList();

    /* destroys the list */
    ~AMultiList();

    /* how many items are in the list */
    int size();

    /* make the list empty again */
    void clear();

    /* add data to the head of the list */
    void prepend(int data);

    /* add data to the tail of the list */
    void append(int data);

    /* swap the data between two nodes */
    void swap(int pos1, int pos2);

    /* returns the data contents of the "pos"ition-th list menber */
    int get(int pos);

    /* replaces the existing position in the list with the data */
    void set(int pos, int data);

    /* removes the existing cell from the list */
    void remove(int pos);

    /* print the list to the console */
    void print();

    /* insert sort */
    void insert_sort();

private:

    int **rows;
    int capacity;
    int length;
};

#endif	/* AMultiLIST_H */ 


**IMPLEMENTATION FILE**
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * File:   AMultiList.cpp
 * Author: Chris C
 *
 *
 * An ordered, "multi-list" of integers... a dynamic array of dynamic arrays. */
#include <iostream>

#include "AMultiList.h"

using namespace std;

/*
 * Change this value affect the frequency of memory allocations when adding
 * new data to the list.
 */
const int row_size = 10;

AMultiList::AMultiList()
{
    rows = NULL;
    capacity = 0;
    length = 0;
}

AMultiList::~AMultiList()
{

    if (size() > 0)
        delete []rows;

}

/* how many items are in the list */
int AMultiList::size()
{
    return length;
}

/* make the list empty again */
void AMultiList::clear()
{
    capacity = 0;
    length = 0;
    delete []rows;

    cout << "nThe List is Clear!" << endl;

    return;
}

/* add data to the head of the list */
void AMultiList::prepend(int data)
{
     int cnt = capacity / row_size;
     /* common case */
     if (size() < capacity)
     {
          for (int i = size(); i > 0; i--)
          {
               int row1 = i / row_size;
               int col1 = i % row_size;
               int row0 = (i - 1) / row_size;
               int col0 = (i - 1) % row_size;
               rows[row1][col1] = rows[row0][col0];
          }
     }

     /* special case */
     else if(size() == capacity)
     { /* initiate new tmp pointer(adds an extra address to tmp array) to copy rows */
        int **tmp = new int*[cnt + 1];

        for (int i = 0; i < cnt; i++)
        { // copies all the data
            tmp[i] = rows[i];
        }

        /* adds the new row to last position of tmp[] */
        tmp[cnt] = new int[row_size];
        delete rows;
        rows = tmp;
        capacity += row_size;

        for (int i = size(); i > 0; i--)
        {
               int row1 = i / row_size;
               int col1 = i % row_size;
               int row0 = (i - 1) / row_size;
               int col0 = (i - 1) % row_size;
               rows[row1][col1] = rows[row0][col0];
        }
    }

    rows[0][0] = data;

    length++;

    return;
}

/* add data to the tail of the list */
void AMultiList::append(int data)
{
    /* Checks to see if a list already exists and if so it adds to the end of the list */
    if (size() < capacity)
    {
        rows[size() / row_size][size() % row_size] = data;
    }

    /* If the list doesn't exist then this creates a lists and adds the data. */
    else if (size() == capacity)
    {
        int cnt = capacity / row_size;
        int **tmp = new int *[cnt + 1];

        for (int i = 0; i < cnt; i++)
        {
            tmp[i] = rows[i];
        }

        tmp[cnt] = new int[row_size];
        delete rows;
        rows = tmp;
        capacity += row_size;
        rows[size() / row_size][size() % row_size] = data;
    }

    length++;

    return;
}

/* swap the data between two nodes */
void AMultiList::swap(int pos1, int pos2)
{
    int tmp; //Temp Vasriable to store data from pos1

    /* Calculation to locate position of data */
    int row1 = pos1 / row_size;
    int col1 = pos1 % row_size;
    int row2 = pos2 / row_size;
    int col2 = pos2 % row_size;


    /* The swap happens here */
    tmp = rows[row1][col1];
    rows[row1][col1] = rows[row2][col2];
    rows[row2][col2] = tmp;


    return;
}

/* returns the data contents of the "pos"ition-th list menber */
int AMultiList::get(int pos)
{
    int result;

    /* Assumes the position exists and stores the location to variabls row and col */
    int row = pos / row_size;
    int col = pos % row_size;

    result = rows[row][col]; //Stores the data located at the position

    return result;
}

/* replaces the existing position in the list with the data */
void AMultiList::set(int pos, int data)
{
    /* Assumes the position exists and stores the location to variabls row and col */
    int row = pos / row_size;
    int col = pos % row_size;

    /* Stores the number passed through the data variable to the location stores in rows */
    rows[row][col] = data;

    return;
}

/* removes the existing cell from the list */
void AMultiList::remove(int pos)
{
     /* Loops through the list to find the position to be deleted */
     for (int i = pos; i < size(); i++)
     {
          int row1 = i / row_size;
          int col1 = i % row_size;
          int row0 = (i + 1) / row_size;
          int col0 = (i + 1) % row_size;
          rows[row1][col1] = rows[row0][col0];
     }

     length--;


    return;
}

/* print the list to the console */
void AMultiList::print()
{
    /* Prints when a list exists */
    for (int i = 0; i < size(); i++)
    {
        int row = (i) / row_size;
        int col = (i) % row_size;
        cout << rows[row][col] << endl;
    }

    return;
}

/* sort data using the insert sort */
void AMultiList::insert_sort()
{
    for (int i = 1; i <= size(); i++)
    {


        int pos = i;

        while ((pos >= 1) && (pos < pos - 1))
        {

            swap(pos, (pos - 1));
            pos--;
        }
        cout << pos << endl;
    }

}


Any help would be greatly appreciated!

Thanks,
Chris
Hey Chris,

i havent read all your code, but in void AMultiList::insert_sort() is a logical error.
while ((pos >= 1) && (pos < pos - 1))...
you will never enter that loop.
maybe fixing that will solve your problem, i dont have time to check, sorry.
Topic archived. No new replies allowed.