java算法面試題:排序都有哪幾種方法?請列舉。用JAVA實現一個快速排序。選擇冒泡快速集合至少4種方法排序


package com.swift;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class QuickSort {
    /*
     * 快速排序
     */
    
    public static void main(String[] args) {
        String[] strVoid = new String[] {"11", "66", "22", "0", "55", "22", "0", "32"};
        QuickSort sort = new QuickSort();
        sort.quickSort(strVoid, 0, strVoid.length - 1);
        for (int i = 0; i < strVoid.length; i++) {
            System.out.println(strVoid[i] + " ");
        }
        //用比較器排序
        List<String> list=new ArrayList<String>();
        for(String str:strVoid) {
            list.add(str);
        }
        Collections.sort(list,new Comparator<String>() {

            @Override
            public int compare(String arg0, String arg1) {
                int num=arg1.compareTo(arg0);
                return num;
            }
            
        });
        for(String str:list) {
            System.out.print(str+" | ");
        }
    }
    
    public void quickSort(String[] strDate, int left, int right) {
        String middle, tempDate;
        int i, j;
        i = left;
        j = right;
        middle = strDate[(i + j) / 2];
        do {
            while (strDate[i].compareTo(middle) < 0 && i < right)
                i++; // 找出左邊比中間值大的數
            while (strDate[j].compareTo(middle) > 0 && j > left)
                j--; // 找出右邊比中間值小的數
            if (i <= j) { // 將左邊大的數和右邊小的數進行替換
                tempDate = strDate[i];
                strDate[i] = strDate[j];
                strDate[j] = tempDate;
                i++;
                j--;
            }
        } while (i <= j); // 當兩者交錯時停止

        if (i < right) {
            quickSort(strDate, i, right);//
        }
        if (j > left) {
            quickSort(strDate, left, j);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM