谁能帮忙写一个java的合并排序和二分查找的程序?10万个数统计时间太短,我多写个循环给你吧
public class Testchazhao{
public static void main(String[] args){
int[] array = new int[100000];
int i = 0;
int j = 0;
int[] count = {0};
for(i = 0;i100000;i){
array[i] = (int)(Math.random()*100000000);
}
int test = (int)(Math.random()*100000);
long start = System.currentTimeMillis();
for(j = 0;j100000;j){
for(i = 0;i100000;i){
if(array[i] == test){
break;
}
}
}
long end = System.currentTimeMillis();
long time1 = end-start;
System.out.println(time1);
fen(array,0,100000);
start = System.currentTimeMillis();
for(j = 0;j100000;j){
erfen(array,0,99999,test,count);
}
end = System.currentTimeMillis();
long time2 = end - start;
System.out.println(time2);
}
public static void fen(int[] array,int p,int r){
if(pr){
int q = (p r)/2;
fen(array,p,q);
fen(array,q 1,r);
he(array,p,q,r);
}
}
public static void he(int[] array,int p,int q,int r){
int i = 0;
int j = 0;
int k = 0;
int lenthleft = q-p;
int lenthright = r-q;
int[] arrayleft = new int[lenthleft 1];
int[] arrayright = new int[lenthright 1];
for(i = 0;ilenthleft;i){
arrayleft[i] = array[p i];
}
for(j = 0;jlenthright;j){
arrayright[j] = array[q j];
}
arrayleft[lenthleft] = arrayright[lenthright] = 2000000000;
i = j = 0;
for(k = p;kr;k){
if(arrayleft[i] = arrayright[j]){
array[k] = arrayleft[i];
i;
}
else{
array[k] = arrayright[j];
j;
}
}
}
public static void erfen(int[] array,int qian,int hou,int key,int[] count){
int temp = 0;
if(key = array[qian]key = array[hou] count[0] != 1){
if(qianhou){
temp = (qian hou)/2;
if(key == array[temp]||key == array[qian]||key == array[hou]){
count[0] = 1;
}
else if(keyarray[temp]){
erfen(array,qian,temp,key,count);
}
else{
erfen(array,temp,hou,key,count);
}
}
}
}
}
求一个java的二分查找的程序代码//你那程序太难改了,每个方法都单职责啊
public class Test6 {
//二分查找
public static int findPos(int[] a,int key) {
int start=0;
int end=a.length-1;
int temp=0;
while(startend){
int mid=(start end)/2;
if(keya[mid]){
start=mid 1;
temp=start;
}else if(keya[mid]){
end=mid-1;
temp=end;
}else {
return mid;
}
}
return temp;
}
public static void main(String[] args) {
int[]array={1,4,6,7,10,11,23,78};
System.out.println(findPos(array, 0));
}
}
急求java冒泡排序 快速排序 二分之一查找的代码//标记法
public class Test {
public static void main(String[] args){
int[] array = {9,7,5,8,7,5,3,8,4,2,6,1,0};
for(int i = 0; iarray.length; i){
int temp = i;
for(int j=i 1; jarray.length; j){
if(array[temp]array[j]){
temp=j;
}
}
int buff = array[temp];
array[temp] = array[i];
array[i] = buff;
}
for(int a : array){
System.out.println(a);
}
}
}
//二分之一
public class Test {
public void find(int x){
int[] s = {1,2,3,6,7,8,9,12,13,14,15,16,17,18,19,20,23,25,27,30,32,40,50};
int start = 0;
int end = s.length;
int half = end/2;
while(true){
if(s[half]x){
start = half;
half = half(end-start)/2;
}else if(s[half]x){
end = half;
half = half - (end-start)/2;
}else{
System.out.println(half 1);
break;
}
}
}
public static void main(String[] args){
new Test().find(20);
}
}
【java二分排序代码 java 二分排序】java二分排序代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 二分排序、java二分排序代码的信息别忘了在本站进行查找喔 。
推荐阅读
- 怎么进入西瓜数据查公众号的简单介绍
- 配置postgresqlmacos,配置windows失败还原更改请勿关闭计算机
- 游戏竞技男孩,小孩竞技类游戏
- mysql怎么下载和安装 mysql下载安装
- 宏定义c语言百度,c语言宏定义的数值是什么类型
- 轻解谜游戏,解谜游戏推荐知乎
- 3d网络游戏开发,3d网络游戏开发培训
- mysql两表怎么连接 mysql链接两个表
- 有微软的erp系统吗,微软erp系统用什么语言