package com.wang.sort;
import java.util.Arrays;
public class Sort {
/**
* 1.直接插入排序
* 思想:当前数与前面已经排好顺序的数进行比较,插入到合适的位置
* @param arra
*/
public void simpleSort(int[] arra) {
for (int i = 1; i < arra.length; i++) {
int temp = arra[i];
int j = i -1;
for (; j >= 0 && arra[j] > temp; j--) {
arra[j + 1] = arra[j];
}
arra[j+1] = temp;
}
}
/**
* 2.冒泡排序
* 思想:相邻的两个数比较,找到最大的数往下沉
* @param args
*/
public void bubbleSort(int[] arra) {
int temp;
for (int i = 0; i < arra.length - 1; i++) {
for (int j = 0; j < arra.length - 1 - i; j++) {
if (arra[j] > arra[j+1]) {
temp = arra[j];
arra[j] = arra[j+1];
arra[j+1] = temp;
}
}
}
}
/**
* 找中间值
* @param arra
* @param low
* @param hight
*/
public static int getMiddle(int[] arra, int low, int hight) {
int temp = arra[low];
while (low < hight) {
while (low < hight && arra[hight] > temp) {
hight--;
}
arra[low] = arra[hight];
while(low < hight && arra[low] <= temp) {
low++;
}
arra[hight] = arra[low];
}
arra[low] = temp;
return low;
}
/**
* 3.快速排序
* 思想:找一个中间值,将比中间值大的放到中间值的右边,小的放到左边,依此进行递归
* @param arra
* @param low
* @param hight
*/
public static void qickSort(int[] arra, int low, int hight) {
if (low > hight) {
return;
}
int middle = getMiddle(arra, low, hight);
qickSort(arra, low, middle -1);
qickSort(arra, middle + 1, hight);
}
/**
* 归并排序
* @param arra
* @param left
* @param center
* @param right
*/
public static void merge(int[] arra, int left, int center, int right) {
int[] tempArr = new int[right - left + 1];
int temp = left;
int mid = center + 1;
int third = 0;
while(temp <= center && mid <= right) {
if (arra[temp] < arra[mid]) {
tempArr[third++] = arra[temp++];
} else {
tempArr[third++] = arra[mid++];
}
}
while(temp <= center) {
tempArr[third++] = arra[temp++];
}
while (mid <= right) {
tempArr[third++] = arra[mid++];
}
for (int i = 0; i < tempArr.length; i++) {
arra[i + left] = tempArr[i];
}
}
/**
* 4.归并排序
* @param arra
* @param left
* @param right
*/
public void mergingSort(int arra[], int left, int right) {
if (left < right) {
int center = (left + right) / 2;
mergingSort(arra, left, center);
mergingSort(arra, center + 1, right);
merge(arra, left, center, right);
}
}
/**
* 5.希尔排序
* @param args
*/
public static void shellSort(int[] array) {
int i;
int j;
int temp;
int gap = 1;
int len = array.length;
while (gap < len / 3) {
gap = gap * 3 + 1;
}
for (; gap > 0; gap /= 3) {
for (i = gap; i < len; i++) {
temp = array[i];
for (j = i - gap; j >= 0 && array[j] > temp; j -= gap) {
array[j + gap] = array[j];
}
array[j + gap] = temp;
}
}
System.out.println(Arrays.toString(array) + " shellSort");
}
/**
* 5.希尔排序
* @param arra
*/
public void shelllSort(int[] arra) {
int j = 0;
int temp = 0;
for (int step = arra.length / 2; step > 0; step /= 2) {
for (int i = step; i < arra.length; i++) {
temp = arra[i];
for (j = i - step; j >= 0 && arra[j] > temp; j -= step) {
arra[j + step] = arra[j];
}
arra[j + step] = temp;
}
}
}
/**
* 6.简单选择排序
* 思想:找出最小的放到第一位,第二小的放到第二位,依次类推
* @param args
*/
public void selectSort(int[] arra) {
int position = 0;
for (int i = 0; i < arra.length; i++) {
int j = i + 1;
position = i;
int temp = arra[i];
for (; j < arra.length; j++) {
if (arra[j] < temp) {
temp = arra[j];
position = j;
}
}
arra[position] = arra[i];
arra[i] = temp;
}
}
/**
* 7.基数排序
* @param args
*/
/**
* 8.堆排序
* @param args
*/
public static void main(String[] args) {
Sort sort = new Sort();
int[] arra = {1,2,3,9,6,8,5,10};
// sort.simpleSort(arra); //简单排序
// sort.bubbleSort(arra); //冒泡排序
// Sort.qickSort(arra, 0, arra.length - 1); //快速排序
// sort.mergingSort(arra, 0, arra.length - 1);
sort.shelllSort(arra);
for (int i = 0; i < arra.length; i++) {
System.out.println(arra[i]);
}
}
}