日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 排序

發布時間:2024/4/15 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、冒泡排序

偽代碼:

i∈[0,N-1) //循環N-1遍
  j∈[0,N-1-i) //每遍循環要處理的無序部分
    if a[j] > a[j+1]
    swap(j,j+1) //兩兩排序(升序/降序)

?

2、插入排序

?

3、歸并排序

?

4、快速排序

步驟為:

  • 從數列中挑出一個元素,稱為"基準"(pivot),
  • 重新排序數列,所有比基準值小的元素擺放在基準前面,所有比基準值大的元素擺在基準后面(相同的數可以到任何一邊)。在這個分區結束之后,該基準就處于數列的中間位置。這個稱為分區(partition)操作。
  • 遞歸地(recursively)把小于基準值元素的子數列和大于基準值元素的子數列排序
  • ?

    5、選擇排序

    每次從數組中選出最小或者最大的,然后依次放在前面

    ?

    以上排序代碼如下:

    public class Sort{public static void main(String [] args) {Integer [] a = {9,8,5,1,3,4};Double [] b = {5.54,8.2,7.22,15.22,71.0}; // bubbleSort(a); // bubbleSort(b);// InsertSort(a); // InsertSort(b);// mergeSort(a);// mergeSort(b);// quickSort(a); // quickSort(b); selectSort(a);selectSort(b);for(int i=0; i<a.length; i++) {System.out.print(a[i] + " ");}System.out.println();for(int i=0; i<b.length; i++) {System.out.print(b[i] + " ");}}//冒泡排序public static <T extends Comparable<? super T>> void bubbleSort(T [] a) {for(int i=0; i<a.length-1; i++) {for(int j=0; j<a.length-1-i; j++) {if(a[j].compareTo(a[j+1]) > 0) {swap(a, j, j+1);}}} }//插入排序public static <T extends Comparable<? super T>> void InsertSort(T [] a) {for(int i=1; i<a.length; i++) {T currentElement = a[i];int j;for(j=i-1; j>=0 && a[j].compareTo(currentElement) > 0; j--) {a[j+1] = a[j];}a[j+1] = currentElement;}}//歸并排序public static <T extends Comparable<? super T>> void mergeSort(T [] a) {if(a.length > 1) {T [] firstHalf = (T[]) new Comparable[a.length/2];System.arraycopy(a, 0, firstHalf, 0, a.length/2);mergeSort(firstHalf);int secondHalfLength = a.length - a.length / 2;T [] secondHalf = (T[]) new Comparable[secondHalfLength];System.arraycopy(a, a.length/2, secondHalf, 0, secondHalfLength);mergeSort(secondHalf);merge(firstHalf, secondHalf, a);}}public static <T extends Comparable<? super T>> void merge(T [] firstHalf, T [] secondHalf,T [] a) {// TODO Auto-generated method stubint current1;int current2;int current3;current1 = current2 = current3 = 0;while(current1 < firstHalf.length && current2 < secondHalf.length) {if(firstHalf[current1].compareTo(secondHalf[current2]) < 0 ) {a[current3++] = firstHalf[current1++];}else {a[current3++] = secondHalf[current2++];}}while(current1 < firstHalf.length) {a[current3++] = firstHalf[current1++];}while(current2 < secondHalf.length) {a[current3++] = secondHalf[current2++];}}//快速排序public static <T extends Comparable<? super T>> void quickSort(T [] a) {qsort(a,0,a.length-1);}private static <T extends Comparable<? super T>> void qsort(T[] a, int left, int right) {// TODO Auto-generated method stubif(left < right) {int pivot = partition(a,left,right);qsort(a, left, pivot-1);qsort(a, pivot+1, right);}}//快速排序 中的 找出 把數組分成兩部分的 那個下標private static <T extends Comparable<? super T>> int partition(T[] a, int left, int right) {// TODO Auto-generated method stubT pivot = a[left];while(left < right) {while(left < right && a[right].compareTo(pivot)>=0) {right--;}a[left] = a[right];while(left < right && a[left].compareTo(pivot)<=0) {left++;}a[right] = a[left];}a[left] = pivot;return left;}public static <T extends Comparable<? super T>> void selectSort(T [] a){T min;int minIndex;for(int i=0; i<a.length-1; i++) {min = a[i];minIndex = i;for(int j=i+1; j<a.length; j++) {if(min.compareTo(a[j])>0) {min = a[j];minIndex = j;}}swap(a, minIndex, i);}}private static <T> void swap(T[] a, int i, int j) {// TODO Auto-generated method stubT temp = a[i];a[i] = a[j];a[j] = temp;}}

    ?

    轉載于:https://www.cnblogs.com/zquan/p/9372544.html

    總結

    以上是生活随笔為你收集整理的Java 排序的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。