冒泡、选择、插入、希尔、快速、归并、堆排序算法的实现

常见排序算法及其对应的时间复杂度和空间复杂度

排序方法 时间复杂度(平均) 时间复杂度(最坏) 时间复杂度(最好) 空间复杂度 稳定性 复杂性
直接插入排序 O ( n 2 ) O(n^2) O(n2) O ( n 2 ) O(n^2) O(n2) O ( n ) O(n) O(n) O ( 1 ) O(1) O(1) 稳定 简单
希尔排序 O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( n 2 ) O(n^2) O(n2) O ( n 1.3 ) O(n^{1.3}) O(n1.3) O ( 1 ) O(1) O(1) 不稳定 较复杂
直接选择排序 O ( n 2 ) O(n^2) O(n2) O ( n 2 ) O(n^2) O(n2) O ( n 2 ) O(n^2) O(n2) O ( 1 ) O(1) O(1) 不稳定 简单
堆排序 O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( 1 ) O(1) O(1) 不稳定 较复杂
冒泡排序 O ( n 2 ) O(n^2) O(n2) O ( n 2 ) O(n^2) O(n2) O ( n ) O(n) O(n) O ( 1 ) O(1) O(1) 稳定 简单
快速排序 O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( n 2 ) O(n^2) O(n2) O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) 不稳定 较复杂
归并排序 O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( n l o g 2 n ) O(nlog_2n) O(nlog2n) O ( n ) O(n) O(n) 稳定 较复杂

冒泡排序

import java.util.Scanner;

public class Bubble {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        int[] arr = new int[len];
        for (int i = 0;i < len;i++) {
            arr[i] = scanner.nextInt();
        }

        for (int i = 0;i < len;i++) {
            for (int j = 0;j < len - i - 1;j++) {
                if (arr[j] > arr[j+1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }

        for (int i = 0;i < len;i++) {
            System.out.printf("%d ",arr[i]);
        }
    }

}

选择排序

import java.util.Scanner;

public class Selection4 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        int[] arr = new int[len];
        for (int i = 0;i < len;i++) {
            arr[i] = scanner.nextInt();
        }

        for (int i = 0;i < len;i++) {
            int max = i;
            for (int j = i + 1;j < len;j++) {
                if (arr[max] > arr[j]) {
                    max = j;
                }
            }
            if (max != i) {
                int tmp = arr[max];
                arr[max] = arr[i];
                arr[i] = tmp;
            }
        }

        for (int i = 0;i < len;i++) {
            System.out.printf("%d ",arr[i]);
        }
    }

}

插入排序

import java.util.Scanner;

public class Insertion4 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        int[] arr = new int[len];
        for (int i = 0;i < len;i++) {
            arr[i] = scanner.nextInt();
        }

        for (int i = 0;i < len;i++) {
            for (int j = i;j >= 1;j--) {
                if (arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = tmp;
                }
            }
        }

        for (int i = 0;i < len;i++) {
            System.out.printf("%d ",arr[i]);
        }
    }

}

希尔排序

import java.util.Scanner;

public class Shell4 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        int[] arr = new int[len];
        for (int i = 0;i < len;i++) {
            arr[i] = scanner.nextInt();
        }

        for (int gap = len / 2;gap >= 1;gap /= 2) {
            for (int i = gap;i < len;i++) {
                for (int j = i;j >= gap;j -= gap) {
                    if (arr[j] < arr[j-gap]) {
                        int tmp = arr[j];
                        arr[j] = arr[j-gap];
                        arr[j-gap] = tmp;
                    }
                }
            }
        }

        for (int i = 0;i < len;i++) {
            System.out.printf("%d ",arr[i]);
        }
    }

}

快速排序

import java.util.Scanner;

public class Quick4 {

    static void swap(int[] arr, int a, int b) {
        int tmp = arr[a];
        arr[a] = arr[b];
        arr[b] = tmp;
    }

    static int parition(int[] arr, int l, int r) {
        swap(arr,l,(int)Math.random()*(r-l+1)+l);
        int i = l;
        int v = arr[l];
        for (int j = l+1;j <= r;j++) {
            if (arr[j] < v) {
                i++;
                swap(arr,i,j);
            }
        }
        swap(arr,i,l);
        return i;
    }

    static void sort(int[] arr, int l, int r) {
        if (l >= r) return;
        int p = parition(arr,l,r);
        sort(arr,l,p-1);
        sort(arr,p+1,r);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        int[] arr = new int[len];
        for (int i = 0;i < len;i++) {
            arr[i] = scanner.nextInt();
        }

        sort(arr,0,len-1);

        for (int i = 0;i < len;i++) {
            System.out.printf("%d ",arr[i]);
        }
    }

}

归并排序

import java.util.Arrays;
import java.util.Scanner;

public class Merge4 {

    static void sort(int[] arr, int l, int mid, int r) {
        int[] tmp = Arrays.copyOfRange(arr,l,r+1);
        int i = l;
        int j = mid + 1;
        for (int k = l;k <= r;k++) {
            if (i > mid) {
                arr[k] = tmp[j-l];
                j++;
            } else if (j > r) {
                arr[k] = tmp[i-l];
                i++;
            } else if (tmp[i-l] < tmp[j-l]) {
                arr[k] = tmp[i-l];
                i++;
            } else {
                arr[k] = tmp[j-l];
                j++;
            }
        }
    }

    static void sort(int[] arr, int l, int r) {
        if (l >= r) return;
        int mid = (l + r) / 2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        if (arr[mid] > arr[mid+1]) {
            sort(arr,l,mid,r);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        int[] arr = new int[len];
        for (int i = 0;i < len;i++) {
            arr[i] = scanner.nextInt();
        }

        sort(arr,0,len-1);

        for (int i = 0;i < len;i++) {
            System.out.printf("%d ",arr[i]);
        }
    }

}

堆排序

import java.util.Scanner;

public class Heap4 {

    static void swap(int[] arr, int a, int b) {
        int tmp = arr[a];
        arr[a] = arr[b];
        arr[b] = tmp;
    }

    static void maxHeapify(int[] arr, int i, int heapsize) {
        int l = 2 * i + 1;
        int r = 2 * i + 2;
        int largest = i;
        if (l < heapsize && arr[l] > arr[largest]) largest = l;
        if (r < heapsize && arr[r] > arr[largest]) largest = r;
        if (largest != i) {
            swap(arr,largest,i);
            maxHeapify(arr,largest,heapsize);
        }
    }

    static void buildMaxHeap(int[] arr, int heapsize) {
        for (int i = heapsize / 2;i >= 0;i--) {
            maxHeapify(arr,i,heapsize);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        int[] arr = new int[len];
        for (int i = 0;i < len;i++) {
            arr[i] = scanner.nextInt();
        }

        int heapsize = len;
        buildMaxHeap(arr,heapsize);
        for (int i = 0;i < len;i++) {
            heapsize--;
            swap(arr,0,heapsize);
            maxHeapify(arr,0,heapsize);
        }

        for (int i = 0;i < len;i++) {
            System.out.printf("%d ",arr[i]);
        }
    }

}

你可能感兴趣的:(算法,排序算法,快速排序,heap,algorithm,插入排序)