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

📄 zym9.java

📁 java写的一个多线程排序实验
💻 JAVA
字号:
import java.util.Random;
import java.io.*;


public class zym9 implements Runnable
{
	
	static int x[]=new int[10000];
	static int n = 1;
	static int a ;
	static long time_start;
	static long time_end;
	zym9(int j)
	{
		a =j;
	}
	
	public void run()
	{
	  time_start = System.currentTimeMillis();
	
	  Sort sort = new Sort();
	  if( a ==0)  sort.SelectSort(x,10000);
	  else sort.BobSort(x,10000);
	      
	  try{
	  	Thread.sleep((int)(Math.random()*1000));
	  } 
	  catch (InterruptedException ex)
	  {}
	  
	  time_end = System.currentTimeMillis();
	  System.out.print("线程方式需要的时间:");
	  System.out.print(time_end-time_start);

	  System.out.println("ms");
	  
	}
	
	public static void  main(String args[])
	{
         
        int i =0;
        for(i =0;i<10000;i++)
        {x[i]=(int)(Math.random()*100);}
         
        System.out.println("已经生成了10000个随机数字,请选择相应的操作。");

        Sort sort1 = new Sort();
        for(;n!=0;)
        {
        
        System.out.println("1:串行方式");
        System.out.println("2:线程方式");
        System.out.println("0:退出");
        System.out.print("请选择(输入相应的数字):");
        
        try{
       
         n =  numinput();
     
          }
       catch(IOException e)  
       {}
         switch( n)
         {
        	case 1:
            time_start = System.currentTimeMillis();
            sort1.SelectSort(x,10000);
            sort1.BobSort(x,10000);
            time_end = System.currentTimeMillis();
             
            System.out.print("串行方式需要的时间:");
            System.out.print(time_end-time_start);
            System.out.println("ms");
            break;
        
            case 2:
           
            Thread t1 = new Thread(new zym9(0));
            t1.start();
            
             
            Thread t2 = new Thread(new zym9(1));
            t2.start();
            
            break;
            default:
        }
        
       }  
       
	}
	

public  static int numinput()  throws java.io.IOException
{


   BufferedReader fig = new BufferedReader(new InputStreamReader(System.in));
   String s1 = fig.readLine();
   int n = Integer.valueOf(s1).intValue();
   return n;
}
}




 class Sort
{

public static void SelectSort(int A[] , int n)	//选择排序
	{
		int i;
		int j;
		int p;
		for(i = 1 ; i < n-1;i++)
		{
			p = i;
			for(j = i+1 ; j<n ; j++)
			{
			
				if(A[p]>A[j]) 
				{
					p=j;
				}
			}	
			if(i != p)
			{
				A[0] = A[i];
				A[i] = A[p];
				A[p] = A[0];
				 
			}
		}
		
	}
	
	public static void BobSort(int A[],int n)	//冒泡排序
	{
		int i;
		int j;
		int flag;
		for(i = 0; i < n-1 ; i++)
		{
			flag = 0;
			for(j=0 ; j<n-i-1 ;j++)
			{
			
				if(A[j]>A[j+1])
				{
					A[0] = A[j];
					A[j] = A[j+1];
					A[j+1] = A[0];
					flag = 1;
				
				}
			}
			if(flag == 0)
			  break;
		}
	
	}
}

⌨️ 快捷键说明

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