⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sort.java

📁 java实验答案。其中包括:金额的中文大写方式
💻 JAVA
字号:
import java.util.*;

class InsertSort extends Thread{
	private int[] a;
	Object obj;
	public InsertSort(int[] a,Object obj){
		this.a=a;
		this.obj=obj;
	}
	public void run(){
		synchronized(obj){
		    long time=System.currentTimeMillis();
		    int j;
  	    	for(int i=2;i<a.length;i++){
  	            if(a[i]<a[i-1]){ //需要将a[i]插入有序子表中
  		           a[0]=a[i];    //复制为哨兵
  		           a[i]=a[i-1];
  		           for( j=i-2;a[0]<a[j];j--)
  		              a[j+1]=a[j];    //记录后移
  		           a[j+1]=a[0];       //插入到正确位置
  		        }
  		
  	        }
  	        time=System.currentTimeMillis()-time;
  	        System.out.println("Use insert sort,the time is:"+time+"ms");
  	        System.out.println("After insert sort,the sorted array is:"); //打印排序数组   
            for(int i=1;i<a.length;i++)
               System.out.print(a[i]+" ");
            System.out.println();
		}
	}
	
	public static void main(String args[]){		
         Object obj=new Object();         
         Random r=new Random();
         int size=10;//数组个数      
         int randomArray1[]=new int[size];   //定义一个随机数组
         int randomArray2[]=new int[size];     //定义一个排序数组
    
         //生成两个同样的随机数组
         for(int i=1;i<size;i++)
              randomArray1[i]=r.nextInt()%1000;   //在随机数组中存入随机数
         for(int i=1;i<size;i++)
              randomArray2[i]=randomArray1[i];
         
         //打印随机数组         
         System.out.println("The random array is:");    
         for(int i=1;i<size;i++)
         System.out.print(randomArray1[i]+" ");
         System.out.println();
         
         (new InsertSort(randomArray1,obj)).start();
         (new BubbleSort(randomArray2,obj)).start();
		
	}
}

class BubbleSort extends Thread{
	private int[] a;
	Object obj;
	public BubbleSort(int[] a,Object obj){
		this.a=a;
		this.obj=obj;
	}
	public void run(){
		synchronized(obj){
		    long time=System.currentTimeMillis();
		    int temp,j;  	
  	        for(j=a.length-1;j>1;j--){  	
  	            for(int i=1;i<a.length-1;i++){
  		           if(a[i]>a[i+1]){
  			           temp=a[i];
  			           a[i]=a[i+1];
  			           a[i+1]=temp;
  	               }
  	            }
  	        }
  	        time=System.currentTimeMillis()-time;
  	        System.out.println("Use bubble sort,the time is:"+time+"ms");
  	        
  	        System.out.println("After bubble sort,the sorted array is:"); //打印排序数组   
            for(int i=1;i<a.length;i++)
               System.out.print(a[i]+" ");
            System.out.println();
		}
	}
}      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -