排序算法
一、插入排序
1)直接插入排序
2)折半插入排序(二分插入排序)
3)希尔排序
二、交换排序
1)冒泡排序
2)快速排序
三、 选择排序
1)简单选择排序
2)堆排序
四、归并排序
五、基数排序
package com;
import java.util.Arrays;
public class sort1 {
public static void InsertSort(int a[]) {
for (int i = 1; i < a.length; i++) {
int temp = a[i];
int j = i - 1;
while (j >= 0 && a[j] > temp) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
show(a);
}
public static void InsertSort1(int a[]) {
int i, j, low, high, mid;
for(i=1;i<a.length;i++) {
int temp=a[i];
low=0;
high=i-1;
while(low<=high) {
mid=(low+high)/2;
if(a[mid]>temp) {
high=mid-1;
}else {
low=mid+1;
}
}
for(j=i-1;j>=high+1;--j) {
a[j+1]=a[j];
}
a[high+1]=temp;
}
show(a);
}
public static void ShellSort(int a[]) {
int dk;
int i,j,temp;
for(dk=a.length/2; dk>=1; dk=dk/2) {
for(i=dk; i<a.length; i++) {
if(a[i]<a[i-dk]) {
temp=a[i];
for(j=i-dk;j>=0 && temp<=a[j]; j-=dk) {
a[j+dk]=a[j];
}
a[j+dk]=temp;
}
}
}
show(a);
}
public static void BubbleSort(int a[]) {
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < a.length - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
show(a);
}
public static void QuickSort(int[] a, int begin, int end) {
if (begin < end) {
sort st = new sort();
int i = begin + 1, j = end, flag = begin;
while (i < j) {
while (i < j && a[i] >= a[flag])
i++;
while (i < j && a[j] <= a[flag])
j--;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
if (a[i] <= a[flag])
i = i - 1;
int temp = a[i];
a[i] = a[flag];
a[flag] = temp;
st.QuickSort(a, begin, i - 1);
st.QuickSort(a, i + 1, end);
}
}
public static void SelectSort(int a[]) {
for (int i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min])
min = j;
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
show(a);
}
public static void buildHeap(int[] a, int i, int len) {
int left = 2 * i + 1, right = 2 * i + 2, min = i;
if (left < len && a[left] < a[min])
min = left;
if (right < len && a[right] < a[min])
min = right;
if (min != i) {
int temp = a[i];
a[i] = a[min];
a[min] = temp;
buildHeap(a, min, len);
}
}
public static void HeapSort(int a[]) {
int len = a.length;
for (int i = len / 2; i >= 0; i--) {
buildHeap(a, i, len);
}
for (int i = len - 1; i >= 0; i--) {
int temp = a[i];
a[i] = a[0];
a[0] = temp;
len = len - 1;
buildHeap(a, 0, len);
}
show(a);
}
public static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int i = 0;
while (left.length > 0 && right.length > 0) {
if (left[0] <= right[0]) {
result[i++] = left[0];
left = Arrays.copyOfRange(left, 1, left.length);
} else {
result[i++] = right[0];
right = Arrays.copyOfRange(right, 1, right.length);
}
}
while (left.length > 0) {
result[i++] = left[0];
left = Arrays.copyOfRange(left, 1, left.length);
}
while (right.length > 0) {
result[i++] = right[0];
right = Arrays.copyOfRange(right, 1, right.length);
}
return result;
}
public static int[] MergeSort(int[] a) {
sort st = new sort();
if (a.length < 2)
return a;
int mid = (int) Math.floor(a.length / 2);
int[] left = Arrays.copyOfRange(a, 0, mid);
int[] right = Arrays.copyOfRange(a, mid, a.length);
return merge(st.MergeSort(left), st.MergeSort(right));
}
static void show(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] a = new int[] {8, 4, 3, 6, 9, 1, 2, 5, 10, 7 };
System.out.println("待排序数组:");
show(a);
System.out.println("\n直接插入排序:");
InsertSort(a);
System.out.println("\n折半插入排序:");
InsertSort1(a);
System.out.println("\n希尔排序排序:");
ShellSort(a);
System.out.println("\n冒泡排序:");
BubbleSort(a);
System.out.println("\n快速排序:");
QuickSort(a, 0, a.length - 1);
show(a);
System.out.println("\n选择排序:");
SelectSort(a);
System.out.println("\n堆排序:");
HeapSort(a);
System.out.println("\n归并排序:");
show(MergeSort(a));
}
}