排序算法的Java实现
import com.sun.scenario.effect.Merge;
public class Sort {
//插入排序
public void InsertSort(int A[]){
int i,j,temp;
for (i=1;i<A.length;i++){
if (A[i] < A[i-1]){
temp = A[i];
for (j=i-1;temp < A[j];--j){
A[j+1] = A[j];
}
A[j+1] = temp;
}
}
}
//折半插入排序
public void HalfInsertSort(int A[]){
int i,j,low,hight,mid,temp;
for (i=1;i<A.length;i++){
temp = A[i];
low = 0;
hight = i - 1;
while (low <= hight){
mid = (low + hight) / 2;
if (A[mid] > temp){
hight = mid - 1;
}else {
low = mid + 1;
}
}
for (j=i-1;j>=hight+1;--j){
A[j+1] = A[j];
}
A[hight+1] = temp;
}
}
//希尔排序
public void ShellSort(int A[]){
int i,j,temp;
for(int dk = A.length/2;dk >= 1;dk = dk/2){ //变化步长 从d1 = n/2 di+1 = di/2 到 1
for (i = dk+1;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;
}
}
}
}
//冒泡排序
public void BubbleSort(int A[]){
int i,j,temp;
boolean flag;
for (i=0;i<A.length-1;i++){
flag = false;
for (j=A.length-1;j>i;j--){
if (A[j-1] > A[j]){
temp = A[j-1];
A[j-1] = A[j];
A[j] = temp;
flag = true;
}
}
if (!flag){
return;
}
}
}
//快速排序 划分算法
public int Partition(int A[],int low,int hight){
int pivot = A[low];
while (low < hight){
while (low<hight&&A[hight]>=pivot) --hight;
A[low] = A[hight];
while (low<hight&&A[low]<=pivot) ++low;
A[hight] = A[low];
}
A[low] = pivot;
return low;
}
//快速排序
public void QuickSort(int A[],int low,int hight){
if (low<hight){
int pivotpos = Partition(A,low,hight);
QuickSort(A,low,pivotpos-1);
QuickSort(A,pivotpos+1,hight);
}
}
//简单选择排序
public void SelectSort(int A[]){
int i,j,min,temp;
for (i=0;i<A.length-1;i++){
min = i;
for (j=i+1;j<A.length;j++){
if (A[j]<A[min]){
min = j;
}
}
if (min!=i){
temp = A[i];
A[i] = A[min];
A[min] = temp;
}
}
}
//堆排序
public void BuildMaxHeap(int A[]){
for (int i = (A.length - 1)/2;i>0;i--)
AdjustDown(A,i,A.length-1);
}
public void AdjustDown(int A[],int k,int len){
A[0] = A[k];
for (int i=2*k;i<=len;i*=2){
//System.out.println(i);
if (i<len && A[i]<A[i+1]) i++;//找最大的子节点
if (A[0]>A[i]) break;
else {
A[k] = A[i];
k = i;
}
}
A[k] = A[0];
}
public void HeapSort(int A[]){
BuildMaxHeap(A);
int i,temp;
for (i=A.length-1;i>1;i--){
temp = A[i];
A[i] = A[1];
A[1] = temp;
AdjustDown(A,1,i-1);
}
}
//归并排序
public void MergeSort(int A[],int B[],int low,int hight){
if (low<hight){
int mid = (low + hight) / 2;
MergeSort(A,B,low,mid);
MergeSort(A,B,mid+1,hight);
Merge(A,B,low,mid,hight);
}
}
public void Merge(int A[],int B[],int low,int mid,int hight){
int i,j,k;
for (k=low;k<=hight;k++) B[k] = A[k]; //复制数组
for (i=low,j=mid+1,k=i;i<=mid&&j<=hight;k++){
if (B[i] < B[j]) A[k] = B[i++];
else A[k] = B[j++];
}
while (i<=mid) A[k++] = B[i++];
while (j<=hight) A[k++] = B[j++];
}
//基数排序略
public static void main(String[] args){
int A[] = {1,4,3,5,7,2,8,6,9};
int B[] = {0,1,4,3,5,7,2,8,6,9};//作为堆排序的输入,也作为归并排序的辅助数组
Sort s = new Sort();
//s.InsertSort(A);
//s.HalfInsertSort(A);
//s.ShellSort(A);
//s.BubbleSort(A);
//s.QuickSort(A,0,A.length-1);
//s.SelectSort(A);
//s.HeapSort(B);
//s.MergeSort(A,B,0,A.length-1);
for (int x : A){
System.out.println(x);
}
}
}