I had to code Selection Sort, and I previously used an Array and it worked perfectly, but the textfiles were too large to use an Array so I had to convert the code to ArrayList. What happens now is, (with the large textfile too) majority numbers are sorted but there are a few unsorted number in between the sorted ones, think it might be a duplication error... I have tried changing to descending order and it does the same thing. I used a smaller textfile to show the output. All the relevant methods are below.
public void SelectionSort() // ascending order
{
for (int i = 1; i <= List.Count; i++) // go through the list
{
ListClass minimum = (ListClass)List[i-1];
int min = i - 1;
for (int j = i;j <= List.Count-1; j++)
{
ListClass cur = (ListClass)List[j];
if (minimum.getNum() > cur.getNum())
min = j; // min equals smallest in list j
}
swap(List, i-1, min);
}
Console.WriteLine("Array after selection sort: ");
foreach (ListClass cur in List)
{
cur.Display();
}
public static void swap(ArrayList List, int x, int y)
{
object temp = List[x];
List[x] = List[y];
List[y] = temp;
}
public void Display()
{
foreach (ListClass cur in List)
{
cur.Display();
}
}
Array before sorting: 1 2 5 7 8 90 889 88 654 33 2 3 Array after selection sort: 1 2 3 5 7 8 90 2 88 33 654 889
List<T>.Sort? Or theArray.Sort?