C
C#2y ago
Grin

✅ I'm having trouble understanding what is going on within this selection search

// The SelectionSort method accepts a string array as an argument.
// It uses the Selection Sort algorithm to sort the array.
private void SelectionSort(string[] sArray)
{

int minIndex; // Subscript of minimum value in scanned area
string minValue; // Minimum value in the scanned area

// The outer loop steps through all the array elements,
// except the last one. The startScan variable marks the
// position where the scan should begin.
for (int startScan = 0; startScan < sArray.Length - 1; startScan++)
{
// Assume the first element in the scannable area
// is the minimum value.
minIndex = startScan;
minValue = sArray[startScan];

// Scan the array, starting at the 2nd element in the
// scannable area, looking for the minimum value.
for (int index = startScan + 1; index < sArray.Length; index++)
{
if (string.Compare(sArray[index], minValue, true) < 0)
{
minValue = sArray[index];
minIndex = index;
}
}

// Swap the element with the lesser value with the
// first element in the scannable area.
Swap(ref sArray[minIndex], ref sArray[startScan]);
}
}
// The SelectionSort method accepts a string array as an argument.
// It uses the Selection Sort algorithm to sort the array.
private void SelectionSort(string[] sArray)
{

int minIndex; // Subscript of minimum value in scanned area
string minValue; // Minimum value in the scanned area

// The outer loop steps through all the array elements,
// except the last one. The startScan variable marks the
// position where the scan should begin.
for (int startScan = 0; startScan < sArray.Length - 1; startScan++)
{
// Assume the first element in the scannable area
// is the minimum value.
minIndex = startScan;
minValue = sArray[startScan];

// Scan the array, starting at the 2nd element in the
// scannable area, looking for the minimum value.
for (int index = startScan + 1; index < sArray.Length; index++)
{
if (string.Compare(sArray[index], minValue, true) < 0)
{
minValue = sArray[index];
minIndex = index;
}
}

// Swap the element with the lesser value with the
// first element in the scannable area.
Swap(ref sArray[minIndex], ref sArray[startScan]);
}
}
I'm mostly confused about the if statement. What is happening there?
13 Replies
Grin
Grin2y ago
It seems to be comparing two strings, I think I understand that much. I'm not sure what the true is, and what is the purpose of the <0? Strings aren't values are they?
mtreit
mtreit2y ago
At a quick glance, It's comparing strings for their lexical ordering...this is how you sort strings. A given string is either "less than" another string in the sort order, "greater than" in the sort order, or equal. -1 means less than, 0 means equal, 1 means greater than
Grin
Grin2y ago
ty I need to find more to read about this, because I still don't have a strong grasp on it
mtreit
mtreit2y ago
Imagine a dictionary of English words, like Webster's dictionary or similar. The words are sorted from the first page to the last, right? Well if you had to do that in code, the string comparison mechanism implements the logic to determine if one word should appear before or after another...
Grin
Grin2y ago
Ah okay That makes sense I'm going to try debugging it tomorrow and watching for the values to change. The -1, 0, and 1 And I'll keep that concept in mind
mtreit
mtreit2y ago
String.Compare Method (System)
Compares two specified String objects and returns an integer that indicates their relative position in the sort order.
mtreit
mtreit2y ago
It means "do a case insensitive compare"
Anton
Anton2y ago
strictly speaking, the function doesn't guarantee that the result will be -1, 0 or 1, instead of being -1 it could be -2 or less, and vice versa so you should check against 0 always > 0, < 0, == 0
mtreit
mtreit2y ago
That's a good point.
Grin
Grin2y ago
In what instance would it be -2?
mtreit
mtreit2y ago
It depends on how the comparer is implemented. As far as I know the default string comparer only ever uses -1, 0 and 1, but the documentation is that a value less than zero is returned, zero is returned, or a value greater than zero is returned...so strictly speaking someone could change the implementation of the compare to use other values...
Grin
Grin2y ago
ty
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server
More Posts