常用排序算法总结

冒泡排序

public static int[] bubleSort(int[] arr) {
  if (arr == null || arr.length == 0) {
    return arr;
  }
  int n = arr.length;
  for (int i = 0; i < n; i++) {
    boolean changed = false;
    for (int j = 1; j < n - i; j++) {
      if (arr[j] < arr[j - 1]) {
        // 交换arr[j]和arr[j - 1]
        int tmp = arr[j];
        arr[j] = arr[j - 1];
        arr[j - 1] = tmp;
        changed = true;
      }
    }
    if (!changed) {
      break;
    }
  }
  return arr;
}

选择排序

public static int[] selectSort(int[] arr) {
  if (arr == null || arr.length == 0) {
    return arr;
  }
  int n = arr.length;
  for (int i = 0; i < n; i++) {
    int largestIndex = 0;
    for (int j = 1; j < n - i; j++) {
      if (arr[j] > arr[largestIndex]) {
        largestIndex = j;
      }
    }
    // 交换arr[largestIndex]和arr[n - i - 1]
    int tmp = arr[largestIndex];
    arr[largestIndex] = arr[n - i - 1];
    arr[n - i - 1] = tmp;
  }
  return arr;
}

直接插入排序

public static int[] insertSort(int[] arr) {
  if (arr == null || arr.length < 2) {
    return arr;
  }
  int n = arr.length;
  for (int i = 1; i < n; i++) {
    int tmp = arr[i];
    for (int j = i - 1; j >= 0; j--) {
      if (arr[j] > tmp) {
        arr[j + 1] = a[j];
      } else {
        break;
      }
    }
    arr[j + 1] = tmp;
  }
}

归并排序

public static int[] merge(int arr[], int low, int mid, int high) {
  int[] tmp = new int[high - low + 1];
  int i = low;
  int j = mid + 1;
  int k = 0;
  while (i <= mid && j <= high) {
    if (arr[i] < arr[j]) {
      tmp[k++] = arr[i];
      i++;
    } else {
      tmp[k++] = arr[j];
      j++;
    }
  }

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

  for (i = low, k = 0; i <= high; i++, k++) {
    arr[i] = tmp[k];
  }
  return arr;
}

public static void mergeSort(int arr[], int low, int high) {
  if (low < high) {
    int mid = (low + high) / 2;
    mergeSort(arr, low, mid);
    mergeSort(arr, mid + 1, high);
    merge(arr, low, mid, high);
  }
}

快速排序

public static int partition(int[] arr, int low, int high) {
  int pivot = arr[low];
  while (low < high) {
    while (low < high && arr[high] >= pivot) {
      high--;
    }
    if (low < high) {
      arr[low++] = arr[high];
    }
    while (low < high && arr[low] <= pivot) {
      low++;
    }
    if (low < high) {
      arr[high--] = arr[low];
    }
  }
  arr[low] = pivot;
  return low;
}

堆排序

public static void heapAdjust(int[] arr, int parent, int length) {
  int tmp = arr[parent];
  int child = parent * 2 + 1;
  while (child <= length) {
    if (child + 1 <= length && arr[child] < arr[child + 1]) {
      child++;
    }
    
    if (tmp >= arr[child]) {
      break;
    }

    arr[parent] = arr[child];
    parent = child;
    child = child * 2 + 1;
  } 
  arr[parent] = tmp;
}

public static void heapSort(int[] arr) {
  // 循环建堆
  for (int i = arr.length / 2; i >= 0; i--) {
    heapAdjust(arr, i, arr.length - 1);
  }

  for (int i = arr.length - 1; i > 0; i--) {
    int tmp = arr[i];
    arr[i] = arr[0];
    arr[0] = arr[i];
    heapAdjust(arr, 0, i);
  }
}

你可能感兴趣的:(常用排序算法总结)