第二次实验 快速排序 实习与工作

来源:互联网 发布:java等腰三角形代码 编辑:程序博客网 时间:2024/05/16 02:06

快速排序:冒泡排序可参考第三次作业

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 exceptione.printStackTrace();} finally {try {fou.close();} catch (Exception e) {// TODO Auto-generated catch blocke.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;}   }  


实习与工作:

       作为一名大三的学生来说,对于实习和工作已经很有必要了,在此前我已经在51job和智联招聘网上的几家公司

0 0
原创粉丝点击