Over San Guan Java bubble Sort Select sort Insert sort small exercise

Source: Internet
Author: User

Material: Monkey sort, according to height from small to large to sort.

First Pass:

The old monkeys led the small monkeys team by the size of a comparison, exchange, began to height ranks. (Bubble sort)

Second Pass:

Too slow, to make the first level more difficult, select sort

Third Pass:

Finally, try to sort by using the Insert Sort method.

Test class:

ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;//function: Monkey sort Public classMonkeyselect { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//define an array of objects that can fit 5 monkeys        intSize = 5; Monkey[] Monkeys=NewMonkey[size]; InputStreamReader ISR=NewInputStreamReader (system.in); BufferedReader BR=NewBufferedReader (ISR); //initialize each of the monkeys         for(inti = 0; i < monkeys.length; i++) {System.out.println ("Please enter" + (i + 1) + "Height of monkey only"); Try{String height=Br.readline (); Monkeys[i]=NewMonkey ((i + 1) + "", float.parsefloat (height)); } Catch(NumberFormatException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        //let the old monkeys sortMonkey Oldmonkey =NewMonkey ("1.2f",); //Bubble Sortoldmonkey.bubblesort (monkeys); //oldmonkey.selectsort (monkeys); Select Sort Call//oldmonkey.insertsort (monkeys); Insert sort Call//Validationoldmonkey.show (monkeys); }}

Monkey Class (function):

//MonkeyclassMonkey {PrivateString Monkeyid; Private floatheight;  PublicMonkey (String Monkeyid,floatheight) {         This. Monkeyid =Monkeyid;  This. Height =height; }    //Insert Sort     Public voidInsertsort (monkey[] monkeys) {//Start Sorting         for(inti = 0; i < monkeys.length; i++) {            //record the height of the monkey first            floatInsertheight =Monkeys[i].height; String Insertno=Monkeys[i].monkeyid; intInsertindex = i-1;  while(insertindex >= 0 && monkeys[insertindex].height >insertheight) {Monkeys[insertindex+ 1].height =Monkeys[insertindex].height; Monkeys[insertindex+ 1].monkeyid =Monkeys[insertindex].monkeyid; Insertindex--; }            //InsertMonkeys[insertindex + 1].height =Insertheight; Monkeys[insertindex+ 1].monkeyid =Insertno; }    }    //Select Sort     Public voidSelectsort (monkey[] monkeys) {floatTempheight = 0.0f; String Tempno= "";  for(inti = 0; i < monkeys.length; i++) {            floatMinHeight =Monkeys[i].height; intMinindex =i; //and the back of the comparison             for(intj = i + 1; J < Monkeys.length; J + +) {                if(MinHeight >monkeys[j].height) {                    //Fix the minimum valueMinHeight =Monkeys[j].height; //correction of subscriptMinindex =J; }            }            if(Minindex! =i) {//ExchangeTempheight =Monkeys[minindex].height; Monkeys[minindex].height=Monkeys[i].height; Monkeys[i].height=Tempheight; Tempno=Monkeys[minindex].monkeyid; Monkeys[minindex].monkeyid=Monkeys[i].monkeyid; Monkeys[i].monkeyid=Tempno; }        }    }    //Bubbling Queue     Public voidBubblesort (monkey[] monkeys) {floatTempheight = 0.0f; String Tempno= ""; //bubbling         for(inti = 0; i < monkeys.length-1; i++) {             for(intj = 0; J < monkeys.length-1-I; J + +) {                //Judging                if(Monkeys[j].height > Monkeys[j + 1].height) {//HeightTempheight =Monkeys[j].height; Monkeys[j].height= Monkeys[j + 1].height; Monkeys[j+ 1].height =Tempheight; //numberingTempno =Monkeys[j].monkeyid; Monkeys[j].monkeyid= Monkeys[j + 1].monkeyid; Monkeys[j+ 1].monkeyid =Tempno; }            }        }    }    //Display Queue     Public voidShow (monkey[] monkeys) { for(inti = 0; i < monkeys.length; i++) {System.out.println ("Monkey number:" + Monkeys[i].monkeyid + "Height:" +monkeys[i].height); }    }}

Over San Guan Java bubble Sort Select sort Insert sort small exercise

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.