InsertSort

package com.challenger;

import com.challenger.Util;

public class InsertSort
{
    public static void main(String[] args)
    {
        int i,j,exNum,k;
        int[] arr={6,3,4,1,9,2,10,7,5,8};

        Util.printArray(arr);

        i=2;

        do
        {
            j=1;
            //i-1 and j-1 both represent index in arr
            do
            {
                if(arr[i-1]<arr[j-1])
                {
                    exNum=arr[i-1];
                    k=i-1;
                    do
                    {
                        //k+1-1 and k-1 both represent index in arr
                        arr[k+1-1]=arr[k-1];
                        k--;

                    }
                    while(k>=j);

                    //j-1 represent index in arr
                    arr[j-1]=exNum;
                }
                else
                {
                    j++;

                }
            }
            while(j<i);
            i++;
        }
        while(i<=arr.length);

        Util.printArray(arr);
    }
}

 

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