First of all I would like to say hello to everyone,
I'm trying to learn C++. I have a pretty decent background in Java, and wanted to move over to C++.
I was working on writing insertion sort in order to learn C++, and I keep getting complier errors.
Code:
#include<iostream>
using namespace std;
class Demo
{
public:
void insertionSort(int[]);
private:
InsertionSort sort;
};
void Demo::insertionSort(int anArray[])
{
Demo::sort::sort(anArray);
cout << sort::toString();
}
public int main()
{
Demo demo;
int myArray [6] = {5,3,7,10,54,43};
demo.insertionSort(myArray);
}
and
Code:
class InsertionSort
{
public:
void sort(int[]);
String toString();
private:
int[] myArray;
};
void InsertionSort::sort(int [] anArray)
{
myArray = anArray;
for(int i = 1; i < myArray.length; i++)
{
int temp = myArray[i];
int k = i - 1;
while(k >= 0 && temp < myArray[k])
{
myArray[k + 1] = myArray[k];
k--;
}
myArray[k + 1] = temp;
}
}
String InsertionSort::toString()
{
String tempStr = "";
for(int i = 0; i < myArray.length; i++)
{
temp += myArray[i] + " ";
}
}
I would like to say thank you for advance.