JAVA hobbled self-study the choice of array ordering bubble

Source: Internet
Author: User

1 Select Sort

Principle int[] arr = new int[] {2,4,21,44,10};

The first element of the array is compared to the other elements if the value of the element is smaller then the two values are compared so that the maximum value of the array is the maximum value on the 0 index of the array. Now we find out the first and second place how to find it, the reason is very simple, is to think of the remaining elements as a new array, and then repeat the above operation will find the second, and so on.

Process

Arr[0] Compare------arr[1] arr[1] Compare-------------arr[2] arr[2] Compare-----arr[3] arr[3] Compare arr[4]

Compare------arr[2] Compare-------------arr[3] Compare-----arr[4]

Compare------arr[3] Compare-------------arr[4]

Compare------Arr[4]

5 elements only need to compare 4 times, that is, n elements need to compare N-1 times can be, that is, the number of comparisons to the N-1 bit can be

Use a for to extract the left element value (Arr.length-1 is the last corner of the array)

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

{Arr[x]}

Use the second for to extract the elements on the right to participate in the comparison

for (int y=x+1;y<arr.length;y++)

{Arr[y])

If implementation comparison if (Arr[x]<arr[y])

We use ^ here to achieve the permutation of 2 elements

Arr[x]=arr[x]^arr[y]

Arr[y]=arr[x]^arr[y]

Arr[x]=arr[x]^arr[y]

We integrate code for (int x=0;x<arr.length-1;x++)

{for (int y=x+1;y<arr.length;y++)

{if (Arr[x]<arr[y])

{

                                                                                  arr[x]=arr[x]^arr[y] ;

                                                                                  Arr[y]=arr[x]^arr[y];

Arr[x]=arr[x]^arr[y];

}

}

}

2

Bubble sort

Principle int[] arr = new int[] {2,4,21,44,10};

Starting from the 0-point index of an array, each of the 2 adjacent elements is compared to the last value of the array so that the final maximum is seen in the end. Remove the last element to the left element right from the 0-point mark to start the next element to compare the most value to each other, the final value appears in the penultimate digit, and so on, completes the sort.

Step 1 using for to extract 22 adjacent elements

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

{

ARR[X] arr[x+1]

}

2 If implementation comparison if (Arr[x] <arr[x+1])

3 Realization Interchange arr[x] =arr[x]^arr[x+1];

ARR[X+1] =arr[x]^arr[x+1];

ARR[X] =arr[x]^arr[x+1];

4 How many times do you need to compare an array with a for

for (int w=0;w<arr.length;w++)

Integration for (int w=0;w<arr.length;w++)

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

{if (arr[x]<arr[x+1])

{

ARR[X] =arr[x]^arr[x+1];

ARR[X+1] =arr[x]^arr[x+1];

ARR[X] =arr[x]^arr[x+1];

}

}

}

Select the difference between sort and bubble sort the most value of the selection sort is the top value of the bubble sort that starts with the 0-point mark, which starts with the end-point mark.

JAVA hobbled self-study the choice of array ordering bubble

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.