SelectSortWhile


public class SelectSortWhile
{
    public static void main(String[] args)
    {
        //define array
        int[] arr={5,8,2,3,7,4,10,6,9,1};
        //define i as array loop index
        int i;
        //define min as minium index
        int min;
        //define j as each loop to use for compare number
        int j;
        //define temporary number for exchange
        int tmp;

        i=1;
        while(i<=arr.length-1)
        {
            min=i;
            j=min+1;
            while(j<=arr.length)
            {
                //j-1 and min-1 both represent j and min in array index
                if(arr[j-1]<arr[min-1])
                {
                    min=j;
                }
                j++;
            }
            //min-1 and i-1 both represent min and i in array index
            tmp=arr[min-1];
            arr[min-1]=arr[i-1];
            arr[i-1]=tmp;

            i++;
        }

        Util.printArray(arr);
    }
}

posted @ 2022-11-18 00:53  MikeLi  阅读(20)  评论(0编辑  收藏  举报