首页 > 其他分享 >InsertSort

InsertSort

时间:2022-11-20 11:25:28浏览次数:29  
标签:index arr represent challenger int 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);
    }
}

 

标签:index,arr,represent,challenger,int,InsertSort
From: https://www.cnblogs.com/lsjava/p/16908074.html

相关文章