Java Array 3 (2015-8-27)

Source: Internet
Author: User

1.

Bubble sort: There are N number, to compare N-1 Wheel, the first round compares N-1 times. Each additional round, the number of times will be reduced one time, then the n-1-i; Select Sort method: Have N number, Comparison N-1 Times, the first round to find the minimum value, placed in the lowest subscript position, the second round to find the remaining minimum value, if the value is not in the subscript second small position, then put it in that position.

/*
Bubble sort (bubblesort) Explanation: Descending/Ascending. (compare two adjacent data)
Formula: Two-phase comparison, small front, outer circulation N-1 times, inner circulation n-1-i times.
*/
public class test3{
public static void Main (string[] args) {
--Define the number of groups
int[] arr = new int[]{7,2,10,9,45,3};
--Temporary variables
int temp = 0;
The outermost comparison is: the length of the array-1. n Number of wheels compared to N-1
for (int i=0;i<arr.length-1;i++) {
The number of comparisons per round is n-1-i; After the first round, the maximum value is found and the last, then the next comparison is less.
for (int j=0;j<arr.length-1-i;j++) {
--Comparison of two data
if (Arr[j]<arr[j+1]) {
int temp = Arr[j];
ARR[J] = arr[j+1];
ARR[J+1] = temp;
}
}
}
for (int i=0;i<arr.length;i++) {
System.out.print (arr[i]+ "");
}
}
}

Java Array 3 (2015-8-27)

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.