(1)算法基本功——快速排序
对文件 largeW.txt(下载链接)中的数据,应用快速排序算法进行排序,并与冒泡排序、归并排序进行时间比较。体验算法复杂度对设计算法的影响。
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Quicksort {
static String Wpath="src/largeW.txt";
public static void main(String []args){
try{
File file=new File(Wpath);
Scanner scan=new Scanner(file);
List<Integer> array=new ArrayList<Integer>();
while(scan.hasNext()==true){
array.add(scan.nextInt());
}
Integer[] num= (Integer[]) array.toArray(new Integer[array.size()]);
long start=System.currentTimeMillis();
Quicksort(num,0,num.length-1);
long end=System.currentTimeMillis();
System.out.println("花费的时间是:"+(end-start)+"毫秒");
}catch(Exception e){
e.printStackTrace();
}
}
public static void Quicksort(Integer []num,int left,int right){
if(left<right){
int par_middle=partition(num,left,right);
Quicksort(num,left,par_middle-1);
Quicksort(num,par_middle+1,right);
}
}
public static int partition(Integer num[],int left,int right){
int temp=num[left];//取数组的第一个数字作为中轴
while(left<right){
while(left<right&&num[right]>=temp){//判断从最后一个数往左稍描是否小于中轴
right--;
}
num[left]=num[right];//如果小于中轴,则交换位置
while(left<right&&num[left]<=temp){
left++;
}
num[right]=num[left];//如果大于中轴则交换位置
}
num[left]=temp;//中轴值得两边都已排序,记录中轴值,方便下次继续排序
return left;
}
}
(3)算法综合实践——搜索引擎
上网搜索有关“搜索引擎”的相关资料,包括但不限于以下方面(至少要有2个方面):搜索引擎岗位要求、搜索引擎工作原理、搜索引擎涉及到教材中哪些算法、搜索引擎的盈利模式、搜索引擎源码链接、国内外搜索引擎公司现状等。
答:搜索引擎算法:获得网站网页资料,建立数据库并提供查询的系统,我们都可以把它叫做搜索引擎。搜索引擎的数据库是依靠一个叫“网络机器人(crawlers)”或叫“网络蜘蛛(Spider)”的软件,通过网络上的各种链接自动获取大量网页信息内容,并按一定的规则分析整理形成的。搜索引擎的盈利模式:常见的有收费排名、购买关键词广告。著名的google的盈利模式是广告+技术授权授权费