Java - Selection Sort Algorithm

后端 未结 17 1647
我在风中等你
我在风中等你 2020-12-17 04:26

I have some questions about selection sort.I\'m a little bit confused.

 int [] arr = {5,4,3,2,1}; // This is my array
    int min = 0;

    for(int i = 0;i&l         


        
17条回答
  •  青春惊慌失措
    2020-12-17 04:53

    Correct:

    public class Test {
    
    public static void main(String args[]){
        int[] arr = {5,4,3,2,1}; // This is my array
        int min = 0;
    
        for(int i = 0;i

    About the min part: it just refers to the index of whatever is the current min. You move on down the array until you meet the new min, and set min to that index. So 5 is the minimum number [min =0] until you see 4 [so now min =1] but then you compare 3 to whatever is stored at 4 [when min=1] and then realize that you should set min=2.... etc. etc.

提交回复
热议问题