I have just created an int[ ] class called 'Statistics' and it has two selection sort methods for listing the integers in a Statistics object (int[ ]) in ascending or descending order. When I use either of these methods they tend to work about half of the time and then not work the other half of the time. Here's a couple examples of what I mean:
New Run
Test1 = {2, 5, 3, 7, 8, 9, 6}
Test1.sortDataDsc() will give me: Test1 = {8, 7, 6, 9, 3, 5, 2}
Test1A = {8, 7, 6, 9, 3, 5, 2}
Test1A.sortDataAsc() will give me: {2, 5, 3, 6, 7, 8, 9}
New Run
Test1 = {2, 5, 3, 7, 8, 9, 6}
If I do Test1.sortDataAsc() first it will sort the data correctly and also correctly sort it in descending order if I do that after.
New Run
Test2 = {7, 4, 5, 8, 0, 1}
Test2.sortDataAsc() will give me: {1, 0, 4, 5, 7, 8}.
It will then correctly sort these numbers in descending order and back to the correct ascending order.
All of the test cases I have tried are repeatable if you enter the numbers in the same order. If you change the order of the numbers, then the output could be correct or it could be a different wrong order. I have ruled out every problem I can think of that could cause this and I cannot find any similarities between test cases. If anyone sees anything in my code I could fix or add to remedy this situation it would be greatly appreciated.
count = the number of elements in the array
//sortDataAsc Method - Sorts data elements in Statistics array from least to greatest
public void sortDataAsc(){
int min, temp;
for(int index = 0; index < count; index++){
min = index;
for(int scan = index + 1; scan < count; scan++){
if(data[scan] < data[min]){
min = scan;
}
temp = data[min];
data[min] = data[index];
data[index] = temp;
}
}
}
//sortDataDsc Method - Sorts data elements in Statistics array from greatest to least
public void sortDataDsc(){
int max, temp;
for(int index = 0; index < count; index++){
max = index;
for(int scan = index + 1; scan < count; scan++){
if(data[scan] > data[max]){
max = scan;
}
temp = data[max];
data[max] = data[index];
data[index] = temp;
}
}
}