栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java第五课

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java第五课

 

1、冒泡排序

比较数组中,两个相邻的元素,如果第一个数比第二个数大,就交换他们的位置 2.每一次比较,都会产生出一个最大,或者最小的数字 3.下一轮则可以少一次排序 4.依次循环,直到结束

//冒泡排序
    public void sort(int[] array){
        for(int i=1;i             for(int j=0;j
                if(array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
           showArray(array);    
    }


2、选择排序
public static void main(String[] args) {
    int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
    // 只需要修改成对应的方法名就可以了
    selectionSort(array);
​
    System.out.println(Arrays.toString(array));
}
​
 
public static void selectionSort(int[] array) {
    if (array == null || array.length <= 1) {
        return;
    }
​
    int length = array.length;
​
    for (int i = 0; i < length - 1; i++) {
        // 保存最小数的索引
        int minIndex = i;
​
        for (int j = i + 1; j < length; j++) {
            // 找到最小的数
            if (array[j] < array[minIndex]) {
                minIndex = j;
            }
        }
​
        // 交换元素位置
        if (i != minIndex) {
            swap(array, minIndex, i);
        }
    }
​
}
​
 
private static void swap(int[] array, int a, int b) {
    int temp = array[a];
    array[a] = array[b];
    array[b] = temp;
}
3、插入排序

public static void main(String[] args) {
    int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
    // 只需要修改成对应的方法名就可以了
    insertionSort(array);
​
    System.out.println(Arrays.toString(array));
}
​
 
public static void insertionSort(int[] array) {
    if (array == null || array.length <= 1) {
        return;
    }
​
    int length = array.length;
​
    // 要插入的数
    int insertNum;
​
    for (int i = 1; i < length; i++) {
        insertNum = array[i];
        // 已经排序好的元素个数
        int j = i - 1;
        while (j >= 0 && array[j] > insertNum) {
            // 从后到前循环,将大于insertNum的数向后移动一格
            array[j + 1] = array[j];
            j--;
        }
​
        // 将需要插入的数放在要插入的位置
        array[j + 1] = insertNum;
    }
}
4、快速排序

public static void main(String[] args) {
    int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
    // 只需要修改成对应的方法名就可以了
    quickSort(array);
​
    System.out.println(Arrays.toString(array));
}
​
 
public static void quickSort(int[] array) {
    quickSort(array, 0, array.length - 1);
}
​
​
private static void quickSort(int[] array, int left, int right) {
    if (array == null || left >= right || array.length <= 1) {
        return;
    }
    int mid = partition(array, left, right);
    quickSort(array, left, mid);
    quickSort(array, mid + 1, right);
}
​
​
private static int partition(int[] array, int left, int right) {
    int temp = array[left];
    while (right > left) {
        // 先判断基准数和后面的数依次比较
        while (temp <= array[right] && left < right) {
            --right;
        }
        // 当基准数大于了 arr[left],则填坑
        if (left < right) {
            array[left] = array[right];
            ++left;
        }
        // 现在是 arr[right] 需要填坑了
        while (temp >= array[left] && left < right) {
            ++left;
        }
        if (left < right) {
            array[right] = array[left];
            --right;
        }
    }
    array[left] = temp;
    return left;
}
​

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996390.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号