The example of this article tells the way Java uses bubbling sorting to sort the array. Share it for everyone for your reference. The specifics are as follows:
1. Bubble sorting:
Use bubbling sorting to sort the array
2. Basic concepts:
Two adjacent numbers are compared in turn, put the decimal front, and the large number is behind. That is, in the first trip: First compare the first and second numbers, put the decimal before, and large number. Then compare the second and third numbers, put the decimal before, and after the large number, continue, until the last two numbers, put the decimal before, and the large number is put. At this point, the largest number was set to the end. In the second trip: Starting from the first pair (because the second number of the second and the third number may be reduced, the first number is no longer less than the second number), put the decimal After putting it, it has been compared until the penultimate number (the first position is already the largest). At the end of the second trip, a new maximum number is obtained in the penultimate position (in fact, in the entire series Two major numbers). As a result, repeat the above process until the final order is completed.
Third, realize ideas:
For dual -loop implementation, the outer cycle variable is set to i, and the inner circulation variable is set to J. If there are n numbers that need to be sorted, the external cycle is repeated N-1 times, and the inner cycle repeats N-1, N-2, ..., 1 time. The two elements that compare each time are related to the inner cycle J. They can be identified with A [J] and a [J+1] separately, and the value of i is 1,2, ..., n-1, n-1 For the value of each I, J is 0,1,2, ... ni.
Let the array length n:
1. Compared with the two adjacent two data, if the previous data is greater than the later data, the two data will be exchanged.
2. In this way, the largest data is "sinking" to the n-1 position of the array after one traversal to the n-1 data.
3. N = n-1, if n is not 0, repeat the first two steps, otherwise the sort is completed.
4. Java code implementation:
Package ArrayDemo; / *** @AutHor Pplsunny* @Category. 21* / Public Class ArrayDemo { / *** Use enhanced for cyclic output sort results* / Public Static Void Main (String [String [String [String ] ARGS) {int [] a = {2, 4, 76, 12, 34, 23, 86}; ArrayDemo.bubblesort (a); for (int b: a) {system.out.print (b + "");} / * * bubble Sorting functions are defined as static and convenient use. * It is also a method of defining tools in development * / Public Static Void Bubblesort (int a []) {for (int i = 1; i <.length; i ++) { / / /This is the control number for (int j = 0; j <a.length -i; j ++) {// j <a.length -i, the number of comparative elements IF (a [j]> a [j+j+ 1]) {int Temp = a [j]; a [j] = a [j + 1]; a [j + 1] = test;}}}}
5. Performance analysis:
If the initial status of the record sequence is "positive sequence", the bubbling sorting process only needs to be sorted, and only N-1 comparison during the sorting process is performed without mobile records; For "reverse order", N (N-1)/2 comparisons and records need to be performed. Therefore, the total time complexity of bubbling sorting is O (n*n).
6. Optimization of algorithms:
The deficiency and improvement method of bubbling sorting method:
First, during the sorting process, after the final order is performed, although the data has been completely sorted, the program cannot determine whether to complete the sorting. In order to solve this deficiencies, you can set a logo position FLAG to set the initial value of its initial value to non -non -non -non -non -non -non -non -non -non -non -non -non -non -enough set value. 0, indicating that the sorted table is an disorderly table. Each sort is set to 0 before the start of the sort. When the data is exchanged, the FLAG is modified to the non -0. At the beginning of a new round of sorting, check this logo. If this sign is 0, it means that the last time has not been exchanged, and the sorting; otherwise the sorting;
/*** Bubble Sorting Function Improved Version*/ Public Static Void Bubblesort (int [] a) {Boolean Flag = TRUE; While (Flag) {Flag = False; for (int i = 0; I <A.Length -1 ; I ++) {for (int j = 0; j <a <a.Length -i; j ++) {if (a [j]> a [j+1]) {int Temp = a [j]; a [j] = a [j + 1]; a [j + 1] = test; flag = true;}} if (! Flag) break; // If no exchange occurs, the exit loop}}}}
Second, in the bubbling sorting, a scanning may be exchanged countless, or there may be once or multiple data exchange. In the traditional bubbling sorting algorithm and some improved algorithms in recent years, only one scan is recorded There are countless information information, and the location information of the data exchange is not processed. In order to make full use of this information, in a global scan, you can perform local bubbling sorting on each back -order data, which is called local bubbling sorting. Local bubbling sorting and bubbling sorting algorithms have the same time complexity, and under the condition of positive and inverse order, the number of keywords required for the number of keywords and movements is exactly the same. Because the number of data movement of local bubbling sorting and bubbling sorting is always the same, the number of keywords required for local bubbling sorting is often less than bubbling sorting, which means that local bubbling sorting is likely to compare the average number of times on average. The above improvement of bubbling sorting, when the advantages of less comparison are not enough to offset the additional expenses brought by the complexity of the program, and when the data volume is large, the time performance of the local bubbling sorting is significantly better than the counterfeit. Sort. For N disabled data, when we are in bubbling sorting, if the K data and K+1 data reverse order, then the k+1 data is sorted forward It moves to the right position, that is, let the previous K+1 data adjust into a positive order. Because this bubbling method is only bubbling for the previous K+1 data, we call it -local bubbling
It is hoped that this article is helpful to everyone's Java program design.