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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

判断一个数是否存在于一个非递减的有序数列中 算法(Ordered Search Problem)

發布時間:2025/3/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 判断一个数是否存在于一个非递减的有序数列中 算法(Ordered Search Problem) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. Description

Given a list of?

n?numbers in non-decreasing order?A={a1,a2,?,an}?such that?a1a2?an?and a number?x, the objective is to determine if?x?is present in the list?A

2. Algorithm

Algorithm 1.
  • Linear Search Algorithm
    • Iterate through?n?numbers to find the?x.
Algorithm 2.
  • Binary Search Algorithm.
3. Time complexity

Algorithm 1.

O(n)


Algorithm 2.

O(logn)

4. Implementation

Algorithm 1.

public class LinearSearch {public static void Result(int Seq[],int Len,int x){for(int i=0;i<Len;i++){if(x==Seq[i]){System.out.println(x+" is present at seq["+i+"]");break;}else if(x!=Seq[i]&&i==Len-1){System.out.println(x+" is not present in seq");}}}public static void main(String[] args){int Seq[]={4,33,40,44,45,45,55,60,99};int Len=Seq.length;Result(Seq,Len,55); Result(Seq,Len,70);} }Result of Algorithm1:



Algorithm 2-Implementation1

public class BinarySearch1 {public static void BinarySearch(int Seq[],int x,int left,int right){ int mid=(left+right)/2;if(left==right-1&&x!=Seq[left]&&x!=Seq[right]) //如果只剩兩個數,兩個數都不是x,那么x不在數組Seq中{System.out.println(x+" is not present in Seq");}else if(x==Seq[mid]){System.out.println(x+" is present at Seq["+mid+"]");}else if(x>Seq[mid]){ System.out.println(x+" is between seq["+mid+"] and Seq["+right+"]");left=mid;BinarySearch(Seq,x,left,right);}else if(x<Seq[mid]){System.out.println(x+" is between seq["+left+"] and Seq["+mid+"]");right=mid;BinarySearch(Seq,x,left,right);}}public static void main(String args[]){int Seq[]={1,2,3,3,8,9,11,56,89};int left=0, right=Seq.length-1;System.out.println("Test whether 56 is present in seq:");BinarySearch(Seq,56,left,right);System.out.println("Test whether 7 is present in seq:");BinarySearch(Seq,7,left,right);} }

Algorithm 2-Implementation2

public class BinarySearch2 {public static void BinarySearch(int Seq[],int x){int left=0;int right=Seq.length-1;int mid=(left+right)/2;while(true){if(x==Seq[mid]){System.out.println(x+" is present at Seq["+mid+"]");break;}else if(x>Seq[mid]){System.out.println(x+" is between seq["+mid+"] and Seq[" +right+"]"); left=mid+1;if(left>=right){System.out.println(x+" is not in seq");break;}mid=(left+right)/2;}else if(x<Seq[mid]){System.out.println(x+" is between seq["+left+"] and Seq[" +mid+"]"); right=mid-1; if(left>=right){System.out.println(x+" is not in seq");break;}mid=(left+right)/2;}}}public static void main(String[] args){int Seq[]={4,33,40,44,45,45,55,60,99}; System.out.println("Test whether 56 is present in seq:"); BinarySearch(Seq,55); System.out.println("Test whether 7 is present in seq:"); BinarySearch(Seq,70); } }Result of Both implementation1 and implementation2:




總結

以上是生活随笔為你收集整理的判断一个数是否存在于一个非递减的有序数列中 算法(Ordered Search Problem)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。