Java binary find bubble sort selection sort

Source: Internet
Author: User

More search can refer to http://www.cnblogs.com/liuling/p/2013-7-24-01.html this is someone else's resources, feel write very full. You can study it carefully. /* Use binary to find the premise that the data is ordered (ascending). */class Halfsearchdemo {public static void main (string[] args) {int[] arr={1,2,3,6,2,3,5,1,8,9,3,5,2};sop ("keyword location:" + Halfsearch_2 (arr,6)); System.out.println ("Hello world!");} public static int halfsearch_2 (int[] arr,int key) {searchsort (arr); int Max=arr.length-1,min=0,mid;while (Min<=max) { Note is "<=" mid= (Min+max)/2;if (Key>arr[min]) Min=mid+1;else if (Key<arr[max]) Max=mid-1;elsereturn mid;} return-1;} public static void Pubblesort (int[] arr)//bubble sort, first determines the last position on the element {for (int i=0;i<arr.length-1;i++) {for (int j=0;j< arr.length-1-i;j++) {if (arr[j]>arr[j+1]) {int temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}} for (int k=0;k<arr.length;k++) SOP (Arr[k]);} public static void Searchsort (int[] arr)//Select sort first to determine the first position of the element {for (int. i=0;i<arr.length-1;i++) {for (int j=i+1;j< arr.length;j++) {if (arr[i]>arr[j]) {int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}}} for (int k=0;k<arr.length;k++) sOP (Arr[k]);} public static void Sop (Object obj) {System.out.println (obj);}}

The following is another representation of the binary lookup:

public static int Halfsearch (int[] arr,int key) {selectsort (arr); int max=arr.length-1;int min=0;int mid= (max+min)/2; while (Arr[mid]!=key) {if (Key>arr[mid]) min=mid+1;    if (Key<arr[mid]) max=mid-1;if (Min>max) return-1;//cannot find the target element mid= (max+min)/2;} Return mid;//jumps out of the loop, indicating that the target element has been found, at which point the target element's index is returned. }



Java binary find bubble sort selection sort

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.