#Selectionsort
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @delicate vigil! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
import java.util.Arrays;
public class sauce{
public static void main(String[] args) {
int[] a = {5, 4, 3, 2, 1};
for(int i = 0; i < (a.length-1); i++){
int indexmaxvalue = i;
int tobetested = a[i];
if(tobetested > indexmaxvalue){
indexmaxvalue = tobetested;
}
a[a.length-1-i] = indexmaxvalue;
}
System.out.println(Arrays.toString(a));
}
}
this is what i got up until now but the output is [5, 4, 3, 4, 5]
also it's supposed to be in ascending order
then you'd need to find the min, not the max
you're only doing half of the swap
wait i'll try again with the min and let you know if i got it right this time
thanks for yourhelp
also, consider that you have 2 kinds of values that you're working with
you have elements of the array, and you have indexes of the array.
with selection sort, you only need to compare elements.
indexes should never show up in comparisons within selectionsort, and indexes should never be compared with elements unless they already have some other special meaning
you're mixing indexes and elements