zoukankan      html  css  js  c++  java
  • 快排和归并排

    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++];
    }
    }


    }
  • 相关阅读:
    【C语言期末实训】学生学籍管理系统
    【我要学python】愣头青之小数点精度控制
    【吾爱破解第二期】操作学习笔记
    【吾爱破解第一期】破解基础知识之认识壳与程序的特征
    【linux入门必备】小白需要掌握的基础知识_不定期更新
    【我要学python】爬虫准备之了解基本的html标签
    【我要学python】愣头青之初安装就打了一记耳光
    【我要学python】MethodType和isinstance和Type函数
    【我要学python】面对对象编程之继承和多态
    【我要学python】open函数的简单用法
  • 原文地址:https://www.cnblogs.com/james111/p/6953167.html
Copyright © 2011-2022 走看看