午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    java多種排序方法代碼集合
    來源:易賢網(wǎng) 閱讀:1348 次 日期:2015-04-16 11:36:33
    溫馨提示:易賢網(wǎng)小編為您整理了“java多種排序方法代碼集合”,方便廣大網(wǎng)友查閱!

    最近在學(xué)習(xí)java,想練習(xí)一下編寫,使用了eclipse編寫了如下的排序代碼。其中包含了快速排序, 選擇排序,插入排序,冒泡排序。

    /*

    *

    * Roc

    *

    * 2013/10/4

    *

    * 此段代碼主要是通過java來實現(xiàn)多種排序

    *

    */

    package Sorting;

    import java.util.Scanner;

    public class sorting {

    //直接插入排序

    static void InsertSort(int a[]){

    for (int i = 1; i < a.length; i++){

    int t = a[i];

    int j = i;

    while ((j > 0) && (a[j-1] > t)){

    a[j] = a[j-1];

    --j;

    }

    a[j] = t;

    }

    }

    //快速排序

    static void QuickSort(int a[], int low, int high){

    int index[] = new int[2];//用于保存劃分方法返回的結(jié)果

    if (low < high){

    Partition(a, low, high, index);//劃分L[low..high]

    QuickSort(a, low, index[0]);

    QuickSort(a, index[1], high);

    }

    }

    static void Partition(int a[], int p, int r, int ind[]){

    int i, j, k, pivot;

    pivot = a[(p+r)/2];

    i = p;

    j = p;

    k = r;

    while (j != k){

    if (a[j] == pivot){

    j++;

    } else if (a[j] < pivot){

    swap(a[j], a[i]);

    j++;

    i++;

    } else {

    swap(a[j], a[k-1]);

    k--;

    }

    }

    ind[0] = i;

    ind[1] = j;

    }

    //用于整數(shù)之間進(jìn)行的交換

    static void swap(int a, int b){

    int temp;

    temp = a;

    a = b;

    b = temp;

    }

    //選擇排序

    static void SelectionSort(int a[]){

    int min;

    for (int j = 0; j < a.length-1; j++){

    min = j;

    for (int k = j+1; k < a.length; k++){

    if (a[k] < a[min])

    min = k;

    }

    swap(a[min], a[j]);

    }

    }

    //冒泡排序

    static void BubbleSort(int a[]){

    for (int i = 0; i < a.length; i++){

    for (int j = 0; j < a.length-i-1; j++){

    if (a[j] > a[j+1])

    swap(a[j], a[j+1]);

    }

    }

    }

    static void PrintThem(int a[]){

    System.out.println("\nAfter sorted, they are: ");

    for (int i = 0; i < a.length; i++)

    System.out.print(a[i]+" ");

    }

    public static void main(String[] args){

    int number1, number2;

    int low, high;

    Scanner reader = new Scanner(System.in);

    System.out.print("How many numbers you want to sort: ");

    number1 = reader.nextInt();

    int a[] = new int[number1];

    System.out.println("\nNow, input them here: ");

    for (int i = 0; i < a.length; i++)

    a[i] = reader.nextInt();

    System.out.println("\t\tChose one of way to sort them: ");

    System.out.println("\t1.Insert Sort \t 2.Quick Sort \t " +

    "3.Select Sort \t 4.Bubble Sort \t 0.Abort");

    System.out.print("Input your chose here: ");

    number2 = reader.nextInt();

    while (number2 != 0){

    switch (number2){

    case 1:

    InsertSort(a);

    PrintThem(a);

    break;

    case 2:

    low = 0;

    high = a.length;

    QuickSort(a, low, high);

    PrintThem(a);

    break;

    case 3:

    SelectionSort(a);

    PrintThem(a);

    break;

    case 4:

    BubbleSort(a);

    PrintThem(a);

    break;

    default:

    System.out.println("Input error! Try later!");

    break;

    }

    System.out.println("\n\n1.Insert Sort \t 2.Quick Sort \t" +

    " 3.Select Sort \t 4.Bubble Sort \t 0.Abort");

    System.out.print("Input your chose here: ");

    number2 = reader.nextInt();

    }

    if (number2 == 0)

    System.out.print("\tBye~Bye~");

    }

    }

    更多信息請查看IT技術(shù)專欄

    更多信息請查看技術(shù)文章
    易賢網(wǎng)手機(jī)網(wǎng)站地址:java多種排序方法代碼集合
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機(jī)號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)