欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

二分排序java代碼,二分排序js

Java的線性查找,二分查找,冒泡排序,插入排序,快速排序的源代碼

C++的,只要把,函數(shù)名改下,輸出語(yǔ)句改下,就可以了。希望對(duì)你有幫助

永年網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)建站自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站

void Sort :: SelectionSort(int a[],int n)

{

bool sorted=false;

cout"中間過程為:"endl;

for(int size=n;!sorted size1;size--){

int pos = 0;

sorted = true;

for(int i=1;isize;i++)

if(a[pos]=a[i])pos=i;

else sorted=false;

Swap(a[pos],a[size-1]);

for(int j=0;j!=n;j++){//顯示中間過程

couta[j]" ";

}

coutendl;

}

cout"選擇排序結(jié)果為:"endl;

for(int i=0;i!=n;i++){

couta[i]" ";

}

coutendl;

}

/*

冒泡排序

*/

bool Sort :: Bubble(int a[],int m,int n)

{

bool swapped=false;

for(int i=0;im-1;i++){

if(a[i]a[i+1]){

Swap(a[i],a[i+1]);

swapped=true;

for(int j=0;j!=n;j++){//顯示中間過程

couta[j]" ";

}

coutendl;

}

}

return swapped;

}

void Sort :: BubbleSort(int a[],int n)

{

cout"中間過程為:"endl;

for(int i=n;i1 Bubble(a,i,n);i--);

coutendl;

cout"冒泡排序結(jié)果為:"endl;

for(i=0;i!=n;i++){

couta[i]" ";

}

coutendl;

}

/*

插入排序

*/

void Sort :: InsertionSort(int a[],int n)

{

cout"中間過程為:"endl;

for (int i=1;in;i++){

int t=a[i];

int j;

for (j=i-1;j=0 ta[j];j--){

a[j+1]=a[j];

}

a[j+1]=t;

for(int k=0;k!=n;k++){//顯示中間過程

couta[k]" ";

}

coutendl;

}

cout"插入排序結(jié)果為:"endl;

for(i=0;i!=n;i++){

couta[i]" ";

}

coutendl;

}

/*

基數(shù)排序

*/

void Sort :: RadixSort(int a[],int n)

{

int d=1;

int m=10;

for (int i=0;in;i++){

while(a[i]=m){

m*=10;

++d;

}

}

int *t=new int[n];

int *count=new int [10];

int radix=1,k;

for(i=1;i=d;i++){

for(int j=0;j10;j++){

count[j]=0;//每次分配前清空計(jì)數(shù)器

}

for(j=0;jn;j++){

k=(a[j]/radix)%10;//統(tǒng)計(jì)每個(gè)桶中的記錄數(shù)

count[k]++;

}

cout"分桶顯示:"endl;

for(j=0;j10;j++){//顯示中間xiangxi過程

if(count[j]!=0){

coutj": ";

for(int l=0;ln;l++){

if ((a[l]/radix)%10==j)

couta[l]" ";

}

coutendl;

}

}

coutendl;

for(j=1;j10;j++){

count[j]=count[j-1]+count[j];

}

for(j=n-1;j=0;j--){

k=(a[j]/radix)%10;

count[k]--;

t[count[k]]=a[j];

}

for(j = 0;j n;j++) {

a[j]=t[j];

}

radix=radix*10;

cout"按桶依次排序排序:"endl;

for(j=0;j!=n;j++){//顯示中間過程

couta[j]" ";

}

coutendl;

}

delete[] t;

delete[] count;

cout"基數(shù)排序結(jié)果為:"endl;

for(i=0;i!=n;i++){

couta[i]" ";

}

coutendl;

}

急求java冒泡排序 快速排序 二分之一查找的代碼

//標(biāo)記法

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二分法查找的遞歸算法怎么實(shí)現(xiàn)

什么是二分查找?

二分查找也稱折半查找(Binary Search),它是一種效率較高的查找方法。但是,折半查找要求線性表必須采用順序存儲(chǔ)結(jié)構(gòu),而且表中元素按關(guān)鍵字有序排列。

二分查找優(yōu)缺點(diǎn)

優(yōu)點(diǎn)是比較次數(shù)少,查找速度快,平均性能好;

其缺點(diǎn)是要求待查表為有序表,且插入刪除困難。

因此,折半查找方法適用于不經(jīng)常變動(dòng)而查找頻繁的有序列表。

使用條件:查找序列是順序結(jié)構(gòu),有序。

過程

首先,假設(shè)表中元素是按升序排列,將表中間位置記錄的關(guān)鍵字與查找關(guān)鍵字比較,如果兩者相等,則查找成功;否則利用中間位置記錄將表分成前、后兩個(gè)子表,如果中間位置記錄的關(guān)鍵字大于查找關(guān)鍵字,則進(jìn)一步查找前一子表,否則進(jìn)一步查找后一子表。重復(fù)以上過程,直到找到滿足條件的記錄,使查找成功,或直到子表不存在為止,此時(shí)查找不成功。

利用循環(huán)的方式實(shí)現(xiàn)二分法查找

public class BinarySearch {

public static void main(String[] args) {

// 生成一個(gè)隨機(jī)數(shù)組 ? ? ? ?int[] array = suiji();

// 對(duì)隨機(jī)數(shù)組排序 ? ? ? ?Arrays.sort(array);

System.out.println("產(chǎn)生的隨機(jī)數(shù)組為: " + Arrays.toString(array));

System.out.println("要進(jìn)行查找的值: ");

Scanner input = new Scanner(System.in);

// 進(jìn)行查找的目標(biāo)值 ? ? ? ?int aim = input.nextInt();

// 使用二分法查找 ? ? ? ?int index = binarySearch(array, aim);

System.out.println("查找的值的索引位置: " + index);

}

/** ? ? * 生成一個(gè)隨機(jī)數(shù)組 ? ? *

* @return 返回值,返回一個(gè)隨機(jī)數(shù)組 ? ? */

private static int[] suiji() {

// random.nextInt(n)+m ?返回m到m+n-1之間的隨機(jī)數(shù) ? ? ? ?int n = new Random().nextInt(6) + 5;

int[] array = new int[n];

// 循環(huán)遍歷為數(shù)組賦值 ? ? ? ?for (int i = 0; i array.length; i++) {

array[i] = new Random().nextInt(100);

}

return array;

}

/** ? ? * 二分法查找 ?---循環(huán)的方式實(shí)現(xiàn) ? ? *

* @param array 要查找的數(shù)組 ? ? * @param aim 要查找的值 ? ? * @return 返回值,成功返回索引,失敗返回-1 ? ? */

private static int binarySearch(int[] array, int aim) {

// 數(shù)組最小索引值 ? ? ? ?int left = 0;

// 數(shù)組最大索引值 ? ? ? ?int right = array.length - 1;

int mid;

while (left = right) {

mid = (left + right) / 2;

// 若查找數(shù)值比中間值小,則以整個(gè)查找范圍的前半部分作為新的查找范圍 ? ? ? ? ? ?if (aim array[mid]) {

right = mid - 1;

// 若查找數(shù)值比中間值大,則以整個(gè)查找范圍的后半部分作為新的查找范圍 ? ? ? ? ? ?} else if (aim array[mid]) {

left = mid + 1;

// 若查找數(shù)據(jù)與中間元素值正好相等,則放回中間元素值的索引 ? ? ? ? ? ?} else {

return mid;

}

}

return -1;

}}

運(yùn)行結(jié)果演示:

由以上運(yùn)行結(jié)果我們得知,如果要查找的數(shù)據(jù)在數(shù)組中存在,則輸出該數(shù)據(jù)在數(shù)組中的索引;如果不存在則輸出 -1 ,也就是打印 -1 則該數(shù)在數(shù)組中不存在,反之則存在。

四、利用遞歸的方式實(shí)現(xiàn)二分法查找

public class BinarySearch2 {

public static void main(String[] args) {

// 生成一個(gè)隨機(jī)數(shù)組 ? ? ? ?int[] array = suiji();

// 對(duì)隨機(jī)數(shù)組排序 ? ? ? ?Arrays.sort(array);

System.out.println("產(chǎn)生的隨機(jī)數(shù)組為: " + Arrays.toString(array));

System.out.println("要進(jìn)行查找的值: ");

Scanner input = new Scanner(System.in);

// 進(jìn)行查找的目標(biāo)值 ? ? ? ?int aim = input.nextInt();

// 使用二分法查找 ? ? ? ?int index = binarySearch(array, aim, 0, array.length - 1);

System.out.println("查找的值的索引位置: " + index);

}

/** ? ? * 生成一個(gè)隨機(jī)數(shù)組 ? ? * ? ? * @return 返回值,返回一個(gè)隨機(jī)數(shù)組 ? ? */

private static int[] suiji() {

// Random.nextInt(n)+m ?返回m到m+n-1之間的隨機(jī)數(shù) ? ? ? ?int n = new Random().nextInt(6) + 5;

int[] array = new int[n];

// 循環(huán)遍歷為數(shù)組賦值 ? ? ? ?for (int i = 0; i array.length; i++) {

array[i] = new Random().nextInt(100);

}

return array;

}

/** ? ? * 二分法查找 ---遞歸的方式 ? ? * ? ? * @param array 要查找的數(shù)組 ? ? * @param aim ? 要查找的值 ? ? * @param left ?左邊最小值 ? ? * @param right 右邊最大值 ? ? * @return 返回值,成功返回索引,失敗返回-1 ? ? */

private static int binarySearch(int[] array, int aim, int left, int right) {

if (aim array[left] || aim array[right]) {

return -1;

}

// 找中間值 ? ? ? ?int mid = (left + right) / 2;

if (array[mid] == aim) {

return mid;

} else if (array[mid] aim) {

//如果中間值大于要找的值則從左邊一半繼續(xù)遞歸 ? ? ? ? ? ?return binarySearch(array, aim, left, mid - 1);

} else {

//如果中間值小于要找的值則從右邊一半繼續(xù)遞歸 ? ? ? ? ? ?return binarySearch(array, aim, mid + 1, array.length-1);

}

}}

運(yùn)行結(jié)果演示:

總結(jié):

遞歸相較于循環(huán),代碼比較簡(jiǎn)潔,但是時(shí)間和空間消耗比較大,效率低。在實(shí)際的學(xué)習(xí)與工作中,根據(jù)情況選擇使用。通常我們?nèi)绻褂醚h(huán)實(shí)現(xiàn)代碼只要不是太繁瑣都選擇循環(huán)的方式實(shí)現(xiàn)~

用java寫二分搜索,要求數(shù)組是由用戶輸入,再輸入時(shí),數(shù)組是無(wú)序的,要對(duì)數(shù)組進(jìn)行從小到大的排序

二分查找又稱折半查找,它是一種效率較高的查找方法。

【二分查找要求】:1.必須采用順序存儲(chǔ)結(jié)構(gòu) 2.必須按關(guān)鍵字大小有序排列。

/**

* 二分查找又稱折半查找,它是一種效率較高的查找方法。

【二分查找要求】:1.必須采用順序存儲(chǔ)結(jié)構(gòu) 2.必須按關(guān)鍵字大小有序排列。

* @author Administrator

*

*/

public class BinarySearch {

public static void main(String[] args) {

int[] src = new int[] {1, 3, 5, 7, 8, 9};

System.out.println(binarySearch(src, 3));

System.out.println(binarySearch(src,3,0,src.length-1));

}

/**

* * 二分查找算法 * *

*

* @param srcArray

* 有序數(shù)組 *

* @param des

* 查找元素 *

* @return des的數(shù)組下標(biāo),沒找到返回-1

*/

public static int binarySearch(int[] srcArray, int des){

int low = 0;

int high = srcArray.length-1;

while(low = high) {

int middle = (low + high)/2;

if(des == srcArray[middle]) {

return middle;

}else if(des srcArray[middle]) {

high = middle - 1;

}else {

low = middle + 1;

}

}

return -1;

}

/**

*二分查找特定整數(shù)在整型數(shù)組中的位置(遞歸)

*@paramdataset

*@paramdata

*@parambeginIndex

*@paramendIndex

*@returnindex

*/

public static int binarySearch(int[] dataset,int data,int beginIndex,int endIndex){

int midIndex = (beginIndex+endIndex)/2;

if(data dataset[beginIndex]||datadataset[endIndex]||beginIndexendIndex){

return -1;

}

if(data dataset[midIndex]){

return binarySearch(dataset,data,beginIndex,midIndex-1);

}else if(datadataset[midIndex]){

return binarySearch(dataset,data,midIndex+1,endIndex);

}else {

return midIndex;

}

}

}

java 二分排序疑問

原理,就是拆半,兩邊分別排序。再拆半、再分別找;…………

誰(shuí)能幫忙寫一個(gè)java的合并排序和二分查找的程序?

10萬(wàn)個(gè)數(shù)統(tǒng)計(jì)時(shí)間太短,我多寫個(gè)循環(huán)給你吧

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;i 100000;i++){

array[i] = (int)(Math.random()*100000000);

}

int test = (int)(Math.random()*100000);

long start = System.currentTimeMillis();

for(j = 0;j 100000;j++){

for(i = 0;i 100000;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;j 100000;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(p r){

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;i lenthleft;i++){

arrayleft[i] = array[p+i];

}

for(j = 0;j lenthright;j++){

arrayright[j] = array[q+j];

}

arrayleft[lenthleft] = arrayright[lenthright] = 2000000000;

i = j = 0;

for(k = p;k r;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(qian hou){

temp = (qian+hou)/2;

if(key == array[temp]||key == array[qian]||key == array[hou]){

count[0] = 1;

}

else if(key array[temp]){

erfen(array,qian,temp,key,count);

}

else{

erfen(array,temp,hou,key,count);

}

}

}

}

}

文章標(biāo)題:二分排序java代碼,二分排序js
URL網(wǎng)址:http://www.chinadenli.net/article48/heighp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化企業(yè)建站營(yíng)銷型網(wǎng)站建設(shè)建站公司ChatGPT網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)