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
/*
* 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>
usingnamespace 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;
}
/*
* 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 */
/*
* File: AMultiList.cpp
* Author: Chris C
*
*
* An ordered, "multi-list" of integers... a dynamic array of dynamic arrays. */
#include <iostream>
#include "AMultiList.h"
usingnamespace std;
/*
* Change this value affect the frequency of memory allocations when adding
* new data to the list.
*/
constint 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 */
elseif(size() == capacity)
{ /* initiate new tmp pointer(adds an extra address to tmp array) to copy rows */
int **tmp = newint*[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] = newint[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. */
elseif (size() == capacity)
{
int cnt = capacity / row_size;
int **tmp = newint *[cnt + 1];
for (int i = 0; i < cnt; i++)
{
tmp[i] = rows[i];
}
tmp[cnt] = newint[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;
}
}
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.