第二次实验

快速排序

</pre><pre name="code" class="java">import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

//快速排序
public class w {  
	
	public static void main(String[] args){  
       		double start_all = System.currentTimeMillis();
		String path = "D://UserData///Administrator///桌面//largeW.txt";
		ArrayList<Integer> list=read(path); 
		double start = System.currentTimeMillis();
		   
		double end = System.currentTimeMillis();
		
		
		w quickSort = new w();  
        quickSort.quickSort(list,0,list.size()-1); 
    
        
        System.out.println("快速排序时间为: " + (end - start)+"毫秒");
        // 写入txt文件
        write(list);
        
		double end_all = System.currentTimeMillis();
		System.out.println("总运行时间为: " + (end_all - start_all)+"毫秒");
    }  
	
	//创建并写入largeW_quick.txt文件
	public static void write(ArrayList<Integer> list) {
		File f = new File("D://UserData///Administrator///桌面//largeW.txt");
		FileOutputStream fou = null;
		try {
			fou = new FileOutputStream(f, false);// true,设置可追加

			for (int i = 0; i < list.size(); i++) {
				String s = String.valueOf(list.get(i));
				String a = "" + s + "\t\n";
				// byte []bytes=new byte[1024];
				// 如何把string转换byte数组
				fou.write(a.getBytes());

			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				fou.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	 public void quickSort(ArrayList<Integer> list,int first, int end){  
	        if(first<end){  
	            int pivot = partition(list,first,end);  
	            quickSort(list,first,pivot-1);  
	            quickSort(list,pivot+1,end);  
	        }  
	    }      
    public  int partition(ArrayList<Integer> a, int first , int end){ //对数据进行快速排序 
        int i = first; int j = end;  
        while(i<j){  
            while(i<j&&a.get(i)<=a.get(j))j--;  
            if(i<j){  
                int temp;  
                temp = a.get(i);  
                a.set(i, a.get(j));  
                a.set(j, temp);  
                i++;  
            }  
            while(i<j&&a.get(i)<=a.get(j))i++;  
            if(i<j){  
                int temp;  
                temp = a.get(j);  
                a.set(j, a.get(i));  
                a.set(i, temp); 
                j--;  
            }  
        }  
       
       
        
            return i ;  
    }  
     
  //读取文件到int数组
	public static ArrayList read(String path) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		BufferedReader input = null;
		try {
			FileReader in = new FileReader(path);
			input = new BufferedReader(in);
			String ss;
			try {
				while ((ss = input.readLine()) != null) {
					String[] s = ss.split("\r\n");
					for (int i = 0; i < s.length; i++) {
						list.add(Integer.parseInt(s[i].trim())); // 将String s中的内容添加到动态数组中
					}
				}
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			in.close();
			input.close();
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

		return list;
	}
   
}  

工作与实习:

目前正在努力考研和考公务员

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值