十大排序算法(java源码):
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e8fd352d6ece54dd3abcc259f1e41e7d.png#pic_center)
1.选择排序:
package testTenSort;
import java.util.Arrays;
public class SelectSort {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
selectSort(arr);
}
static void selectSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minPos]) {
minPos = j;
swap(arr, i, minPos);
}
}
}
print(arr);
System.out.println();
System.out.println(Arrays.toString(arr));
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
2.冒泡排序:
package testTenSort;
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
bubbleSort(arr);
}
static void bubbleSort(int[] arr) {
for (int i = arr.length - 1; i >= 0; i--) {
for (int j = 1; j <= i; j++) {
if (arr[j] < arr[j - 1])
swap(arr, j, j - 1);
}
}
System.out.println(Arrays.toString(arr));
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
3.插入排序:
package testTenSort;
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
insertSort(arr);
}
static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1])
swap(arr, j, j - 1);
}
}
System.out.println(Arrays.toString(arr));
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
4.希尔排序:
package testTenSort;
import java.util.Arrays;
public class ShellSort {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
shellSort(arr);
KnuthShellSort(arr);
}
static void shellSort(int[] arr) {
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < arr.length; i++) {
for (int j = i; j >= gap; j -= gap) {
if (arr[j] < arr[j - gap])
swap(arr, j, j - gap);
}
}
}
System.out.println(Arrays.toString(arr));
}
static void KnuthShellSort(int[] arr) {
int h = 0;
while (h <= arr.length) {
h = h * 3 + 1;
}
for (int gap = h; gap > 0; gap = (gap - 1) / 3) {
for (int i = gap; i < arr.length; i++) {
for (int j = i; j > gap - 1; j -= gap) {
if (arr[j] < arr[j - gap])
swap(arr, j, j - gap);
}
}
}
System.out.println(Arrays.toString(arr));
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
5.堆排序:
package testTenSort;
import java.util.Arrays;
public class HeapSort {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
heapSort(arr);
}
static void heapSort(int[] arr) {
for (int i = arr.length / 2 - 1; i >= 0; i--)
heapify(arr, i, arr.length - 1);
for (int i = arr.length - 1; i >= 0; i--) {
swap(arr, 0, i);
heapify(arr, 0, i - 1);
}
System.out.println(Arrays.toString(arr));
}
static void heapify(int[] arr, int i, int last_index) {
int max = i;
if (2 * i + 1 <= last_index && arr[2 * i + 1] > arr[max])
max = 2 * i + 1;
if (2 * i + 2 <= last_index && arr[2 * i + 2] > arr[max])
max = 2 * i + 2;
if (max != i) {
swap(arr, i, max);
heapify(arr, max, last_index);
}
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
6.快速排序:
package testTenSort;
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] arr = { 7, 3, 2, 10, 8, 1, 9, 5, 4, 6 };
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
static void quickSort(int[] arr, int leftBound, int rightBound) {
if (leftBound >= rightBound)
return;
int mid = partition(arr, leftBound, rightBound);
quickSort(arr, leftBound, mid - 1);
quickSort(arr, mid + 1, rightBound);
}
static int partition(int[] arr, int leftBound, int rightBound) {
int pivot = arr[rightBound];
int left = leftBound;
int right = rightBound - 1;
while (left <= right) {
while (left <= right && arr[left] <= pivot)
left++;
while (left <= right && arr[right] > pivot)
right--;
if (left <= right)
swap(arr, left, right);
}
swap(arr, left, rightBound);
return left;
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
7.归并排序:
package testTenSort;
import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) {
int[] arr = { 1, 2, 4, 7, 8, 3, 5, 6, 9 };
mergeSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
static void mergeSort(int[] arr, int left, int right) {
if (left == right)
return;
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid + 1, right);
}
static void merge(int[] arr, int leftPtr, int rightPtr, int righeBound) {
int mid = rightPtr - 1;
int[] temp = new int[righeBound - leftPtr + 1];
int i = leftPtr;
int j = rightPtr;
int k = 0;
while (i <= mid && j <= righeBound) {
temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
}
while (i <= mid)
temp[k++] = arr[i++];
while (j <= righeBound)
temp[k++] = arr[j++];
for (int m = 0; m < temp.length; m++)
arr[leftPtr + m] = temp[m];
}
}
8.桶排序:
package testTenSort;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
public class BucketSort {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
bucketSort(arr);
}
static void bucketSort(int[] arr) {
int max = arr[0];
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
max = arr[i] > max ? arr[i] : arr[0];
min = arr[i] < min ? arr[i] : arr[0];
}
int count = (max - min) / arr.length + 1;
ArrayList<ArrayList<Integer>> buckets = new ArrayList<>();
for (int i = 0; i < count; i++)
buckets.add(new ArrayList<Integer>());
for (int i = 0; i < arr.length; i++)
buckets.get((arr[i] - min) / arr.length).add(arr[i]);
for (int i = 0; i < buckets.size(); i++)
Collections.sort(buckets.get(i));
int k = 0;
for (int i = 0; i < buckets.size(); i++) {
ArrayList<Integer> list = buckets.get(i);
for (int j = 0; j < list.size(); j++)
arr[k++] = list.get(j);
}
System.out.println(Arrays.toString(arr));
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
9.计数排序:
package testTenSort;
import java.util.Arrays;
public class CountSort {
public static void main(String[] args) {
int[] arr = { 2, 4, 2, 3, 7, 1, 1, 0, 0, 5, 6, 9, 8, 5, 7, 4, 0, 9 };
countSort(arr);
}
static void countSort(int[] arr) {
int[] result = new int[arr.length];
int max = arr[0];
for (int i = 1; i < arr.length; i++)
max = arr[i] > max ? arr[i] : arr[0];
int[] count = new int[max + 1];
for (int i = 0; i < arr.length; i++) {
count[arr[i]]++;
}
for (int i = 1; i < count.length; i++) {
count[i] = count[i] + count[i - 1];
}
for (int i = arr.length - 1; i >= 0; i--) {
result[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
System.out.println(Arrays.toString(result));
}
}
10.基数排序:
package testTenSort;
import java.util.Arrays;
public class RadixSort {
public static void main(String[] args) {
int[] arr = { 2, 4, 2, 3, 7, 1, 1, 0, 0, 5, 6, 9, 8, 5, 7, 4, 0, 9 };
radixSort(arr);
}
static void radixSort(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++)
max = arr[i] > max ? arr[i] : arr[0];
for (int exp = 1; max / exp > 0; exp *= 10) {
int[] buckets = new int[10];
int[] temp = new int[arr.length];
for (int i = 0; i < arr.length; i++)
buckets[arr[i] / exp % 10]++;
for (int i = 1; i < buckets.length; i++)
buckets[i] += buckets[i - 1];
for (int i = arr.length - 1; i >= 0; i--) {
temp[buckets[arr[i]] - 1] = arr[i];
buckets[arr[i]]--;
}
for (int i = 0; i < temp.length; i++)
arr[i] = temp[i];
}
System.out.println(Arrays.toString(arr));
}
}