Java algorithm--sort

Source: Internet
Author: User

1. Bubble sort

Central idea: In order to sort a group of numbers, the current is not yet ranked in the range of all the number, the top-down to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward.

1 public static int[] Bubblesort (int[] numbers) {2     int temp, 3     int size = numbers.length; 4 for     ( int i = 0; i < size-1;  i++) {5 for         (int j = 0; J < Size-i-1; J + +) {6             if (Numbers[j] > numbers[j+1]) {7 temp =  NUMBERS[J]; 8 Numbers[j] = numbers[j+1]; 9 numbers[j+1] = }13 return numbers;14}       /c7>  

2. Select sort

Central idea: In the set of numbers to be sorted, select the smallest number to exchange the number of the first position, and then in the remaining number to find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison.

1 public static int[] Selectsort (int[] numbers) {2     int temp; 3     int k; 4     int size = Numbers.len Gth 5 for     (int i = 0; i < size-1; i++) {6         k=I, 7 for (int j = i + 1; j < size; J + +) {8 if (number S[k] > numbers[j]) {9 k= }12 temp=numbers[k];13 numbers[k]=numbers[i];14 numbers[i]= } ret Urn numbers;17}            

Java algorithm--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.