(十一)、数组的简单操作

数组的简单操作

冒泡排序法

public class Demo3 {

    public static void main(String[] args) {
        // 从一个给定的数组中找出最大值最小值
        int[] arr = { 4, 50, 8, 9, 88, 77, 99, 51, 25 };
        int max = arr[0];
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
            if (min > arr[i]) {
                min = arr[i];
            }
        }
        System.out.println(max + " " + min);
        // 排序
        sort(arr);
        print(arr);
    }

    public static void sort(int[] a) {
        /**
         * 对传入的int型数组完成排序,对原数组进行排序
         *  排序算法: 1.冒泡 2.插入 3.选择
         */
        for (int j = 0; j < a.length - 1; j++) {
            // 外循环控制需要排序的数字个数
            for (int i = 0; i < a.length - 1 - j; i++) {
                if (a[i] > a[i + 1]) {
                    a[i] = a[i] ^ a[i + 1];
                    a[i + 1] = a[i] ^ a[i + 1];
                    a[i] = a[i] ^ a[i + 1];
                }
            }
        }
    }

    public static void print(int[] a) {
        for (int num : a) {
            System.out.print(num + " ");
        }
    }
}

顺序查找

    public static int search(int[] a, int num) {
        for (int i = 0; i < a.length; i++) {
            if (num == a[i]) {
                return i;
            }
        }
        return -1;
    }
}

二分查找

public class Demo5 {

    public static void main(String[] args) {
        int[] a = { 4, -50, 8, 9, 88, -20, 77, 99, 51, 25 };
        sort(a);
        search(a, 88);

    }

    public static void sort(int[] a) {
        /**
         * 对传入的int型数组完成排序,对原数组进行排序 排序算法: 1.冒泡 2.插入 3.选择
         */
        for (int j = 0; j < a.length - 1; j++) {
            // 外循环控制需要排序的数字个数
            for (int i = 0; i < a.length - 1 - j; i++) {
                if (a[i] > a[i + 1]) {
                    a[i] = a[i] ^ a[i + 1];
                    a[i + 1] = a[i] ^ a[i + 1];
                    a[i] = a[i] ^ a[i + 1];
                }
            }
        }
    }

    public static void search(int[] a, int num) {
        int minIndex = 0;
        int maxIndex = a.length - 1;
        for (; minIndex <= maxIndex;) {
            int midIndex = (minIndex + maxIndex) / 2;
            if (num == a[midIndex]) {
                System.out.println("找到了");
                break;
            } else if (num > a[midIndex]) {
                minIndex = midIndex + 1;
            } else {
                maxIndex = midIndex - 1;
            }
        }
        if (minIndex > maxIndex) {
            System.out.println("没找到");
        }
    }
}

使用Arrays工具类

import java.util.Arrays;

/*
 * Arrays:工具类,不需要对象,静态方法
 * 排序,二分查找,格式化输出
 * 不用对象,直接调用
 */
public class Demo6 {

    public static void main(String[] args) {
        int[] a = { 4, -50, 8, 9, 88, -20, 77, 99, 51, 25 };
        Arrays.sort(a);// 直接对传入的数组进行操作
        int index = Arrays.binarySearch(a, 4);
        Arrays.sort(a, 0, 5);// [from,to)
        System.out.println(Arrays.toString(a));
        System.out.println(index);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值