Java implements an array of bubbles, select, insert Sort

Source: Internet
Author: User

"Notes" bubble, select, insert three of the sorting algorithm time complexity is O (N2) level, the order is mainly to perform comparison and exchange (copy) operations, relatively speaking: choose a sort of more bubble sort to reduce the number of exchanges;
The left side of the insertion sort is kept orderly.
"Reference book" Java Data structure and algorithm

The following is the source code, the operation for the array, the access element is the basic type:


1 ImportJava.util.Random;2 3 classhigharray{4 Private Long[] array;5 Private intNelems,i_mark;6 PublicHigharray (intmax) {7array=New Long[Max];8Nelems=0;9 }Ten Public BooleanFindLongSearchkey) { One for(i_mark=0;i_mark<nelems;i_mark++) A if(array[i_mark]==Searchkey) - Break; - if(i_mark==nelems) the return false; - Else - return true; - } + Public BooleanBinarySearch (LongSearchkey) { - intLowerbound=0; + intHighbound=nelems-1; A intCurin=0; at while(true){ -curin= (Highbound+lowerbound)/2; - if(array[curin]==Searchkey) - return true; - Else if(lowerbound>highbound) - return false; in Else{ - for(inti=lowerbound;i){ to if(array[i]>Searchkey) +Highbound=curin-1; - if(array[i]<Searchkey) theLowerbound=curin+1; * } $ }Panax Notoginseng } -}//find:1~log^n+1 the Public voidInsertLongvalue) { +array[nelems]=value; Anelems++; the } + Public voidBubblesort () { - for(inti=0;i<nelems;i++) $ for(intj=0;j<nelems-1-i;j++) $ if(array[j]>array[j+1]){ -Swap (j,j+1); - } the}//sort:o (n*n) - Public voidSelectsort () {Wuyi for(inti=0;i<nelems-1;i++) the for(intj=i+1;j<nelems;j++) - if(array[i]>Array[j]) Wu swap (I,J); -}//Sort:o (n*n), relatively speaking,it ' s better than the Bubblesort,because the count of swaps About Public voidSwapintOneintBoth ) { $ Longtemp; -temp=Array[one]; -array[one]=Array[two]; -array[two]=temp; A}//Sort:o (n*n) but better than before + the Public voidInsertsort () { - intin ; $ intOut ; the Longtemp; the for(out=nelems;out>0;out--){ thein=Out ; thetemp=Array[in]; - while(in>0&&array[in-1]>temp) { inArray[in]=array[in-1]; thein--; the } About } the } the Public BooleanDeleteLongvalue) { the if(Find (value)) { + for(intk=i_mark;k<nelems-1;k++) -Array[k]=array[k+1]; thenelems--;Bayi return true; the } the Else - return false; - } the Public voiddisplay () { the for(intj=0;j<nelems;j++) theSystem.out.println ("array[" +j+ "] =" +array[j]); the } - } the the Public classarrayquery{ the 94 Public Static voidMain (string[] args) { the Booleanflag=false; the intmax=1000; theHigharray harr=NewHigharray (max);98 for(intj=0;j<max-1;j++) AboutHarr.insert (NewRandom (). Nextint (max)); - Harr.display ();101 if(Harr.find (13))102System.out.println ("We find it:");103 Else104System.out.println ("There is no:"); the if(Harr.delete (15)){106System.out.println ("Delete success:");107 Harr.display ();108 }109 Else{ theSystem.out.println ("Delete fail:"); 111 Harr.display (); the }113 Longb; theA =System.nanotime (); the Harr.bubblesort (); theb =system.nanotime ();117System.out.println ("Bubblesort result:time =" + (b-a) + "ns.");118 //Harr.display ();119 - 121A =system.nanotime ();122 Harr.selectsort ();123b =system.nanotime ();124System.out.println ("Selectsort result:time =" + (b-a) + "ns."); the //Harr.display ();126 127A =system.nanotime (); - Harr.insertsort ();129b =system.nanotime (); theSystem.out.println ("Insertsort result:time =" + (b-a) + "ns.");131 //Harr.display (); the }133}

Java implements an array of bubbles, select, insert Sort

Related Article

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.