Java——数组排序(算法)

Java——数组排序

选择排序

public class Test01{
    public static void main(String[] args){
        int[] arr = {4,6,2,3,1,5};
        for(int i = 0;i<arr.length;i++){
            int current = arr[i];
            int index = i;
            for(int j = i+1 ;j<arr.length;j++){
                if(arr[index] > arr[j]){
                    index = j;
                }
            }
            arr[i] = arr[index];
            arr[index] = current;
        }
        for(int i:arr){
            System.out.println(i);
        }
    }
}

插入排序

public class Test02{
    public static void main(String[] args){
        int[] arr = {4,6,2,3,1,5};
        for(int i = 0;i<arr.length-1;i++){
            int current=[i+1];
            int index = i;
            while(index>=0 && current<arr[index]){
                arr[index+1]=arr[index];
                index--;
            }
            arr[index+1]=current;
        }
        for(int a:arr){
            System.out.print(a+"\t");
        }
    }
}

冒泡排序

public class test03{
    public static void main(String[] args){
        int[] arr = {4,8,2,3,1,5};
        for(int i = 0;i<arr.length-1;i++){
            for(int j = 0;j<arr.length-1-i;j++){
                if(arr[j]>arr[j+1]){
                    arr[j]=arr[j]^arr[j+1];
                    arr[j+1] = arr[j]^arr[j+1];
                    arr[j] = arr[j]^arr[j+1];
                }
            }
        }
        for(int a:arr){
            System.out.println(a);
        }
    }
}

二分查找

public class test04{
    public static void main(String[] args){
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int key = 9;
        int start = 0;
        int end = arr.length-1;
        int mid = (start+end)/2;
        while(end>=start){
            if(arr[mid] == key){
                System.out.println(mid);
                break;
            }else if(arr[mid] < key){
                start = mid+1;
            }else{
                end = mid-1;
            }
            mid = (start+end)/2
        }
        if(end<start){
            System.out.println("该数字不存在")
        }
    }
}

快速排序

public class Demo01 {
    public static void main(String[] args) {
        int[] arr = {3,7,6,1,8,2,0,9,-3,-1};
        quictSort(arr,0,arr.length-1);
        for (int i : arr) {
            System.out.println(i);
        }
    }
    public static void quictSort(int[] arr,int startIndex,int endIndex){

        int current = arr[startIndex];
        int low = startIndex;
        int height = endIndex;
        if (low>=height){
            return;
        }
        while (low<height){
            while(low < height && arr[height] > current){
                height--;
            }
            while (low < height && arr[low] <= current){
                low++;
            }if (low<height){
                int temp = arr[height];
                arr[height] = arr[low];
                arr[low] = temp;
            }

        }
        arr[startIndex] = arr[height];
        arr[height] = current;
        quictSort(arr, startIndex, height-1);
        quictSort(arr, height+1, endIndex);

    }
}

随机排名

public class Test05{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
        int[] arr = new int[5];
        for(int i = 0;i<arr.length;i++){
            System.out.println("请输入第"+(i+1)+"员工编号:");
            arr[i] = sc.nextInt();
            int index = rand.nextInt(arr.length);
            int temp = arr[index];
            arr[index] = arr[i];
            arr[i] = temp;      
        }
        for(int a:arr){
            System.out.println(a);
        }
    }
}

多维数组

定义二维数组

数据类型[][] 数组名;
数据类型 数组名[ ][ ];

// 动态初始化
int[][] arr = new int[3][4]; 
arr[0][0] = 1;
// 静态初始化
int[][] arr1 = new int[][]{ 
	{1, 2, 3},
	{2, 3},
	{3, 4, 5, 4}
};


//二维数组的迭代
for (int i = 0; i < arr1.length; i++) {
	for (int j = 0; j < arr1[i].length; j++) {
		System.out.print(arr1[i][j]);
	}
    System.out.println();
}

for (int[] t : arr1) {
	for (int a : t) {
		System.out.println(a);
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值