日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

快速排序和折半查找

發(fā)布時間:2023/12/13 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 快速排序和折半查找 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package BinarySerach;import java.util.Scanner;public class BinarySerch {/***折半查找和快速排序*/static final int N = 15;static void quickSort(int [] array,int left,int right){int f,t;int ltemp =left;int rtemp = right;//確定分界值f = array[(left+right)/2];while(ltemp < rtemp){while(array[ltemp] <f){++ltemp;}while(array[rtemp] > f){--rtemp;}if(ltemp <= rtemp){t = array[ltemp];array[ltemp] = array[rtemp];array[rtemp] = t;--rtemp;++ltemp;}if(ltemp == rtemp){ltemp++;}if(left < rtemp){quickSort(array, left, ltemp-1);}if(ltemp < right){quickSort(array, rtemp+1, right);}}}//折半查找static int searchFun(int a[],int n,int x){int mid,low,high;low = 0;high = n-1;while(low <= high){mid = (low+high)/2;if(a[mid] == x){return mid;}else if(a[mid] > x){high = mid -1;}else{low = mid+1;}}return -1;}public static void main(String[] args) {int [] shuzu = new int[N];int x,n,i;for(i=0;i<N;i++){shuzu[i] = (int)(100+Math.random()*(100+1));}for(i=0;i<N;i++){System.out.print(" "+shuzu[i]);}System.out.println("輸出排序的結(jié)果:");quickSort(shuzu, 0, N-1);for(i=0;i<N;i++){System.out.print(" "+shuzu[i]);}System.out.println();System.out.println("輸入要查找的數(shù)字");Scanner input = new Scanner(System.in);x = input.nextInt();n = searchFun(shuzu, N, x);System.out.println(n);if(n < 0){System.out.println("沒有找到");}else{System.out.println("數(shù)據(jù):"+x+"位于數(shù)組中的第"+(n+1)+"個元素處。");}}}

  

轉(zhuǎn)載于:https://www.cnblogs.com/aicpcode/p/4271366.html

總結(jié)

以上是生活随笔為你收集整理的快速排序和折半查找的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。