public class SortUtils {
/**
* 直接插入排序
* @param a 待排序的数组
* @param n 待排序数组的个数
* @return 有序的数列
*/
public static void StraightInsertSort(int[] a,int n){
long s = System.currentTimeMillis();
for(int i=1;i<n;i++){
int t = a[i];
int j;
for(j=i-1;j>=0&&t<a[j];j--){
a[j+1]=a[j];
}
a[j+1]=t;
}
long e = System.currentTimeMillis();
// int time =(int) (e-s)/1000;
// System.out.println("排序耗时:"+time+"秒");
System.out.println("直接排序耗时:"+(e-s)+"毫秒");
}
/**
*
* @param a 待排序的数组
* @param n 待排序的数组长度
* @param inc shell排序的数组
* @param t 排序数组的长度
*/
public static void ShellSort(int[] a,int n,int inc[],int t){
long s = System.currentTimeMillis();
for(int i=1;i<t;i++){
ShellInsert(a,n,inc[i]);
}
long e = System.currentTimeMillis();
System.out.println("希尔排序耗时:"+(e-s)+"毫秒");
}
private static void ShellInsert(int[] a, int n, int inc) {
for(int i =inc;i<n;i++){
int j;
int temp = a[i];
for(j=i-inc;j>0&&temp<a[j];j-=inc){
a[j+inc]=a[j];
}
a[j+inc]=temp;
}
}
public static void BubbleSort(int [] a,int len){
long s = System.currentTimeMillis();
int j;
for(int i=len-1;i>0;i--){//i的起始值为len-1
for(j=0;j<i;j++){
if(a[j]>a[j+1]){
Swap(a[j],a[j+1]);
}
}
}
long e = System.currentTimeMillis();
System.out.println("起泡排序耗时:"+(e-s)+"毫秒");
}
private static void Swap(int i,int j){
int temp;
temp=i;
i=j;
j=temp;
}
public static void QuickSort(int [] a,int len){
long s = System.currentTimeMillis();
QuickSortHelp(a,0,len-1);
long e = System.currentTimeMillis();
System.out.println("快速排序耗时:"+(e-s)+"毫秒");
}
private static void QuickSortHelp(int []a,int low,int high){
if(low<high){
int i = Partition(a,low,high);
QuickSortHelp(a,low,i-1);
QuickSortHelp(a,i+1,high);
}
}
/**
*
* @param a
* @param low
* @param high
* @return
*/
private static int Partition(int[] a,int low,int high){
if(low<high){
while(low<high&&a[low]<=a[high]){
high--;
}
Swap(a[low],a[high]);
while(low<high&&a[low]<=a[high]){
low++;
}
Swap(a[low],a[high]);
}
return low;
}
//简单选择排序
/**
*
* @param a
* @param n
*/
public static void SimpleSelectSort(int[] a,int n){
long s = System.currentTimeMillis();
//选择最小的一个元素,循环len-1次,最后一个数一定是最大的
for(int i=0;i<n-1;i++){
int minIndex=i;
for(int j=i+1;j<n;j++){
if(a[j]<a[minIndex]){
minIndex=j;
}
}
Swap(a[i],a[minIndex]);
}
long e = System.currentTimeMillis();
System.out.println("简单选择排序耗时:"+(e-s)+"毫秒");
}
//归并排序核心参数,五个参数,三个临时变量,三次循环
private static void Merge(int[] a,int[] temp,int low,int mid,int high){
int i =low;
int j=mid;
int index=0;
while(i<mid&&j<=high){
if(a[i]<a[j]){
temp[index++]=a[i++];
}else{
temp[index++]=a[j++];
}
}
while(i<mid){
temp[index++]=a[i++];
}
while(j<=high){
temp[index++]=a[j++];
}
for(i=0;i<index;i++){
a[i]=temp[i];
}
}
//归并排序辅助函数,递归函数,递归条件是low<high
//递归条件,两次递归,一次归并
private static void MergeSortHelp(int[] a,int[] temp,int low,int high){
int mid;
if(low<high){
mid =(low+high)/2;
MergeSortHelp(a,temp,low,mid);
MergeSortHelp(a,temp,mid+1,high);
//不能是下面这样
// MergeSortHelp(a,temp,low,mid-1);
// MergeSortHelp(a,temp,mid,high);
Merge(a,temp,low,mid,high);
}
}
//归并排序
public static void MergeSort(int[] a,int len){
long s = System.currentTimeMillis();
int[] tem=new int[2300];
MergeSortHelp(a,tem,0,len-1);
long e = System.currentTimeMillis();
System.out.println("归并排序耗时:"+(e-s)+"毫秒");
}
参考 《算法》第4版
都是内排序

1058

被折叠的 条评论
为什么被折叠?



