JAVA programming example-bubble Algorithm

Source: Internet
Author: User

I. instance description

This example uses Java to implement a very simple Sorting Algorithm ------ bubble algorithm. Sorting algorithms are often used in software programming of any scale. For example, in desktop tools and management information systems, in addition, sorting algorithms are widely used in industrial statistics and scientific computing. Through the programming examples introduced in this section, you can deepen your understanding of the Java language basics described in this chapter and consolidate your knowledge.

Ii. Basic knowledge points

1. Basic Idea of Bubble Sorting

The idea of Bubble Sorting is very simple. Suppose there are n numbers in a series, and we need to sort them in ascending order. First, compare the first number with the second number. If the first number is greater than the second number, the two numbers are exchanged, then the second and third numbers are compared, and so on, until the number N-1 is compared with the number n. The above process is called the first Bubble sorting, so the nth number at the end of the first trip is the largest number in all the series, just like bubbles in the water, the maximum number will also "float" forward ". Then perform the second sort and perform the same operation on the first n-1 number. The result is that the second number is placed at the n-1 number. An example of Bubble Sorting is as follows:

Initial time: 49 38 65 97 76 13 27 49

After the first round of sorting: 38 49 65 76 13 27 49 97

After the second round of sorting: 38 49 65 13 27 49 76 97

After the third round of sorting: 38 49 13 27 49 65 76 97

After sorting the fourth round: 38 13 27 49 65 76 97

After the fifth round of sorting: 13 27 38 49 65 76 97

After the sixth round of sorting: 13 27 38 49 65 76 97

After the seventh round of sorting: 13 27 38 49 65 76 97

From the above, we can see that a maximum number can be selected for each batch of Bubble sorting at the end. In this way, if n is to be sorted, it is enough to sort n-1 at most. Careful readers will find that the sixth sorting has not exchanged any numbers, and the entire series is arranged in ascending order, that is, the seventh sorting is redundant. So how can we determine in the program when the sorting can end? Interested readers can use the knowledge learned in this chapter to further improve the bubble algorithm.

2. for Loop Structure

The for loop structure needs to be used in the sorting algorithm. It is necessary to consolidate it here:

For (initexpr1; testexpr2; incrementexpr3)

{

Statements;

}

3. if judgment statement

If/else branch structure:

If (boolean)

{

Statements;

}

Else

{

Statements;

}

Iii. Technical Points

1. Java Array

The array element is equivalent to a variable. In this section, you need to use an array to record the sorting of the previous series and the sorted series. An integer array with 8 elements is defined in the following format:

Int a [] = new int [8];

2. value exchange

Numerical exchange is very common in programming, and it is hoped that beginners can master it. If a and B are required for exchange, a temporary variable c must be generated to act as an intermediary and store the value of. The code for the switch part is as follows:

C =;

A = B;

B = C;

3. Loop

Use the for loop statement to compare the size of each trip.

For (INT I = 0; I <= 7; I ++)

{

 

}

IV. Implementation steps

Create a new class named sort. Java and type the following code in the sort. Java file:

Public class sort {

Public sort (){

}

 

Public static void main (string [] ARGs ){

Sort sort1 = new sort ();

Int A [] = new int [8];

A [0] = 49;

A [1] = 38;

A [2] = 65;

A [3] = 97;

A [4] = 76;

A [5] = 13;

A [6] = 27;

A [7] = 49;

Int tmp; // defines temporary variables as registers

For (int I = 0; I <= 7; I ++ ){

For (int j = 0; j <7-I; j ++) {// sort each row below

If (a [j]> a [j + 1]) {// compare the size, swap

Tmp = a [j + 1];

A [j + 1] = a [j];

A [j] = tmp;

}

}

}

// Print the comparison result

For (int I = 0; I <= 7; I ++ ){

System. out. print (a [I] + ";");

}

}

}

V. Summary

The above example mainly demonstrates the basic knowledge of Java, and the Sorting Algorithm itself is not the focus. However, whether it is the basic usage of variable assignment, or the flexible use of control statements such as for and if, the basic structure of Java programs is the basic knowledge necessary for programming. You must have a good understanding of it.

 

Related Article

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.