Java bubbling algorithm and median algorithm

Source: Internet
Author: User

ImportJava.math.BigDecimal; Public classTest { Public Static voidMain (string[] args) {//Controlling random quantities        intc = 100; Number[] Array=NewNumber[c]; /*Initialize*/         for(inti=0;i<array.length;i++) {Number random= Round (Math.random () *100,2); Array[i]=Random; } number[] Array_a=Array; /** * Random order * If the initialized array is an orderly shape of the loop, it is necessary to disturb the composition of the array **/         for(inti = 0;  i < Array.Length; i++) {Number random= Math.Round (Math.random () * (c-1)); Number Temp=Array[i]; Array[i]=Array[random.intvalue ()]; Array[random.intvalue ()]=temp; } number[] Array_b=Array;        System.out.println (Out (array_a));        System.out.println (Getmidnum (array_a)); System.out.println ("**");        System.out.println (Out (array_b));            System.out.println (Getmidnum (array_b)); }            //Bubbling Algorithm     Public Staticnumber[] Sort (number[] array) { for(inti=0;i<array.length-1;i++){            //the start tag and the previous end tag are small details.             for(intj=i+1; j<array.length;j++) {Number temp; //you can control the positive or reverse order here .                if(Array[i].doublevalue () <Array[j].doublevalue ()) {Temp=Array[i]; Array[i]=Array[j]; ARRAY[J]=temp; }            }        }        returnArray; }                //Median algorithm     Public Static Doublegetmidnum (number[] array) {array=sort (array); //Java.util.Arrays.sort (array);                intc =Array.Length; Doubleret = c%2==0? (array[(C/2) -1].doublevalue () +array[c/2].doublevalue ())/2:ARRAY[C/2].doublevalue (); returnret; }        //output (array of sum & Members within array)     Public Staticstring out (number[] array) {string ret= ""; Doublesum = 0;  for(inti = 0; i < Array.Length; i++) {ret+=Array[i]; if(i!=99) {ret+=","; } Sum+=Array[i].floatvalue (); Sum= Round (sum,2); }                returnsum+ ":" +ret; }        //rounding, number of digits     Public Static DoubleRoundDoubleNumberintdigits) {BigDecimal B=NewBigDecimal (number); returnB.setscale (digits,bigdecimal.round_half_up). Doublevalue (); }        }

Java bubbling algorithm and median algorithm

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.