最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)常見(jiàn)的排序算法的示例代碼

 更新時(shí)間:2022年10月17日 11:46:58   作者:時(shí)間郵遞員  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)常見(jiàn)的排序算法(選擇排序、插入排序、希爾排序等)的相關(guān)資料,文中的示例代碼講解詳細(xì),感興趣的可以了解一下

一、優(yōu)化后的冒泡排序

package com.yzh.sort;
/*
冒泡排序
 */
@SuppressWarnings({"all"})
public class BubbleSort {

    public static void BubbleSort(int[]a){
        for (int i = 0; i <a.length-1 ; i++) {
            boolean flag=true;
            for (int j = 0; j <a.length-i-1 ; j++) {
                if(a[j]>a[j+1]){
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                    flag=false;
                }
            }
            if(flag) break;
        }
    }

}

二、選擇排序

package com.yzh.sort;
@SuppressWarnings({"all"})
public class SelectSort {

    public static void SelectSort(int[]a) {
        for (int i = 0; i < a.length - 1; i++) {
            int index = i;//標(biāo)記為待比較的數(shù)
            for (int j = i + 1; j < a.length; j++) { //然后從后面遍歷與第一個(gè)數(shù)比較
                if (a[j] < a[index]) {  //如果小,就交換最小值
                    index = j;//保存最小元素的下標(biāo)
                }
            }
            //找到最小值后,將最小的值放到第一的位置,進(jìn)行下一遍循環(huán)
            int temp = a[index];
            a[index] = a[i];
            a[i] = temp;
        }
    }
}

三、插入排序

package com.yzh.sort;
@SuppressWarnings({"all"})
/*
插入排序
 */
public class InsertSort {

    public static void InsertSort(int[]a){
        for (int i = 0; i < a.length; i++) {
            //定義待插入的數(shù)
            int insertValue=a[i];
            //找到待插入數(shù)的前一個(gè)數(shù)的下標(biāo)
            int insertIndex=i-1;
            while (insertIndex>=0 && insertValue <a[insertIndex]) {//拿a[i]前面的數(shù)比較
                a[insertIndex+1]=a[insertIndex];
                insertIndex--;
            }
            a[insertIndex+1]=insertValue;
        }
    }
}

四、希爾排序

package com.yzh.sort;

/*
希爾排序
 */
@SuppressWarnings({"all"})
public class ShellSort {

    public static void ShellSort(int[] a){
        for (int gap=a.length / 2; gap > 0; gap = gap / 2) {
            //將整個(gè)數(shù)組分為若干個(gè)子數(shù)組
            for (int i = gap; i < a.length; i++) {
                //遍歷各組的元素
                for (int j = i - gap; j>=0; j=j-gap) {
                    //交換元素
                    if (a[j]>a[j+gap]) {
                        int temp=a[j];
                        a[j]=a[j+gap];
                        a[j+gap]=temp;
                    }
                }
            }
        }
    }
}

五、快速排序

package com.yzh.sort;
@SuppressWarnings({"all"})
/*
隨機(jī)化快速排序
 */
public class QuickSort {

    public static void QuickSort(int[] arr,int low,int high){
        int i,j,temp,t;
        if(low>=high){
            return;
        }
        i=low;
        j=high;
        //temp就是基準(zhǔn)位
        temp = arr[low];
        while (i<j) {
            //先看右邊,依次往左遞減
            while (temp<=arr[j]&&i<j) {
                j--;
            }
            //再看左邊,依次往右遞增
            while (temp>=arr[i]&&i<j) {
                i++;
            }
            //如果滿足條件則交換
            if (i<j) {
                t = arr[j];
                arr[j] = arr[i];
                arr[i] = t;
            }
        }
        //最后將基準(zhǔn)為與i和j相等位置的數(shù)字交換
        arr[low] = arr[i];
        arr[i] = temp;
        //遞歸調(diào)用左半數(shù)組
        QuickSort(arr, low, j-1);
        //遞歸調(diào)用右半數(shù)組
        QuickSort(arr, j+1, high);
    }
}

六、隨機(jī)化快速排序

package com.yzh.sort;

import java.util.Random;
import java.util.Scanner;

@SuppressWarnings({"all"})
public class RandQuickSort {

    public static void randsort(int[] arr, int left, int right) {
        if(left>=right)
            return;
        Random random = new Random();
        int randIndex = random.nextInt(right-left)+left;  //選取隨機(jī)主元
        //把隨機(jī)主元放到數(shù)組尾部
        int temp = arr[randIndex];
        arr[randIndex] = arr[right];
        arr[right] = temp;
        //數(shù)組中元素與主元比較
        int i = left-1;//注意
        for(int j = left;j<=right;j++) {
            if(arr[j]<arr[right]) {
                i++;
                int temp1 = arr[i];
                arr[i] = arr[j];
                arr[j] = temp1;
            }
        }
        //最后把主元放到適當(dāng)位置
        int temp2 = arr[i+1];
        arr[i+1] = arr[right];
        arr[right] = temp2;

        randsort(arr,left,i);
        randsort(arr,i+2,right);
    }
}

七、歸并排序

package com.yzh.sort;

@SuppressWarnings({"all"})
/*
歸并排序
 */
public class MergeSort {

    private static void mergesort(int[] a, int left, int right, int[] temp) {
        //分解
        if (left<right) {
            int mid=((right-left)>>1)+left;
            //向左遞歸進(jìn)行分解
            mergesort(a, left, mid, temp);
            //向右遞歸進(jìn)行分解
            mergesort(a, mid+1, right, temp);
            //每分解一次便合并一次
            merge(a,left,right,mid,temp);
        }
    }

    private static void merge(int[] a, int left, int right, int mid, int[] temp) {
        int i=left; //初始i,左邊有序序列的初始索引
        int j=mid+1;//初始化j,右邊有序序列的初始索引(右邊有序序列的初始位置即中間位置的后一位置)
        int t=0;//指向temp數(shù)組的當(dāng)前索引,初始為0

        //先把左右兩邊的數(shù)據(jù)(已經(jīng)有序)按規(guī)則填充到temp數(shù)組
        //直到左右兩邊的有序序列,有一邊處理完成為止
        while (i<=mid && j<=right) {
            //如果左邊有序序列的當(dāng)前元素小于或等于右邊的有序序列的當(dāng)前元素,就將左邊的元素填充到temp數(shù)組中
            if (a[i]<=a[j]) {
                temp[t++]=a[i++];
            }else {
                //反之,將右邊有序序列的當(dāng)前元素填充到temp數(shù)組中
                temp[t++]=a[j++];
            }
        }
        //把剩余數(shù)據(jù)的一邊的元素填充到temp中
        while (i<=mid) {
            //此時(shí)說(shuō)明左邊序列還有剩余元素
            //全部填充到temp數(shù)組
            temp[t++]=a[i++];
        }
        while (j<=right) {
            //此時(shí)說(shuō)明左邊序列還有剩余元素
            //全部填充到temp數(shù)組
            temp[t++]=a[j++];
        }
        //將temp數(shù)組的元素復(fù)制到原數(shù)組
        t=0;
        int tempLeft=left;
        while (tempLeft<=right) {
            a[tempLeft++]=temp[t++];
        }
    }
}

八、可處理負(fù)數(shù)的基數(shù)排序

package com.yzh.sort;

public class RadixSort{

    public static void main(String[] args) {
        int[]a={-2,-1,-6,3,5,1,2,88,-1,99,100,21};
        RadixSort(a);
        for (int x : a) {
            System.out.print(x+" ");
        }
        System.out.println();
    }


    public static int[] RadixSort(int[] arr){
        //最大值,用來(lái)計(jì)算需要找多少次
        int max = Integer.MIN_VALUE;
        //用來(lái)判斷是否是負(fù)數(shù)
        int min = Integer.MAX_VALUE;
        //從該數(shù)組中找到最大\最小值
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(max, arr[i]);
            min = Math.min(min, arr[i]);
        }
        //如果最小值小于0,那么把每個(gè)數(shù)都減去最小值,這樣可以保證最小的數(shù)是0
        if (min<0) {
            for (int i = 0; i < arr.length; i++) {
                arr[i] -= min;
            }
            //max也要處理
            max -= min;
        }
        //計(jì)算最大值有幾位數(shù)
        int maxLength = (max+"").length();
        //定義十個(gè)桶,每個(gè)桶就是一個(gè)一維數(shù)組
        int[][] bucket = new int[10][arr.length];
        //記錄每個(gè)桶中實(shí)際存放了多少個(gè)數(shù)據(jù)
        int[] bucketElementCount = new int[10];
        //根據(jù)最大長(zhǎng)度數(shù),決定比較次數(shù)
        for (int i = 0 ,n = 1 ; i < maxLength ; i++,n*=10) {
            //每一個(gè)數(shù)字分別計(jì)算余數(shù)
            for (int j = 0; j < arr.length ; j++) {
                int value = arr[j]/n % 10;
                //把當(dāng)前遍歷的數(shù)放到指定的位置
                bucket[value][bucketElementCount[value]] = arr[j];
                //該位置加一,為下一個(gè)值進(jìn)來(lái)做準(zhǔn)備
                bucketElementCount[value]++;
            }
            //記錄arr的位置
            int index = 0;
            //遍歷取出第n次排序的值,等于0的不需要取
            for (int j = 0; j < bucketElementCount.length ; j++) {
                if (bucketElementCount[j]!=0){
                    //遍歷取出數(shù)據(jù)并放到原來(lái)的arr中
                    for (int k = 0; k < bucketElementCount[j]; k++) {
                        arr[index] = bucket[j][k];
                        index++;
                    }
                }
                //把數(shù)量置為零,因?yàn)檫€有n輪
                bucketElementCount[j] = 0;
            }
        }
        //把排序好的arr重新加上減去的值
        if (min<0){
            for (int i = 0; i < arr.length ; i++) {
                arr[i] += min;
            }
        }
        return arr;
    }

}

以上就是Java實(shí)現(xiàn)常見(jiàn)的排序算法的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java排序算法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • IDEA編寫SpringBoot項(xiàng)目時(shí)使用Lombok報(bào)錯(cuò)“找不到符號(hào)”的原因和解決

    IDEA編寫SpringBoot項(xiàng)目時(shí)使用Lombok報(bào)錯(cuò)“找不到符號(hào)”的原因和解決

    本文主要介紹了IDEA編寫SpringBoot項(xiàng)目時(shí)使用Lombok報(bào)錯(cuò)“找不到符號(hào)”,詳細(xì)介紹了幾種可能會(huì)出現(xiàn)的問(wèn)題及其解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • Java8特性之用Stream流代替For循環(huán)操作詳解

    Java8特性之用Stream流代替For循環(huán)操作詳解

    這篇文章主要介紹了Stream流代替For循環(huán)進(jìn)行輸出,這樣可以使代碼更簡(jiǎn)潔,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-09-09
  • IDEA怎么生成UML類圖的實(shí)現(xiàn)

    IDEA怎么生成UML類圖的實(shí)現(xiàn)

    這篇文章主要介紹了IDEA怎么生成UML類圖的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • ElasticSearch整合SpringBoot搭建配置

    ElasticSearch整合SpringBoot搭建配置

    這篇文章主要為大家介紹了ElasticSearch整合SpringBoot搭建配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • springboot整合websocket實(shí)現(xiàn)群聊思路代碼詳解

    springboot整合websocket實(shí)現(xiàn)群聊思路代碼詳解

    通過(guò)springboot引入websocket,實(shí)現(xiàn)群聊,通過(guò)在線websocket測(cè)試進(jìn)行展示。本文重點(diǎn)給大家介紹springboot整合websocket實(shí)現(xiàn)群聊功能,代碼超級(jí)簡(jiǎn)單,感興趣的朋友跟隨小編一起學(xué)習(xí)吧
    2021-05-05
  • 微服務(wù)Spring?Cloud?Alibaba?的介紹及主要功能詳解

    微服務(wù)Spring?Cloud?Alibaba?的介紹及主要功能詳解

    Spring?Cloud?是一個(gè)通用的微服務(wù)框架,適合于多種環(huán)境下的開(kāi)發(fā),而?Spring?Cloud?Alibaba?則是為阿里巴巴技術(shù)棧量身定制的解決方案,本文給大家介紹Spring?Cloud?Alibaba?的介紹及主要功能,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • SpringBoot如何使用MyBatisPlus逆向工程自動(dòng)生成代碼

    SpringBoot如何使用MyBatisPlus逆向工程自動(dòng)生成代碼

    本文介紹如何使用SpringBoot、MyBatis-Plus進(jìn)行逆向工程自動(dòng)生成代碼,并結(jié)合Swagger3.0實(shí)現(xiàn)API文檔的自動(dòng)生成和訪問(wèn),通過(guò)詳細(xì)步驟和配置,確保Swagger與SpringBoot版本兼容,并通過(guò)配置文件和測(cè)試類實(shí)現(xiàn)代碼生成和Swagger文檔的訪問(wèn)
    2024-12-12
  • SpringBoot中FailureAnalyzer的使用詳解

    SpringBoot中FailureAnalyzer的使用詳解

    這篇文章主要介紹了SpringBoot中FailureAnalyzer的使用詳解,FailureAnalyzer攔截啟動(dòng)時(shí)異常,將異常轉(zhuǎn)換成更加易讀的信息并包裝成org.springframework.boot.diagnostics.FailureAnalysis對(duì)象,監(jiān)控應(yīng)用啟動(dòng)過(guò)程,需要的朋友可以參考下
    2023-12-12
  • SpringBoot中的server.context-path的實(shí)現(xiàn)

    SpringBoot中的server.context-path的實(shí)現(xiàn)

    本文主要介紹了SpringBoot中的server.context-path的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • SpringBoot?Security的自定義異常處理

    SpringBoot?Security的自定義異常處理

    這篇文章主要介紹了SpringBoot?Security的自定義異常處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論

庆安县| 南投市| 光山县| 桂平市| 保山市| 汉中市| 若羌县| 长垣县| 汕头市| 宿迁市| 洪洞县| 寿光市| 梁平县| 犍为县| 泰宁县| 讷河市| 岱山县| 锡林浩特市| 莱西市| 长泰县| 广宗县| 阿瓦提县| 通海县| 大新县| 如皋市| 临潭县| 陇西县| 怀化市| 江山市| 隆化县| 五大连池市| 梁山县| 汉寿县| 会宁县| 调兵山市| 南城县| 嘉兴市| 东明县| 广州市| 宁强县| 达州市|