直接插入排序
public static void insertSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
int insertVal = nums[i];
int insertIndex = i - 1;
while (insertIndex >= 0 && nums[insertIndex ] > insertVal) {
nums[insertIndex + 1] = nums[insertIndex ];
insertIndex --;
}
nums[insertIndex + 1] = insertVal;
}
}
希尔排序
public static void shellSortI(int[] nums) {
for (int gap = nums.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < nums.length; i++) {
int insertIndex = i;
int insertVal = nums[insertIndex];
while (insertIndex - gap >= 0 && nums[insertIndex - gap] > insertVal) {
nums[insertIndex] = nums[insertIndex - gap];
insertIndex -= gap;
}
nums[insertIndex] = insertVal;
}
}
}
public static void shellSortII(int[] nums) {
for (int gap = nums.length / 2; gap > 0; gap /= 2) {
for (int i = 0; i < gap; i++) {
for (int j = i + gap; j < nums.length; j += gap) {
int insertVal = nums[j];
int insertIndex = j - gap;
while (insertIndex >= 0 && nums[insertIndex] > insertVal) {
nums[insertIndex + gap] = nums[insertIndex];
insertIndex -= gap;
}
nums[insertIndex + gap] = insertVal;
}
}
}
}
冒泡排序
public static void bubbleSort(int[] nums) {
for (int i = nums.length - 1; i > 0; i--) {
boolean swap = false;
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
swap = true;
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
if (!swap) return;
}
}
快速排序
public static void quickSortI(int[] nums) {
quickSortI(nums, 0, nums.length - 1);
}
private static void quickSortI(int[] nums, int left, int right) {
if (left > right) return;
int i = left, j = right, base = nums[left];
while (i < j) {
while (i < j && nums[j] >= base) j--;
nums[i] = nums[j];
while (i < j && nums[i] <= base) i++;
nums[j] = nums[i];
}
nums[i] = base;
quickSortI(nums, left, i - 1);
quickSortI(nums, i + 1, right);
}
public static void quickSortII(int[] nums) {
quickSortII(nums, 0, nums.length - 1);
}
private static void quickSortII(int[] nums, int left, int right) {
if (left > right) return;
int i = left, j = right, base = nums[left];
while (i < j) {
while (i < j && nums[j] >= base) j--;
while (i < j && nums[i] <= base) i++;
if (i < j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
nums[left] = nums[i];
nums[i] = base;
quickSortII(nums, left, j - 1);
quickSortII(nums, j + 1, right);
}
public static void quickSortWithMidPivot(int[] nums) {
quickSortWithMidPivot(nums, 0, nums.length - 1);
}
private static void quickSortWithMidPivot(int[] nums, int left, int right) {
if (left > right) return;
int i = left, j = right,
pivotIndex = (left + right) / 2,
pivot = nums[pivotIndex];
while (i < j) {
while (i < j && nums[j] >= pivot) j--;
nums[pivotIndex] = nums[j];
pivotIndex = j;
while (i < j && nums[i] <= pivot) i++;
nums[pivotIndex] = nums[i];
pivotIndex = i;
}
nums[i] = pivot;
quickSortWithMidPivot(nums, left, i - 1);
quickSortWithMidPivot(nums, i + 1, right);
}
简单选择排序
public static void selectSort(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int index = i;
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] < nums[index]) {
index = j;
}
}
if (index != i) {
int temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
}
}
}
堆排序
public static void heapSort(int[] nums) {
for (int i = nums.length - 1; i > 0; i--) {
BigHeapI(nums, i + 1);
int temp = nums[0];
nums[0] = nums[i];
nums[i] = temp;
}
}
private static void BigHeapI(int[] nums, int length) {
for (int i = length / 2 - 1; i >= 0; i--) {
int t = i, temp = nums[t];
for (int j = 2 * t + 1; j < length; j = (2 * j) + 1) {
if (j + 1 < length && nums[j] < nums[j + 1]) j++;
if (temp >= nums[j]) break;
else {
nums[t] = nums[j];
t = j;
}
}
nums[t] = temp;
}
}
private static void BigHeapII(int[] nums, int length) {
for (int i = length / 2 - 1; i >= 0; i--) {
int t = i, temp = nums[t];
for (int j = 2 * t + 1; j < length; j = (2 * j) + 1) {
if (j + 1 < length && nums[j] < nums[j + 1]) j++;
if (temp >= nums[j]) break;
else {
nums[t] = nums[j]; nums[j] = temp;
t = j; temp = nums[t];
}
}
}
}
归并排序
public static void mergeSort(int[] nums) {
int[] copy = new int[nums.length];
mergeSort(nums, 0, nums.length - 1, copy);
}
private static void mergeSort(int[] nums, int left, int right, int[] copy) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(nums, left, mid, copy);
mergeSort(nums, mid + 1, right, copy);
merge(nums, left, mid, right, copy);
}
}
private static void merge(int[] nums, int left, int mid, int right, int[] copy) {
for (int i = left; i <= right; i++) {
copy[i] = nums[i];
}
int i, j, k;
for (i = left, j = mid + 1, k=i; i <= mid && j <= right; k++) {
if (copy[i] <= copy[j]) nums[k] = copy[i++];
else nums[k] = copy[j++];
}
while (i <= mid) nums[k++] = copy[i++];
while (j <= right) nums[k++] = copy[j++];
}
基数排序
public static void radixSort(int[] nums) {
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
int maxLength = (max + "").length();
int[][] bucket = new int[10][nums.length];
int[] bucketElementsCounts = new int[10];
for (int i = 0, n = 1; i < maxLength; i++, n*=10) {
for (int j = 0; j < nums.length; j++) {
int digitOfElement = nums[j] / n % 10;
bucket[digitOfElement][bucketElementsCounts[digitOfElement]] = nums[j];
bucketElementsCounts[digitOfElement]++;
}
int index = 0;
for (int j = 0; j < bucketElementsCounts.length; j++) {
if (bucketElementsCounts[j]!=0) {
for (int k = 0; k < bucketElementsCounts[j]; k++) {
nums[index++] = bucket[j][k];
}
bucketElementsCounts[j] = 0;
}
}
}
}