Discuss / Java / 打卡~~

打卡~~

Topic source
/*
*数组排序Demo
*@tangxi.zq
*/
import java.util.Arrays;

public class Sort
{
    public static void main(String[]args)
    {
        int[] ns = {28, 12, 89, 73, 65, 18, 96, 50, 8, 36};
        //排序前
        System.out.println("排序前:" + Arrays.toString(ns));
        //升序
        IncreaseSort(ns);
        System.out.println("升序排序后:" + Arrays.toString(ns));
        //降序
        DescentSort(ns);
        System.out.println("降序排序后:" + Arrays.toString(ns));

    }
    //升序排序
    public static void IncreaseSort(int[]ns)
    {
        if(ns.length <= 0)
        {
            return ;
        }
        for(int i = 0 ; i < ns.length - 1 ; i++)
        {
            for(int j = ns.length -1 ; j > i ;j--)
            {
                if(ns[j] < ns[j-1])
                {
                    int tmp = ns[j];
                    ns[j]= ns[j-1];
                    ns[j-1] = tmp;
                }
            }
        }
    }
    //降序排序
    public static void DescentSort(int[]ns)
    {
        if(ns.length <= 0)
        {
            return ;
        }
        for(int i = 0 ; i < ns.length - 1 ; i++)
        {
            for(int j = ns.length -1 ; j > i ;j--)
            {
                if(ns[j] > ns[j-1])
                {
                    int tmp = ns[j];
                    ns[j]= ns[j-1];
                    ns[j-1] = tmp;
                }
            }
        }
    }
}

  • 1

Reply