admin管理员组文章数量:1022784
I have a few functions here that are taking strings from an array and sorting them alphabetically. SortList is the main function, which calls functions CompareString, LastName, and FirstName. Functions LastName & FirstName simply take a string containing a full name and parse them for their respective parts. CompareString first looks at every char of two last names and compares them, returning true if the pivot value is less than the target. If the last names are the same, then it looks at both first names.
When I run it, I get the error in the attached screenshot.
public bool CompareString(string lName1, string lName2, string fName1, string fName2)
{
// While there are letters in the last name
for (int i = 0; i < lName1.Length && i < lName2.Length; i++)
{
// If letter of first string is after letter of pivot string
if (lName1[i] > lName2[i])
return false;
else if (lName1[i] < lName2[i])
return true;
// If we get here, last names are the same
else if (i == lName1.Length - 1 || i == lName2.Length - 1)
{
// While there are letters in the first name
for (int j = 0; j < fName1.Length && j < fName2.Length; j++)
{
// If letter of first string is less than letter of pivot string
if (fName1[j] < fName2[j])
{
return false;
}
}
} // End first name compare
} // End name compare
// Pivot string is first alphabetically
return true;
} // End CompareString
// PURPOSE: Fills array with data from file
// PRE:
// POST: Array filled with data from file
public void SortList(int leftInd, int rightInd)
{
int l = leftInd; // initialized with 0
int r = rightInd; // initialized with numDivers - 1
// DiverRec pivot = diverList[leftInd]; // set to first value
DiverRec temp;
// Assign last names to left, right, & pivot
string lLast = LastName(diverList[l].name);
string rLast = LastName(diverList[r].name);
string pLast = LastName(diverList[l].name);
// Assign first names to left, right, & pivot
string lFirst = FirstName(diverList[l].name);
string rFirst = FirstName(diverList[r].name);
string pFirst = FirstName(diverList[l].name);
// Compare left & right strings with pivot
while (l <= r)
{
while (CompareString(lLast, pLast, lFirst, pFirst))
{
l++;
lLast = LastName(diverList[l].name);
lFirst = FirstName(diverList[l].name);
}
while (!CompareString(rLast, pLast, rFirst, pFirst))
{
r--;
rLast = LastName(diverList[r].name);
rFirst = FirstName(diverList[r].name);
}
if (l <= r)
{
temp = diverList[l];
diverList[l] = diverList[r];
diverList[r] = temp;
l++;
r--;
}
}
if (leftInd < r)
SortList(leftInd, r);
if (l < rightInd)
SortList(l, rightInd);
/*if (l > r)
{
temp = diverList[r];
diverList[r] = pivot;
pivot = temp;
l++;
r--;
}
for (int i = 0; i < numDivers - 1; i++)
Console.WriteLine(diverList[i].name);*/
} // End SortList
public string LastName(string lName)
{
char delim = ' '; // Separates first & last name
int pos = lName.IndexOf(delim); // Position of delim
// Isolate last name
string last = lName.Substring(pos + 1, lName.Length - (pos + 1));
// Return last name
return last;
} // End LastName
public string FirstName(string fName)
{
char delim = ' '; // Separates first & last name
int pos = fName.IndexOf(delim); // Position of delim
// Isolate first name
string first = fName.Substring(0, pos);
// Return first name
return first;
} // End FirstName
I've tried multiple things, making sure I'm incrementing & decrementing in the right places, making sure the strings are read in correctly... I'm almost certain that the issue is somewhere in the recursive function because I only learned about it last week so I'm not really confident in implementing it yet. Thank you in advance to anyone who's willing to lend a hand.
I have a few functions here that are taking strings from an array and sorting them alphabetically. SortList is the main function, which calls functions CompareString, LastName, and FirstName. Functions LastName & FirstName simply take a string containing a full name and parse them for their respective parts. CompareString first looks at every char of two last names and compares them, returning true if the pivot value is less than the target. If the last names are the same, then it looks at both first names.
When I run it, I get the error in the attached screenshot.
public bool CompareString(string lName1, string lName2, string fName1, string fName2)
{
// While there are letters in the last name
for (int i = 0; i < lName1.Length && i < lName2.Length; i++)
{
// If letter of first string is after letter of pivot string
if (lName1[i] > lName2[i])
return false;
else if (lName1[i] < lName2[i])
return true;
// If we get here, last names are the same
else if (i == lName1.Length - 1 || i == lName2.Length - 1)
{
// While there are letters in the first name
for (int j = 0; j < fName1.Length && j < fName2.Length; j++)
{
// If letter of first string is less than letter of pivot string
if (fName1[j] < fName2[j])
{
return false;
}
}
} // End first name compare
} // End name compare
// Pivot string is first alphabetically
return true;
} // End CompareString
// PURPOSE: Fills array with data from file
// PRE:
// POST: Array filled with data from file
public void SortList(int leftInd, int rightInd)
{
int l = leftInd; // initialized with 0
int r = rightInd; // initialized with numDivers - 1
// DiverRec pivot = diverList[leftInd]; // set to first value
DiverRec temp;
// Assign last names to left, right, & pivot
string lLast = LastName(diverList[l].name);
string rLast = LastName(diverList[r].name);
string pLast = LastName(diverList[l].name);
// Assign first names to left, right, & pivot
string lFirst = FirstName(diverList[l].name);
string rFirst = FirstName(diverList[r].name);
string pFirst = FirstName(diverList[l].name);
// Compare left & right strings with pivot
while (l <= r)
{
while (CompareString(lLast, pLast, lFirst, pFirst))
{
l++;
lLast = LastName(diverList[l].name);
lFirst = FirstName(diverList[l].name);
}
while (!CompareString(rLast, pLast, rFirst, pFirst))
{
r--;
rLast = LastName(diverList[r].name);
rFirst = FirstName(diverList[r].name);
}
if (l <= r)
{
temp = diverList[l];
diverList[l] = diverList[r];
diverList[r] = temp;
l++;
r--;
}
}
if (leftInd < r)
SortList(leftInd, r);
if (l < rightInd)
SortList(l, rightInd);
/*if (l > r)
{
temp = diverList[r];
diverList[r] = pivot;
pivot = temp;
l++;
r--;
}
for (int i = 0; i < numDivers - 1; i++)
Console.WriteLine(diverList[i].name);*/
} // End SortList
public string LastName(string lName)
{
char delim = ' '; // Separates first & last name
int pos = lName.IndexOf(delim); // Position of delim
// Isolate last name
string last = lName.Substring(pos + 1, lName.Length - (pos + 1));
// Return last name
return last;
} // End LastName
public string FirstName(string fName)
{
char delim = ' '; // Separates first & last name
int pos = fName.IndexOf(delim); // Position of delim
// Isolate first name
string first = fName.Substring(0, pos);
// Return first name
return first;
} // End FirstName
I've tried multiple things, making sure I'm incrementing & decrementing in the right places, making sure the strings are read in correctly... I'm almost certain that the issue is somewhere in the recursive function because I only learned about it last week so I'm not really confident in implementing it yet. Thank you in advance to anyone who's willing to lend a hand.
本文标签: cEndless quicksort recursive function (academic assignment)Stack Overflow
版权声明:本文标题:c# - Endless quicksort recursive function (academic assignment) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745576378a2157042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论