Hey everyone,

So, I need help understanding why I'm not able sort my code: here is my code (so far) for my selection sort. I have only written the iterative form of Selection Sort, but I know how to do it recursively as well (which is required for my homework). When I print it out, it just prints out the unsorted array AFTER I call the selection sort. Here is my full code so far:

main.cpp

``````#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "SelectionSort.h"

using namespace std;

template <class print> void printData(print a, int sizeOf);
template <class randomData> void putData(randomData a,int sizeOf);
void createCopy (int a[], int b[], const int size);

int main()
{
const int smallSize = 20;
const int largeSize = 5000;

int smallArray[smallSize];
vector <int> smallVector(smallSize);
int largeArray[largeSize];
vector <int> largeVector(largeSize);

putData(smallArray,smallSize);
putData(largeArray,largeSize);

putData<vector <int>&>(smallVector,smallVector.size());
putData<vector <int>&>(largeVector,largeVector.size());

int copySmallArr[smallSize];
vector<int> copySmallVec(smallSize);
int copyLargeArr[largeSize];
vector<int> copyLargeVec(largeSize);

createCopy(copySmallArr,smallArray,smallSize);
copySmallVec = smallVector;
createCopy(copyLargeArr,largeArray,largeSize);
copyLargeVec = largeVector;

cout << "\nSmall Array Unsorted: ";
printData(smallArray,smallSize);

cout << "\nSmall Vector Unsorted: ";
printData<vector <int>&>(smallVector,smallVector.size());

SelectionSort sort;
sort.sortDataIter(smallArray,smallSize);
cout << "\nSmall Array Sorted: ";
printData(smallArray,smallSize);

sort.sortDataIter(smallArray,smallSize);
cout << "\nSmall Vector Sorted: ";
printData(smallVector,smallVector.size());

return 0;
}

void createCopy (int a[], int b[], const int size)
{
for(int i = 0; i < size; i++)
{
a[i] = b[i];
}
}

template <class randomData>
void putData(randomData a, int sizeOf)
{
srand((unsigned int)time(0));

for(int i = 0; i < sizeOf; i++)
{
a[i] = rand()%1000;
}
}

template <class print>
void printData(print a,int sizeOf)
{
for(int i = 0; i < sizeOf; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
``````

SelectionSort.h

``````#include <iostream>

using namespace std;

class SelectionSort
{
public:
void sortDataIter(int theList[], int sizeOfList);
void sortDataIter(vector<int>& theList, int sizeOfList);
void sortDataRec (vector<int>& theList, int sizeOfList,int min);
void sortDataRec (int theList[], int sizeOfList,int min);
void swap(int one, int two);
};
``````

SelectionSort.cpp

``````#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "SelectionSort.h"

using namespace std;

void SelectionSort :: sortDataIter (int theList[], int sizeOfList)
{
for(int i = 0; i < sizeOfList-1; i++)
{
int min = i;
for(int j = i+1; j<sizeOfList;j++)
{
if (theList[j] < theList[min])
{
min = j;
}
}
swap(theList[i],theList[min]);
}
}

void SelectionSort :: sortDataIter (vector<int>& theList, int sizeOfList)
{
int min = 0;
for(int i = 0; i < sizeOfList-1; i++)
{
min = i;
for(int j = i+1; j<sizeOfList;j++)
{
if (theList[j] < theList[min])
{
min = j;
}
}
swap(theList[min],theList[i]);
}
}

void SelectionSort :: sortDataRec (int theList[], int sizeOfList,int min)
{
}

void SelectionSort :: sortDataRec (vector<int>& theList, int sizeOfList,int min)
{
}

void SelectionSort :: swap(int one, int two)
{
int temp = one;
one = two;
two = temp;
}
``````

Your algorithms seem to be ok, the problem appears to be in the swap. You're only swapping the values not the elements of the collection itself. See if this helps:

``````void SelectionSort :: swap(int& one, int& two)
{
int temp = one;
one = two;
two = temp;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.