Array sorting, bubbling method, fast sorting method

Source: Internet
Author: User

1. Sort by bubbling

/** Bubble Sort: In the set of numbers to be sorted, the total number of the ranges that are not currently in sequence, the exchange of two numbers adjacent to the left and right pairs of adjacent two numbers **/     Public voidBubblesort (int[] num) {        inttemp = 0;  for(inti=0;i<num.length-1;i++){                     for(intj=0;j<num.length-1-i;j++){                            if(num[j]>num[j+1]) {temp=Num[j]; NUM[J]=num[j+1]; Num[j+1]=Temp }        }        }    }

Call:
int[] num = new int[] {2, 4, 5, 1, 6, 8};
Shuzu sz1 = new Shuzu ();
Sz1.bubblesort (num);
for (int i = 0; i < num.length; i++) {
System.out.println ("bubble method sort =" + Num[i] + "");
}

2. Quick Sort Method: The largest number on the last side

//Sort by: Find the largest number in sequence and then release     Public voidSort (int[] nums) {        intMaxpost, temp;//loop inside try not to define variables         for(intk = 0; K < nums.length-1; k++)//Number of executions{maxpost= 0;//assuming that the first number is the largest, the loop will find a large number once, so it is-K             for(inti = 1; i < nums.length-k; i++) {                                if(Nums[maxpost] <Nums[i]) {Maxpost=i; }} Temp= Nums[nums.length-k-1];//each time the exchange is the last numberNums[nums.length-k-1] =Nums[maxpost]; Nums[maxpost]=temp; }    }


Call:int[] num = new int[] {2, 4, 5, 1, 6, 8};
Shuzu sz = new Shuzu ();
Sz.selectsort (num);
for (int i = 0; i < num.length; i++)
{
System.out.println (Num[i] + "");
}

Array sorting, bubbling method, fast sorting method

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.