Ojan
March 27th, 2008, 06:48 PM
Hello all. I'm new to the forum, so please treat me nice ;) Should this topic be in the wrong forum or something, please move it to the right place :)
Anyway, my problem: I have a bunch of strings that I want sorted alphabetically. I have on teh internets found just the code to do this, and it works like a charm. However, for the life of me, I can't understand why it works.
#include <string>
#include <iostream>
using namespace std;
void selectionSort(string [], int);
void showArray(string [], int);
int main()
{
const int SIZE = 20;
string name[SIZE] =
{ "Collins, Bill", "Smith, Bart", "Michalski, Jacob",
"Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah",
"Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff",
"Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
"Moretti, Bella", "Wu, Hong", "Patel, Renee",
"Harrison, Rose", "Smith, Cathy", "Conroy, Patrick",
"Kelly, Sean", "Holland, Beth" };
cout << "\tThe unsorted string is: \n";
showArray(name, SIZE);
selectionSort(name, SIZE);
cout << "\n\tThe sorted string is: \n";
showArray(name, SIZE);
return 0;
}
void selectionSort(string name[], int elems)
{
int startScan, minIndex;
string strName;
for(startScan = 0; startScan < (elems - 1); startScan++)
{
minIndex = startScan;
strName = name[startScan];
for(int index = startScan + 1; index < elems; index++)
{
if(name[index] < strName)
{
strName = name[index];
minIndex = index;
}
}
name[minIndex] = name[startScan];
name[startScan] = strName;
}
}
void showArray(string name[], int elems)
{
for(int count = 0; count < elems; count++)
cout << count << ": " << name[count] << endl;
}
selectionSort is a void-function, there are no global variables, and we don't touch pointers or anything. How can "name" stay sorted when we showArray again?
The "name" in selectionSort appears to be a local copy, right? And I don't see how selectionSort can make "name" changed when we return to main. And yet it works. :p
Any help and explanations would be greatly appreciated :)
Anyway, my problem: I have a bunch of strings that I want sorted alphabetically. I have on teh internets found just the code to do this, and it works like a charm. However, for the life of me, I can't understand why it works.
#include <string>
#include <iostream>
using namespace std;
void selectionSort(string [], int);
void showArray(string [], int);
int main()
{
const int SIZE = 20;
string name[SIZE] =
{ "Collins, Bill", "Smith, Bart", "Michalski, Jacob",
"Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah",
"Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff",
"Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
"Moretti, Bella", "Wu, Hong", "Patel, Renee",
"Harrison, Rose", "Smith, Cathy", "Conroy, Patrick",
"Kelly, Sean", "Holland, Beth" };
cout << "\tThe unsorted string is: \n";
showArray(name, SIZE);
selectionSort(name, SIZE);
cout << "\n\tThe sorted string is: \n";
showArray(name, SIZE);
return 0;
}
void selectionSort(string name[], int elems)
{
int startScan, minIndex;
string strName;
for(startScan = 0; startScan < (elems - 1); startScan++)
{
minIndex = startScan;
strName = name[startScan];
for(int index = startScan + 1; index < elems; index++)
{
if(name[index] < strName)
{
strName = name[index];
minIndex = index;
}
}
name[minIndex] = name[startScan];
name[startScan] = strName;
}
}
void showArray(string name[], int elems)
{
for(int count = 0; count < elems; count++)
cout << count << ": " << name[count] << endl;
}
selectionSort is a void-function, there are no global variables, and we don't touch pointers or anything. How can "name" stay sorted when we showArray again?
The "name" in selectionSort appears to be a local copy, right? And I don't see how selectionSort can make "name" changed when we return to main. And yet it works. :p
Any help and explanations would be greatly appreciated :)