排序算法(java)

10大排序算法详解

冒泡排序

package com.yzq.practice;

import java.util.Arrays;

public class BubbleSort {
  public static void bubbleSort(int[] array) {
    int length = array.length;
    int temp;
    for (int i = 0; i < length; i++)
      for (int j = 0; j < length - 1 - i; j++) {
        if (array[j] > array[j + 1]) {
          temp = array[j];
          array[j] = array[j + 1];
          array[j + 1] = temp;
        }
      }
  }

  // pos记录最后一次交换的位置
  public static void bubbleSort2(int[] array) {
    int i = array.length - 1;
    int temp;
    while (i > 0) {
      int pos = 0;
      for (int j = 0; j < i; j++)
        if (array[j] > array[j + 1]) {
          pos = j;
          temp = array[j];
          array[j] = array[j + 1];
          array[j + 1] = temp;
        }
      i = pos;
    }
  }

  public static void main(String[] args) {
    int[] b = {10, 43, 52, 6, 8, 2, 23, 67, 89, 90, 27};
    // bubbleSort(b);
    bubbleSort2(b);
    System.out.println(Arrays.toString(b));
  }
}

快速排序

package com.yzq.practice;

import java.util.Arrays;

public class QuickSort {
  public static int partition(int[] array, int low, int high) {
    // 三数取中
    int mid = low + (high - low) / 2;
    int temp;
    if (array[mid] > array[high]) {
      temp = array[mid];
      array[mid] = array[high];
      array[high] = temp;
    }
    if (array[mid] > array[low]) {
      temp = array[mid];
      array[mid] = array[low];
      array[low] = temp;
    }
    if (array[low] > array[high]) {
      temp = array[low];
      array[low] = array[high];
      array[high] = temp;
    }

    int key = array[low];

    while (low < high) {
      while (array[high] >= key && high > low) high--;
      array[low] = array[high];
      while (array[low] <= key && low < high) low++;
      array[high] = array[low];
    }
    array[high] = key;
    return high;
  }

  public static void sort(int[] array, int low, int high) {
    if (low >= high) return;
    int index = partition(array, low, high);
    sort(array, low, index);
    sort(array, index + 1, high);
  }

  public static void main(String[] args) {
    int[] a = {10, 43, 52, 6, 8, 2, 23, 1, 67, 89, 90, 27};
    sort(a, 0, a.length - 1);
    System.out.println(Arrays.toString(a));
  }
}

插入排序

package com.yzq.practice;

import java.util.Arrays;

public class InsertionSort {
  public static void insertionSort(int[] array) {
    int length = array.length;
    for (int i = 1; i < length; i++) {
      int key = array[i];
      int j = i - 1;
      while (j >= 0 && array[j] > key) {
        array[j + 1] = array[j];
        j--;
      }
      array[j + 1] = key;
    }
  }

  public static void binaryInsertionSort(int[] array) {
    int length = array.length;
    for (int i = 1; i < length; i++) {
      int key = array[i], left = 0, right = i - 1;
      while (left <= right) {
        int mid = (left + right) / 2;
        if (key < array[mid]) right = mid - 1;
        else left = mid + 1;
      }

      for (int j = i - 1; j >= left; j--) array[j + 1] = array[j];

      array[left] = key;
    }
  }

  public static void main(String[] args) {
    int[] a = {10, 43, 52, 6, 8, 2, 23, 1, 67, 89, 90, 27};
    // insertionSort(a);
    binaryInsertionSort(a);
    System.out.println(Arrays.toString(a));
  }
}

归并排序

package com.yzq.practice;

public class MergeSort {

  public static void merge(int[] arr, int low, int mid, int high, int[] tmp) {
    int i = 0;
    int j = low, k = mid + 1; // 左边序列和右边序列起始索引
    while (j <= mid && k <= high) {
      if (arr[j] < arr[k]) {
        tmp[i++] = arr[j++];
      } else {
        tmp[i++] = arr[k++];
      }
    }
    // 若左边序列还有剩余,则将其全部拷贝进tmp[]中
    while (j <= mid) {
      tmp[i++] = arr[j++];
    }

    while (k <= high) {
      tmp[i++] = arr[k++];
    }

    for (int t = 0; t < i; t++) {
      arr[low + t] = tmp[t];
    }
  }

  public static void mergeSort(int[] arr, int low, int high, int[] tmp) {
    if (low < high) {
      int mid = (low + high) / 2;
      mergeSort(arr, low, mid, tmp); // 对左边序列进行归并排序
      mergeSort(arr, mid + 1, high, tmp); // 对右边序列进行归并排序
      merge(arr, low, mid, high, tmp); // 合并两个有序序列
    }
  }

  public static void main(String[] args) {
    int[] arr = {11, 44, 23, 67, 88, 65, 34, 48, 9, 12};
    int[] tmp = new int[arr.length]; // 新建一个临时数组存放
    mergeSort(arr, 0, arr.length - 1, tmp);
    for (int i = 0; i < arr.length; i++) {
      System.out.print(arr[i] + " ");
    }
  }
}

选择排序

package com.yzq.practice;

import java.util.Arrays;

public class SelectionSort {
  public static void selectionSort(int[] array) {
    int length = array.length;
    int minindex, temp;
    for (int i = 0; i < length - 1; i++) {
      minindex = i;
      for (int j = i + 1; j < length; j++) if (array[j] < array[minindex]) minindex = j;
      temp = array[i];
      array[i] = array[minindex];
      array[minindex] = temp;
    }
  }

  public static void main(String[] args) {
    int[] b = {10, 43, 52, 6, 8, 2, 23, 67, 89, 90, 27};
    selectionSort(b);
    System.out.println(Arrays.toString(b));
  }
}

希尔排序

package com.yzq.practice;

import java.util.Arrays;

public class ShellSort {
  public static void shellSort(int[] array) {
    int length = array.length;
    int temp;
    int gap = 1;
    int i, j;
    while (gap < length / 5) { // 动态定义间隔序列
      gap = gap * 5 + 1;
    }
    for (; gap > 0; gap = (int) Math.floor(gap / 5)) {
      for (i = gap; i < length; i++) {
        temp = array[i];
        for (j = i - gap; j >= 0 && array[j] > temp; j -= gap) array[j + gap] = array[j];
        array[j + gap] = temp;
      }
    }
  }

  public static void main(String[] args) {
    int[] a = {10, 43, 52, 6, 8, 2, 23, 1, 67, 89, 90, 27};
    shellSort(a);
    System.out.println(Arrays.toString(a));
  }
}

堆排序

package com.yzq.practice;

import java.util.Arrays;

public class HeapSort {
  public static void heapSort(int[] arr) {
    if (arr == null || arr.length <= 1) return;
    // 建堆。
    buildHeap(arr);
    int len = arr.length;
    while (len > 1) {
      // 把堆顶和最后一个元素交换。
      swap(arr, 0, len - 1);
      // 交换完之后,逻辑上去掉最后一个元素。
      len--;
      // 重新调整堆的顺序。
      heapfy(arr, 0, len);
    }
  }

  private static void buildHeap(int[] arr) {
    for (int i = (arr.length - 1) / 2 - 1; i >= 0; i--) {
      heapfy(arr, i, arr.length);
    }
  }

  // 调整堆的顺序,保持大顶堆。
  private static void heapfy(int[] arr, int i, int len) {
    while (true) {
      int maxPostion = i;
      int leftChild = 2 * i + 1; // 左孩子索引。
      int rightChild = 2 * i + 2; // 右孩子索引。

      // 若左孩子大于最大值,则更新最大值。
      if (leftChild < len && arr[leftChild] > arr[maxPostion]) {
        maxPostion = leftChild;
      }

      // 若右孩子大于最大值,则更新最大值。
      if (rightChild < len && arr[rightChild] > arr[maxPostion]) {
        maxPostion = rightChild;
      }

      if (maxPostion == i) {
        break; // 若已经是大顶堆了,则退出循环。
      } else {
        swap(arr, i, maxPostion); // 若不是大顶堆,则交换位置。
        i = maxPostion;
      }
    }
  }

  private static void swap(int[] arr, int i, int j) {
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }

  public static void main(String[] args) {
    int[] arr = {6, 9, 1, 4, 5, 8, 7, 0, 2, 3};
    heapSort(arr);
    System.out.println(Arrays.toString(arr));
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值