Java Basics-Array sorting (select sort and bubble sort), summarize brilliantly!!!

Source: Internet
Author: User

1 、-------Message

For beginners of Java students, the sort is in a strange but a basic problem. But because I have just stepped into the world of learning Java, a lot of things can not be well understood, resulting in their own can be knocked out of code or roughly to understand,

But the idea of solving the problem is not very well in depth. (In fact, the beginning of learning is not necessary to be too inquisitive, until they have a certain foundation, there is time to study again.) It feels a little better to have a research attitude. In fact, most of the current

Developers are after a period of training, in the course of training, the curriculum is particularly fast, sometimes we have to "informal", master training in the mainstream knowledge is the wise. If you are training, please remember to catch the learning thread. )

2 、-------Analysis + code

There are two main types of array sorting, one is the selection sort and the other is bubble sort. Of course, the university learned the data structure of the know, there are some other sort, here does not explain, have time to check online. In fact, the sort is not commonly used in development,

We learn it is to learn a thought, the future business logic may be used, there may be similar logic or cultivate our thinking, we can extrapolate in the future. Do not for the topic and the topic, said some digression, sorry, the matter.

(1) Select Sort (from small to large)

1) Thought: Select Sort, let the array of each number, followed by the number of comparison, if the previous number is greater than the number of the subsequent, the position of the exchange. Some people may not understand this argument. In other words, choose Sort: The first number is followed by

After the first comparison, the smallest number is at the front.

Do not understand the picture should be almost, really do not understand and understand the people to discuss the discussion.

2) Code

Import Java.util.Arrays;
/**
* Practice Sort-Select sort
* @author Administrator
*
*/
public class Dome2 {
public static void Main (string[] args) {
Array
Int[] arr = {5,3,7,2,6,7,6,5,4,1,9,8};
The first cycle is the number of comparisons that determine the number of successive and subsequent numbers.
for (int i = 0; i < arr.length-1; i++) {
This is the number compared to the first number.
for (int j = i+1; J < Arr.length; J + +) {
Defines a temporary variable that is used to swap variables
int temp;
if (Arr[i]>arr[j]) {
temp = Arr[i];
Arr[i] = Arr[j];
ARR[J] = temp;
}
}
}
Print the final sort result
System.out.println (arrays.tostring (arr));
}
}

(2) Bubble sort (small to large)

1) thought: Two adjacent numbers to compare, the first wave comparison, the largest number in the last. (after each comparison, the subsequent number decreases by one comparison)

2) Code

Import Java.util.Arrays;
/**
* Practice sort--bubble sort
* @author Administrator
*
*/
public class Dome2 {
public static void Main (string[] args) {
Array
Int[] arr = {5,3,7,2,6,7,6,5,4,1,9,8};
External loops
for (int i=0;i<arr.length-1;i++) {
The index of the two-digit connection is the use of an internal loop
for (int index=0;index<arr.length-1;index++) {
Also using intermediate variables, note the distinction between the selection and sorting
if (Arr[index]>arr[index+1]) {
int Temp=arr[index];
ARR[INDEX]=ARR[INDEX+1];
Arr[index+1]=temp;
}
}
}
System.out.println (arrays.tostring (arr));
}
}

Java Basics-Array sorting (select sort and bubble sort), summarize brilliantly!!!

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.