亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實(shí)現(xiàn)八個(gè)常用的排序算法:插入排序、冒泡排序、選擇排序、希爾排序等

 更新時(shí)間:2021年08月27日 09:10:57   作者:Terry_小三哥  
這篇文章主要介紹了Java如何實(shí)現(xiàn)八個(gè)常用的排序算法:插入排序、冒泡排序、選擇排序、希爾排序 、快速排序、歸并排序、堆排序和LST基數(shù)排序,需要的朋友可以參考下

本文實(shí)現(xiàn)了八個(gè)常用的排序算法:插入排序、冒泡排序、選擇排序、希爾排序 、快速排序、歸并排序、堆排序和LST基數(shù)排序

首先是EightAlgorithms.java文件,代碼如下:

import java.util.Arrays; 
/* 
 * 實(shí)現(xiàn)了八個(gè)常用的排序算法:插入排序、冒泡排序、選擇排序、希爾排序 
 * 以及快速排序、歸并排序、堆排序和LST基數(shù)排序 
 * @author gkh178 
 */ 
public class EightAlgorithms { 
   
  //插入排序:時(shí)間復(fù)雜度o(n^2)  
  public static void insertSort(int a[], int n) { 
    for (int i = 1; i < n; ++i) { 
      int temp = a[i]; 
      int j = i - 1; 
      while (j >= 0 && a[j] > temp) { 
        a[j + 1] =a[j]; 
        --j; 
      } 
      a[j + 1] = temp; 
    } 
  } 
   
  //冒泡排序:時(shí)間復(fù)雜度o(n^2)  
  public static void bubbleSort(int a[], int n) { 
    for (int i = n - 1; i > 0; --i) { 
      for (int j = 0; j < i; ++j) { 
        if (a[j] > a[j + 1]) { 
          int temp = a[j]; 
          a[j] = a[j + 1]; 
          a[j + 1] = temp;     
        } 
      }   
    }   
  } 
   
  //選擇排序:時(shí)間復(fù)雜度o(n^2)  
  public static void selectSort(int a[], int n) { 
    for (int i = 0; i < n - 1; ++i) { 
      int min = a[i]; 
      int index = i; 
      for (int j = i + 1; j < n; ++j) { 
        if (a[j] < min) { 
          min = a[j]; 
          index = j; 
        }   
      } 
      a[index] = a[i]; 
      a[i] = min; 
    } 
  } 
   
  //希爾排序:時(shí)間復(fù)雜度介于o(n^2)和o(nlgn)之間  
  public static void shellSort(int a[], int n) { 
    for (int gap = n / 2; gap >= 1; gap /= 2) { 
      for (int i = gap; i < n; ++i) { 
        int temp = a[i]; 
        int j = i -gap; 
        while (j >= 0 && a[j] > temp) { 
          a[j + gap] = a[j]; 
          j -= gap; 
        } 
        a[j + gap] = temp; 
      } 
    }   
  } 
   
  //快速排序:時(shí)間復(fù)雜度o(nlgn)  
  public static void quickSort(int a[], int n) { 
    _quickSort(a, 0, n-1); 
  } 
  public static void _quickSort(int a[], int left, int right) { 
    if (left < right) { 
      int q = _partition(a, left, right); 
      _quickSort(a, left, q - 1); 
      _quickSort(a, q + 1, right); 
    } 
  } 
  public static int _partition(int a[], int left, int right) { 
    int pivot = a[left]; 
    while (left < right) { 
      while (left < right && a[right] >= pivot) { 
        --right; 
      } 
      a[left] = a[right]; 
      while (left <right && a[left] <= pivot) { 
        ++left; 
      } 
      a[right] = a[left]; 
    } 
    a[left] = pivot; 
    return left; 
  } 
   
  //歸并排序:時(shí)間復(fù)雜度o(nlgn)  
  public static void mergeSort(int a[], int n) { 
    _mergeSort(a, 0 , n-1); 
  } 
  public static void _mergeSort(int a[], int left, int right) { 
    if (left <right) { 
      int mid = left + (right - left) / 2; 
      _mergeSort(a, left, mid); 
      _mergeSort(a, mid + 1, right); 
      _merge(a, left, mid, right); 
    } 
  } 
  public static void _merge(int a[], int left, int mid, int right) { 
    int length = right - left + 1; 
    int newA[] = new int[length]; 
    for (int i = 0, j = left; i <= length - 1; ++i, ++j) { 
      newA[i] = a[j]; 
    } 
    int i = 0; 
    int j = mid -left + 1; 
    int k = left; 
    for (; i <= mid - left && j <= length - 1; ++k) { 
      if (newA[i] < newA[j]) { 
        a[k] = newA[i++]; 
      } 
      else { 
        a[k] = newA[j++]; 
      } 
    } 
    while (i <= mid - left) { 
      a[k++] = newA[i++]; 
    } 
    while (j <= right - left) { 
      a[k++] = newA[j++]; 
    } 
  } 
   
  //堆排序:時(shí)間復(fù)雜度o(nlgn)  
  public static void heapSort(int a[], int n) { 
    builtMaxHeap(a, n);//建立初始大根堆 
    //交換首尾元素,并對交換后排除尾元素的數(shù)組進(jìn)行一次上調(diào)整 
    for (int i = n - 1; i >= 1; --i) { 
      int temp = a[0]; 
      a[0] = a[i]; 
      a[i] = temp; 
      upAdjust(a, i); 
    } 
  } 
  //建立一個(gè)長度為n的大根堆 
  public static void builtMaxHeap(int a[], int n) { 
    upAdjust(a, n); 
  } 
  //對長度為n的數(shù)組進(jìn)行一次上調(diào)整 
  public static void upAdjust(int a[], int n) { 
    //對每個(gè)帶有子女節(jié)點(diǎn)的元素遍歷處理,從后到根節(jié)點(diǎn)位置 
    for (int i = n / 2; i >= 1; --i) { 
      adjustNode(a, n, i); 
    } 
  } 
  //調(diào)整序號為i的節(jié)點(diǎn)的值 
  public static void adjustNode(int a[], int n, int i) { 
    //節(jié)點(diǎn)有左右孩子 
    if (2 * i + 1 <= n) { 
      //右孩子的值大于節(jié)點(diǎn)的值,交換它們 
      if (a[2 * i] > a[i - 1]) { 
        int temp = a[2 * i]; 
        a[2 * i] = a[i - 1]; 
        a[i - 1] = temp; 
      } 
      //左孩子的值大于節(jié)點(diǎn)的值,交換它們 
      if (a[2 * i -1] > a[i - 1]) { 
        int temp = a[2 * i - 1]; 
        a[2 * i - 1] = a[i - 1]; 
        a[i - 1] = temp; 
      } 
      //對節(jié)點(diǎn)的左右孩子的根節(jié)點(diǎn)進(jìn)行調(diào)整 
      adjustNode(a, n, 2 * i); 
      adjustNode(a, n, 2 * i + 1); 
    } 
    //節(jié)點(diǎn)只有左孩子,為最后一個(gè)有左右孩子的節(jié)點(diǎn) 
    else if (2 * i == n) { 
      //左孩子的值大于節(jié)點(diǎn)的值,交換它們 
      if (a[2 * i -1] > a[i - 1]) { 
        int temp = a[2 * i - 1]; 
        a[2 * i - 1] = a[i - 1]; 
        a[i - 1] = temp; 
      }   
    } 
  } 
   
  //基數(shù)排序的時(shí)間復(fù)雜度為o(distance(n+radix)),distance為位數(shù),n為數(shù)組個(gè)數(shù),radix為基數(shù) 
  //本方法是用LST方法進(jìn)行基數(shù)排序,MST方法不包含在內(nèi) 
  //其中參數(shù)radix為基數(shù),一般為10;distance表示待排序的數(shù)組的數(shù)字最長的位數(shù);n為數(shù)組的長度 
  public static void lstRadixSort(int a[], int n, int radix, int distance) { 
    int[] newA = new int[n];//用于暫存數(shù)組 
    int[] count = new int[radix];//用于計(jì)數(shù)排序,保存的是當(dāng)前位的值為0 到 radix-1的元素出現(xiàn)的的個(gè)數(shù) 
    int divide = 1; 
    //從倒數(shù)第一位處理到第一位 
    for (int i = 0; i < distance; ++i) { 
      System.arraycopy(a, 0, newA, 0, n);//待排數(shù)組拷貝到newA數(shù)組中 
      Arrays.fill(count, 0);//將計(jì)數(shù)數(shù)組置0 
      for (int j = 0; j < n; ++j) { 
        int radixKey = (newA[j] / divide) % radix; //得到數(shù)組元素的當(dāng)前處理位的值 
        count[radixKey]++; 
      } 
      //此時(shí)count[]中每個(gè)元素保存的是radixKey位出現(xiàn)的次數(shù) 
      //計(jì)算每個(gè)radixKey在數(shù)組中的結(jié)束位置,位置序號范圍為1-n 
      for (int j = 1; j < radix; ++j) { 
        count[j] = count[j] + count[j - 1]; 
      } 
      //運(yùn)用計(jì)數(shù)排序的原理實(shí)現(xiàn)一次排序,排序后的數(shù)組輸出到a[] 
      for (int j = n - 1; j >= 0; --j) { 
        int radixKey = (newA[j] / divide) % radix; 
        a[count[radixKey] - 1] = newA[j]; 
        --count[radixKey]; 
      } 
      divide = divide * radix; 
    } 
  } 
} 

然后測試代碼TestEightAlgorithms.java,代碼如下:

public class TestEightAlgorithms { 
 
  public static void printArray(int a[], int n) { 
    for (int i = 0; i < n; ++i) { 
      System.out.print(a[i] + " "); 
      if ( i == n - 1) { 
        System.out.println(); 
      } 
    } 
  } 
   
  public static void main(String[] args) { 
    for (int i = 1; i <= 8; ++i) { 
      int arr[] = {45, 38, 26, 77, 128, 38, 25, 444, 61, 153, 9999, 1012, 43, 128}; 
      switch(i) { 
      case 1: 
        EightAlgorithms.insertSort(arr, arr.length); 
        break; 
      case 2: 
        EightAlgorithms.bubbleSort(arr, arr.length); 
        break; 
      case 3: 
        EightAlgorithms.selectSort(arr, arr.length); 
        break; 
      case 4: 
        EightAlgorithms.shellSort(arr, arr.length); 
        break; 
      case 5: 
        EightAlgorithms.quickSort(arr, arr.length); 
        break; 
      case 6: 
        EightAlgorithms.mergeSort(arr, arr.length); 
        break; 
      case 7: 
        EightAlgorithms.heapSort(arr, arr.length); 
        break; 
      case 8: 
        EightAlgorithms.lstRadixSort(arr, arr.length, 10, 4); 
        break; 
      default: 
        break; 
      } 
      printArray(arr, arr.length); 
    } 
  } 
} 

最后是運(yùn)行結(jié)果如下:

以上就是Java實(shí)現(xiàn)八個(gè)常用的排序算法的全部代碼,希望大家對C++排序算法有更進(jìn)一步的了解。

相關(guān)文章

  • Spring?Boot3?跨域配置?Cors的方式

    Spring?Boot3?跨域配置?Cors的方式

    這篇文章主要介紹了Spring?Boot3?跨域配置?Cors,通過使用CORS,開發(fā)人員可以控制哪些外部網(wǎng)頁可以訪問他們的資源,從而提高應(yīng)用程序的安全性,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • Java基礎(chǔ)之詳解HashSet的使用方法

    Java基礎(chǔ)之詳解HashSet的使用方法

    今天給大家?guī)淼氖顷P(guān)于Java基礎(chǔ)的相關(guān)知識,文章圍繞著HashSet的使用方法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Springboot之idea之pom文件圖標(biāo)不對問題

    Springboot之idea之pom文件圖標(biāo)不對問題

    這篇文章主要介紹了Springboot之idea之pom文件圖標(biāo)不對問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • mybatis-plus批量插入優(yōu)化方式

    mybatis-plus批量插入優(yōu)化方式

    MyBatis-Plus的saveBatch()方法默認(rèn)是單條插入,通過在JDBC URL添加rewriteBatchedStatements=true參數(shù)啟用批量插入,官方提供的sql注入器可自定義方法,如InsertBatchSomeColumn實(shí)現(xiàn)真批量插入,但存在單次插入數(shù)據(jù)量過大問題,可通過分批插入優(yōu)化,避免超出MySQL限制
    2024-09-09
  • spring初始化源碼代碼淺析

    spring初始化源碼代碼淺析

    Spring框架被廣泛應(yīng)用于我們的日常工作中,但是很長時(shí)間以來我們都是只會使用,不懂它的作用原理,下面這篇文章主要給大家介紹了關(guān)于spring初始化源碼的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延時(shí)隊(duì)列的方案

    SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延時(shí)隊(duì)列的方案

    在很多的業(yè)務(wù)場景中,延時(shí)隊(duì)列可以實(shí)現(xiàn)很多功能,此類業(yè)務(wù)中,一般上是非實(shí)時(shí)的,需要延遲處理的,需要進(jìn)行重試補(bǔ)償?shù)?本文給大家介紹了SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊(duì)列的方案,文中有詳細(xì)的代碼講解,需要的朋友可以參考下
    2024-04-04
  • SpringMVC實(shí)現(xiàn)RESTful風(fēng)格:@PathVariable注解的使用方式

    SpringMVC實(shí)現(xiàn)RESTful風(fēng)格:@PathVariable注解的使用方式

    這篇文章主要介紹了SpringMVC實(shí)現(xiàn)RESTful風(fēng)格:@PathVariable注解的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • JavaWeb之Filter過濾器詳解

    JavaWeb之Filter過濾器詳解

    本篇文章主要介紹了JavaWeb之Filter過濾器詳解,實(shí)例分析了JavaWeb之Filter過濾器的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-03-03
  • 解決BeanUtils.copyProperties無法成功封裝的問題

    解決BeanUtils.copyProperties無法成功封裝的問題

    這篇文章主要介紹了解決BeanUtils.copyProperties無法成功封裝的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java中IO流使用FileWriter寫數(shù)據(jù)基本操作詳解

    Java中IO流使用FileWriter寫數(shù)據(jù)基本操作詳解

    這篇文章主要介紹了Java中IO流FileWriter寫數(shù)據(jù)操作,FileWriter類提供了多種寫入字符的方法,包括寫入單個(gè)字符、寫入字符數(shù)組和寫入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下
    2023-10-10

最新評論