Java Sort:

Source: Internet
Author: User

* * Select sort

Principle: If the elements on the 0-point mark are compared to the elements in the following sequence,

At the end of the first inner loop, the minimum value appears in the 0-point mark position.

You just think about how we hit the stars.

****

***

**

*

public static void Selectsort (int[] arr)

{

i<arr.length-1 N Number 22 ratio, n-1 times can get the maximum value.

for (int i=0; i<arr.length-1; i++)

{

0 The point mark element is not necessary and itself, so that J starting from 1.

I found that J is always 1 bigger than I in order not to repeat the comparison, so J starts with I+1.

for (int j=i+1; j<arr.length; j + +)

{

if (Arr[j]<arr[i])

{

int temp = Arr[i];

Arr[i] = Arr[j];

ARR[J] = temp;

}

}

}

}

* * Bubble sort

Principle: Two adjacent elements are compared, the maximum value appears at the maximum angle mark after the first comparison.

public static void Bubblesort (int[] arr)

{

i<arr.length-1 N Number 22 ratio, n-1 times can get the maximum value.

for (int i=0; i<arr.length-1; i++)

{

In order to prevent the corner mark from crossing, J<arr.length-1

To reduce the number of comparisons, j<arr.length-1-i

for (int j=0; j<arr.length-1-i; j + +)

{

if (arr[j]>arr[j+1])

{

int temp = Arr[j];

ARR[J] = arr[j+1];

ARR[J+1] = temp;

}

}

}

}

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