lundi 23 février 2015

How to do selection sorting



I have the following data that needs to be sorted using selection sort. We have itemName, category, quantity and price as instance variables.



“Blouse” “W” 21 17.95
“Blouse” “C” 35 17.95
“Shirt” “M” 13 21.95
“Pants” “W” 22 67.95
“Pants” “M” 22 62.95
“Pants” “C” 26 21.95
“Coat” “C” 20 21.95
“Coat” “M” 10 62.95


Here is my code for selection sort but my output isn't coming out right. When it is supposed to display in ascending order it displays in descending order. what should I do? is my problem in my compare to method or on the code below?



int min;
Comparable temp;
for(int index = 0; index< list.length-1; index++)
{
min=index;
for(int scan= index+1; scan<list.length; scan++)
{
if(list[min].compareTo(list[scan])<0)
min = scan;
temp= list[min];
list[min] = list[index];
list[index]= temp;



Aucun commentaire:

Enregistrer un commentaire