欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

给出数组array(1,9,5,8,3,7,2,4,6),写一个方法对其进行排序,使排序后的结果为(1,2,3,4,5,6,7,8,9)

程序员文章站 2022-07-15 12:07:41
...
package bi.shi.demo;

import org.junit.Test;

import java.util.Arrays;

/**
 * @program: bi_shi_test
 * @description: 给出数组array(1,9,5,8,3,7,2,4,6),写一个方法对其进行排序,使排序后的结果为(1,2,3,4,5,6,7,8,9)?
 * @author: xin yi
 * @create: 2021-09-11 15:34
 */
public class Demo03 {
    @Test
    public void demo01(){
        int[] array = new int[]{1,9,5,8,3,7,2,4,6};
        paixu(array);
        System.out.println(Arrays.toString(array));
    }

    private void paixu(int[] array) {
        int temp = 0;
        for (int i = 0; i < array.length-1; i++) {
            for (int i1 = 0; i1 < array.length-1; i1++) {
                if (array[i1] > array[i1+1]){
                    temp = array[i1];
                    array[i1] = array[i1+1];
                    array[i1+1] = temp;
                }
            }
        }
    }
}

给出数组array(1,9,5,8,3,7,2,4,6),写一个方法对其进行排序,使排序后的结果为(1,2,3,4,5,6,7,8,9)