Search Algorithm Summary

Source: Internet
Author: User

Simple search

 

JAVA Implementation

package sort;public class SimpleSearch {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        int[] arr={70,80,31,37,10,1,48,60,33,80};                System.out.print(simpleSearch(arr, 31));    }        public static int simpleSearch(int arr[],int element){                int i;                for(i=0; i<arr.length; i++)        {            if(arr[i]==element)                return i;        }                return -1;    }    }

 

Binary Search

JAVA Implementation

package sort;public class SimpleSearch {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        int[] arr={70,80,31,37,10,1,48,60,33,80};                System.out.print(simpleSearch(arr, 31));    }        public static int simpleSearch(int arr[],int element){                int i;                for(i=0; i<arr.length; i++)        {            if(arr[i]==element)                return i;        }                return -1;    }    }

 

February search

JAVA Implementation

Package sort; public class fibonaccisearch {public static int search (INT [] Number, int des) {int [] fib = createfibonacci (number. length); int x = findx (FIB, number. length + 1, des); int M = number. length-fib [X]; X --; int I = x; If (number [I] <des) I + = m; while (FIB [x]> 0) {If (number [I] <des) I + = fib [-- X]; else if (number [I]> des) I-= fib [-- X]; else return I;} return-1;} Private Static int [] Createfibonacci (INT max) {int [] fib = new int [Max]; for (INT I = 0; I <fib. length; I ++) {fib [I] = integer. min_value;} fib [0] = 0; FIB [1] = 1; for (INT I = 2; I <Max; I ++) FIB [I] = fib [I-1] + fib [I-2]; return fib;} Private Static int findx (INT [] fib, int N, int des) {int I = 0; while (FIB [I] <= N) I ++; I --; return I;} public static void main (string [] ARGs) {int [] Number = {1, 4, 2, 6, 7, 3, 9, 8}; quicksort. quicksort (number); int find = maid. Search (number, 3); If (find! =-1) system. Out. println ("locate data value in index" + find); else system. Out. println ("cannot find data value ");}}

 

Search Algorithm Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.