⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binarysearch.java

📁 Binary Search in Java. This can read in a .txt file and do search,sort on the file and output the so
💻 JAVA
字号:
//------------------------------------------------------------------------------//Programmer:	Jun Cui //Date:         08.05.2008//Assignment:   IT214 Java Lab 6 //Software:     Netbeans IDE 6.0//------------------------------------------------------------------------------package pbinarysearch;/** * * @author CUIJ2 */public class BinarySearch {    public void selectSort (String[] numbers)    {                int smallest = 0;                       //used for comparisons        int newSmallest = 0;                    //used for comparisons            for (int i = 0; i < numbers.length-1; i++)          {           smallest = i;       //first number is set to smallest           newSmallest = i;    // new smallest is reset too           for (int j = i + 1; j < numbers.length; j++)           {                   if (numbers[j].compareTo(numbers[newSmallest]) <= 0)                    newSmallest =j;         //finds the smallest in array                           } //compare new smallest with smallest and swap if so           //numbers[newSmallest]is alphabetically less than numbers[smallest]            if (numbers[newSmallest]. compareTo(numbers[smallest]) < 0)                  {                        String temp= numbers[smallest];		//swap                 numbers[smallest]=numbers[newSmallest];    //swap                 numbers[newSmallest]= temp;		//swap                            }        }    }        public String search(String[]numArray, String wanted)    {        int left = 0;        int right = (numArray.length - 1);                int middle;                       while(left <= right)        {            middle = (left + right)/2;            if (numArray[middle].equals(wanted))                return "Found at " + middle;            else                 if(numArray[middle].compareTo(wanted) < 0)                    left = middle + 1;                else                     //numArray[middle]is alphabetically greater than wanted                    if(numArray[middle].compareTo(wanted) > 0)                        right = middle - 1;                                }        return "Sorry! Can not find the string!";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -