package test;
import java.util.Arrays;
public class SortUtils {
/**
* 冒泡排序
*
* @param arr
*/
public static void bubbleSort(int[] arr) {
int length = arr.length;
int temp;
for (int i = 0; i < length; i++) {
for (int j = 0; j < length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
/**
* 选择排序
*
* @param arr
*/
public static void selectSort(int[] arr) {
int length = arr.length;
int temp;
int minIndex = 0;
for (int i = 0; i < length - 1; i++) {
minIndex = i;
for (int j = i + 1; j < length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (i != minIndex) {
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
/**
* 插入排序法
*
* @param arr
*/
public static void insertSort(int[] arr) {
int length = arr.length;
int temp, j;
for (int i = 1; i < length; i++) {
if (arr[i] < arr[i - 1]) {
temp = arr[i];
j = i - 1;
while (j >= 0 && temp < arr[j]) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
}
/**
* 二分插入排序法
*
* @param arr
*/
public static void binaryInsertSort(int[] arr) {
int length = arr.length;
int lower, upper, middle, temp;
for (int i = 1; i < length; i++) {
if (arr[i] < arr[i - 1]) {
temp = arr[i];
lower = 0;
upper = i - 1;
while (lower <= upper) {
middle = (lower + upper) / 2;
if (temp < arr[middle]) {
upper = middle - 1;
} else {
lower = middle + 1;
}
}
for (int j = i; j > lower; j--) {
arr[j] = arr[j - 1];
}
arr[lower] = temp;
}
}
}
/**
* 希尔排序
*
* @param arr
*/
public static void shellSort(int[] arr) {
int length = arr.length;
int temp, j;
// 计算出最大的h值
int h = 1;
while (h <= length / 3) {
h = 3 * h + 1;
}
while (h > 0) {
for (int i = h; i < length; i++) {
if (arr[i] < arr[i - h]) {
temp = arr[i];
j = i - h;
while (j >= 0 && arr[j] > temp) {
arr[j + h] = arr[j];
j -= h;
}
arr[j + h] = temp;
}
}
h = (h - 1) / 3;
}
}
/**
* 归并排序
*
* @param arr
*/
public static void mergeSort(int[] arr) {
mergeSort(arr, 0, arr.length - 1);
}
/**
* 归并排序
*
* @param arr
* @param left
* @param right
*/
private static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
// 找出中间索引
int mid = (left + right) / 2;
// 对左边数组进行递归
mergeSort(arr, left, mid);
// 对右边数组进行递归
mergeSort(arr, mid + 1, right);
// 合并
merge(arr, left, mid, right);
}
}
private static void merge(int[] arr, int left, int mid, int right) {
// 临时数组
int[] tempArr = new int[arr.length];
// 记录临时数组索引
int tmpIndex = left;
// 左数组第一个元素的索引
int tmpLeft = left;
// 右数组第一个元素的索引
int tmpMid = mid + 1;
while (tmpLeft <= mid && tmpMid <= right) {
if (arr[tmpLeft] < arr[tmpMid]) {
tempArr[tmpIndex++] = arr[tmpLeft++];
} else {
tempArr[tmpIndex++] = arr[tmpMid++];
}
}
while (tmpLeft <= mid) {
tempArr[tmpIndex++] = arr[tmpLeft++];
}
while (tmpMid <= right) {
tempArr[tmpIndex++] = arr[tmpMid++];
}
// 将临时数组的数据复制到主数组
while (left <= right) {
arr[left] = tempArr[left++];
}
}
/**
* 快速排序
*
* @param arr
* @param left
* @param right
*/
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int partition = partition(arr, left, right);
quickSort(arr, left, partition - 1);
quickSort(arr, partition + 1, right);
}
}
private static int partition(int[] arr, int left, int right) {
int temp = arr[left];
while (left < right) {
while (left < right && arr[right] >= temp) {
right--;
}
if (left < right) {
arr[left] = arr[right];
}
while (left < right && arr[left] <= temp) {
left++;
}
if (left < right) {
arr[right] = arr[left];
}
}
arr[left] = temp;
return left;
}
public static void main(String[] args) {
int[] arr = { 2, 7, 6, 3, 4, 5, 9, 8, 1, 0 };
System.out.println("Sort Before:" + Arrays.toString(arr));
// bubbleSort(arr);
// selectSort(arr);
// insertSort(arr);
// binaryInsertSort(arr);
// mergeSort(arr);
// shellSort(arr);
quickSort(arr, 0, arr.length - 1);
System.out.println("Sort After:" + Arrays.toString(arr));
}
}