Looking for some help understanding methods used in this sample code from Jumping into C++ on sorting arrays. At this point the author jumps from the basics of passing arrays to functions to this, and the steps aren't clear. If anything can someone help explain what is going on in functions findSmallestRemainingElement() and swap()? Also what is going on with index? This is all kinds of worlds of confusing. I feel like I need start over.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size)
{
for (int i = 0; i < size; i++)
{
int index = findSmallestRemainingElement (array, size, i); //Why are there not [] for array here?
swap (array, i, index);
}
}
int findSmallestRemainingElement (int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if (array[i] < array[index_of_smallest_value])
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}
void swap (int array[], int first_index, int second_index)
{
int temp = array[first_index];
array[first_index] = array[second_index];
array[second_index] = temp;
}
void displayArray (int array[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if (i != 0)
{
cout << ", ";
}
cout << array[i];
}
cout << "}";
}
int main()
{
int array[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
array[i] = rand() % 100;
}
cout << "Original array: ";
displayArray(array, 10);
cout << "\n";
sort(array, 10);
cout << "Sorted array: ";
displayArray(array, 10);
cout << "\n";
}
Allow me to try and explain using a real life example.
Let's say you wanted to find the heaviest kid in your class ( kind of mean i know). The problem is that you have a really bad memory (you can only remember one kids weight at a time). How would you do it?
Well, first lets line up all the kids in your class in a line and draw a number on their forehead (their index). The first kid is index 0, up to the last kid who is index n. This is basically your array.
But the number on their forehead doesn't tell you the kids weight...obviously. For that you need a scale. So, to 'look up' the weight of each kid, you call their index number, they step on the scale, and you get their weight. This is accessing array elements via index number.
So, what's next? Well...weigh kid '0'. Is he the heaviest so far? Well he is the first kid, so obviously yeah. So remember the index '0' in your head.
Now, call the next numbered kid (kid '1'), and access his weight by getting him to step on the scale. While doing this, weigh the kid who's number you remembered (kid '0') on a second scale. If the new kid's weight is higher than the kid you remembered, then forget about the old kid's index and remember the new kid's index.
Keep doing this until you are through all of the kids, then blurt out the number of the kid you remember. This is the function return.
Ill leave someone else to answer for the 'swap' function.