package demo2;
public class Text2 {
//生成随机数个数NUM、范围[A,B]
public static int NUM = 800;
public static int A = 1;
public static int B = 1000;
public static void main(String args[]) {
//exercise58();
int total=exercise7();
System.out.println();
System.out.println("10min排序数据量:"+total);
}
//5、8题
public static void exercise58() {
//生成随机数组
int[] a = new int[NUM];
int[] b = new int[NUM];
for(int i=0;i<NUM;i++)
{
a[i] = A + (int)(Math.random()*(B-A+1));
b[i] = a[i];
}
System.out.println("原数组如下:");
for(int e:a)
{
System.out.print(e+" ");
}
System.out.println();
//程序开始时间
//long startTime = System.currentTimeMillis();
long startTime=System.nanoTime();
//insertionSort(a,a.length);
//heapSort(a,a.length);
//mergeSort(a,0,a.length-1,a.length);
quickSort(a,0,a.length-1);
//程序结束时间
//long endTime = System.currentTimeMillis();
long endTime=System.nanoTime();
System.out.println("排序后数组如下:");
for(int e:a)
System.out.print(e+" ");
System.out.println();
//System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
System.out.println("程序运行时间: "+(endTime-startTime)+"ns");
//二分搜索版插入排序
/*long b_startTime=System.nanoTime();
binaryInsertion(b,b.length);
long b_endTime=System.nanoTime();
System.out.println("二分搜索版插入排序后数组如下:");
for(int e:a)
System.out.print(e+" ");
System.out.println();
//System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
System.out.println("程序运行时间: "+(b_endTime-b_startTime)+"ns");*/
}
public static int exercise7() {
int orinal=50000;//数组大小
int increase=1000;//数组一次的增量
int[] a=new int[orinal];
int total=0;//a的实际length
long startTime = System.currentTimeMillis();
while(true) {
if(total>orinal-1)
{
int[] b=new int[orinal+increase];
System.arraycopy(a, 0, b, 0, orinal);
a=b;
orinal+=increase;
}
else
{
if(System.currentTimeMillis()-startTime>=600000)
return total;
else
{
a[total++]=A + (int)(Math.random()*(B-A+1));
if(total%10==0)
System.out.print(total+" ");
if(total%200==0)
System.out.println();
//insertionSort(a,total);
//heapSort(a,total);
//mergeSort(a,0,total-1,total);
quickSort(a,0,total-1);
}
}
}
}
private static void swap(int[] a, int i, int j) {
// TODO 自动生成的方法存根
int t=a[i];
a[i]=a[j];
a[j]=t;
}
//插入排序
public static void insertionSort(int[] a,int total) {
for(int i=1;i<total;i++)
{
int current=a[i];
int j=i-1;
for(;j>=0;j--)
{
if(a[j]>current)
a[j+1]=a[j];
else
break;
}
a[j+1]=current;
}
}
//二分搜索版插入排序
public static void binaryInsertion(int[] a,int total) {
for(int i=1;i<total;i++)
{
int current=a[i];
int p=binarySearch(a,i-1,current);
for(int j=i;j>p+1;j--)
{
a[j]=a[j-1];
}
a[p+1]=current;
}
}
//二分搜索
private static int binarySearch(int[] a,int high,int x) {
int low=0,mid;
while(low<=high)
{
mid=(low+high)/2;
if(x<a[mid])
high=mid-1;
else
low=mid+1;
}
return high;
}
//堆排序
public static void heapSort(int[] a,int total) {
heapify(a,total);
for(int i=total-1;i>0;i--) {
swap(a,0,i);
shiftDown(a,i-1,0);
}
}
private static void shiftDown(int[] a,int n,int i) {
// TODO 自动生成的方法存根
boolean done=false;
while(!done&&2*i+1<=n)
{
i=i*2+1;
if(i+1<=n&&a[i]<a[i+1])
i=i+1;
if(a[(i-1)/2]<a[i])
swap(a,i,(i-1)/2);
else
done=true;
}
}
private static void heapify(int[] a,int total) {
// TODO 自动生成的方法存根
for(int i=(total-1)/2;i>=0;i--)//i的初始值和条件错
shiftDown(a,total-1,i);
}
//归并排序
private static void mergeSort(int[] a,int low,int high,int total) {
if(low<high) {
int mid=(high+low)/2;
mergeSort(a,low,mid,total);
mergeSort(a,mid+1,high,total);
twoWayMerge(a,low,mid,high,total);
}
}
private static void twoWayMerge(int[] a, int low, int mid, int high,int total) {
// TODO 自动生成的方法存根
int[] b=new int[total];
int lp=low;
int mp=mid+1;
int bp=low;//不能是0
while(lp<=mid&&mp<=high)
{
if(a[lp]<a[mp])
b[bp++]=a[lp++];
else
b[bp++]=a[mp++];
}
if(lp==mid+1)
{
while(mp<=high)
b[bp++]=a[mp++];
}
else
{
while(lp<=mid)
b[bp++]=a[lp++];
}
for(int i=low;i<=high;i++)//不能从0到a.length
a[i]=b[i];
}
//快速排序
private static void quickSort(int[] a,int low,int high) {
if(low<high)
{
int p=partition(a,low,high);
quickSort(a,low,p-1);
quickSort(a,p+1,high);
}
}
private static int partition(int[] a, int low, int high) {
// TODO 自动生成的方法存根
int p=high;
int j=low-1;
for(int i=low;i<high;i++)
{
if(a[i]<=a[p])
{
j++;
if(j<i)
swap(a,i,j);
}
}
//并不是把p作为枢纽返回,只是当成大小划分标准
//排序后的中间值j+1才是枢纽,用p当标准不需要最后批量后移
if(a[p]<a[j+1])
swap(a,p,j+1);
return j+1;
}
}
排序java实现
最新推荐文章于 2024-08-29 18:44:48 发布