快速排序:冒泡排序可参考第三次作业
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;
}
}
实习与工作:
作为一名大三的学生来说,对于实习和工作已经很有必要了,在此前我已经在51job和智联招聘网上的几家公司