• 快排和归并排


    1.快排:

    Quicksort is a divide and conquer algorithm. It first divides a large list into two smaller sub-lists and then recursively sort the two sub-lists. If we want to sort an array without any extra space, quicksort is a good option. On average, time complexity is O(n log(n)).

    The basic step of sorting an array are as follows:

    • Select a pivot, normally the middle one
    • From both ends, swap elements and make left elements < pivot and all right > pivot
    • Recursively sort left part and right part
    import java.util.*;
    public class Main {
    public static void main(String[] args) {
    int[] arr={23,45,12,6,9,43,57,98,86,24};
    quickSort(arr);
    for(int i:arr){
    System.out.print(i+" ");
    }
    }

    private static void quickSort(int[] arr) {
    int low=0;
    int high=arr.length-1;
    sort(low,high,arr);
    }

    private static void sort(int low, int high, int[] arr) {
    if(arr == null || arr.length==0){ //consider empty array
    return;
    }
    if(low>=high){ //consider just one element in the array
    return;
    }
    int i=low;
    int j=high;
    int pivot=arr[(low+high)/2]; //make the middle position as pivot
    while(i<=j){ //left elements < pivot and all right > pivot
    while(arr[i]<pivot){
    i++;
    }
    while(arr[j]>pivot){
    j--;
    }
    if(i<=j){
    int temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    i++;
    j--;
    }
    }
    if(low<j){ //recursively invoke sort()
    sort(low,j,arr);
    }
    if(high>i){
    sort(i,high,arr);
    }
    }
    }

    2.mergeSort: Merge sort is a divide and conquer algorithm.

    Steps to implement Merge Sort:
    1) Divide the unsorted array into n partitions, each partition contains 1 element. Here the one element is considered as sorted.
    2) Repeatedly merge(归并) partitioned units to produce new sublists until there is only 1 sublist remaining. This will be the sorted list at the end. 

    Merge sort is a fast, stable sorting routine with guaranteed O(n*log(n)) efficiency. When sorting arrays, merge sort requires additional scratch space proportional to the size of the input array. Merge sort is relatively simple to code and offers performance typically only slightly below that of quicksort. 

    import java.util.*;

    public class Main {
    public static void main(String[] args) {
    int[] data = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 };
    mergeSort(data);
    for (int i = 0; i < data.length; i++) {
    System.out.print(data[i] + " ");
    }
    }

    public static void mergeSort(int[] data) {
    sort(data, 0, data.length - 1);
    }

    public static void sort(int[] data, int left, int right) {
    if (left >= right) //if divided to only one element
    return;
    int middle = (left + right) / 2;
    sort(data, left, middle); //first divided then merge
    sort(data, middle + 1, right);
    merge(data, left, middle, right);
    }

    public static void merge(int[] data, int left, int middle, int right) {
    int[] temp = new int[data.length];
    int i=left;
    int j = middle + 1;
    int k = left;
    int tmp = left;
    while (i <= middle && j <= right) {
    if (data[i] <= data[j]) {
    temp[k++] = data[i++];
    } else {
    temp[k++] = data[j++];
    }
    }
    while (j <= right) {
    temp[k++] = data[j++];
    }
    while (i <= middle) {
    temp[k++] = data[i++];
    }
    while (tmp <= right) { //finally,should copy temp array to data
    data[tmp] = temp[tmp++];
    }
    }


    }
  • 相关阅读:
    Hibernate初级
    Servlet, Listener 、 Filter.
    DBCP数据源
    数据库连接池
    MySQL入门笔记
    20170330 webservice代理类测试
    20170330 ABAP代理生成
    20170329 隐士增强问题
    ABAP rfc 发布webservice 错误
    ABAP 性能优化001
  • 原文地址:https://www.cnblogs.com/james111/p/6953167.html
Copyright © 2020-2023  润新知