Java uses bubble sort arrays to sort _java

Source: Internet
Author: User

The example in this article describes how Java uses a bubble sort array to sort the arrays. Share to everyone for your reference. Specifically as follows:

First, bubble sort:

Sort arrays by bubble sort

Ii. Basic Concepts:

In turn, compare the adjacent two numbers, place the decimal number in front, and the large numbers in the back. On the first trip: first compare the 1th and 2nd numbers, put the decimal number before the big number put. Then compare the 2nd and 3rd numbers, put the decimal places before the large number, so continue until the last two, the number of decimal places before the large number of put. At the end of the first trip, the largest number was put to the end. On the second trip: the comparison is still starting from the first logarithm (since the exchange of the 2nd and 3rd numbers is possible, so that the 1th number is no longer less than the 2nd number, the decimal place before, after the large number, has been compared to the penultimate number (the first in the last position is already the largest), the second end, To get a new maximum number (in fact, the second largest number in the whole series) in the penultimate position. So go on, repeat the process until you finish sorting.

Third, the realization of ideas:

With the double loop, the outer loop variable is set to I and the inner loop variable is set to J. If there are n number needs to be sorted, the outer loop repeats n-1 times, and the inner loop repeats n-1,n-2,...,1 times in turn. Each of the two elements to be compared is related to inner Loop j, which can be identified by a[j] and a[j+1 respectively, and the value of I is 1,2,..., n-1 in turn, and I,j,... 0,1,2 for each n-i value.

Set the length of the array to n:
1. Compare the adjacent two data and exchange two data if the preceding data is larger than the following data.
2. After the No. 0 data of the array is traversed by a N-1 data, the largest data is "sunk" to the N-1 position of the array.
3. N=n-1, if N is not 0, repeat the preceding two steps, or the sort completes.

Four, Java code implementation:

Package Arraydemo; 
/** 
 * @author pplsunny 
 * @category./ 
 Public 
class Arraydemo {   
  /** 
   * Sort results with enhanced for loop output 
   * * Public 
  static void Main (string[] args) { 
   int[] a = {2, 4, 
   a. Arraydemo.bubblesort (a); 
   for (int b:a) { 
    System.out.print (b + ""); 
   } 
  } 
  * 
   * bubble sort function, defined as static convenience,
 * is also a way to define the tool class in development * * Public 
  static void Bubblesort (int a[]) {for 
   ( int i = 1; i < a.length; i++) {
   //This is the 
    number of control trips for (int j = 0; J < A.length-i; J + +) {
    //j < A.length-i, compare elements of the Count 
     if (a[j) ; A[j + 1]) { 
      int temp = a[j]; 
      A[J] = a[j + 1]; 
      A[j + 1] = temp; 
     }}} 
 

Five, performance analysis:

If the initial state of the record sequence is "positive sequence", the bubble sort process needs only a sequence of n-1 comparisons and no movement of records in the sort process, whereas, if the initial state of the record sequence is in reverse order, N (n-1)/2 comparisons and record movement are required. So the total time complexity of the bubble sort is O (n*n).

Six, algorithm optimization:

The disadvantages of bubble sorting method and the improvement methods:
First, in the sequencing process, after performing the final sort, although the data has been sorted complete, but the program can not determine whether to complete the sort, in order to solve this problem, you can set a flag bit flag, set its initial value to not 0, indicating that the sorted table is a unordered table, Set the flag value to 0 before each sort start, modifying flag to 0 when data is exchanged. At the beginning of a new round of sorting, check this flag, if this flag is 0, indicates the last time no exchange data has been done, then the sorting;

 
/* Bubble sort function Improved 
/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.length-i; J +) { 
     if (A[j] > a[j + 1]) { 
      int temp = a[j]; 
      A[J] = a[j + 1]; 
      A[j + 1] = temp; 
      Flag = true; 
     } 
    } 
    if (!flag) break 
     ;//If no exchange occurs, exit the loop}} 

Second, in the bubble sort, a scan is likely to have no data exchange, there may be one or more data exchange, in the traditional bubble sorting algorithm and some of the improved algorithms in recent years, only one scan has data exchange information, the location of data exchange is not processed. In order to make full use of this information, a local bubble sort is called local bubble ordering for each reverse sequence data pair in a global scan. The local bubble sort has the same time complexity as the bubble sort algorithm, and in the case of positive and reverse order, the number of keywords required is exactly the same as the number of moves. Because the number of data moves for local bubble sorting and bubbling is always the same, the number of keywords required for local bubble sorting is often less than that of bubble ordering, which means that local bubble sorting is likely to improve the bubbling sort on average. While the advantages of fewer comparisons are insufficient to offset the extra overhead of the complexity of the program, the time performance of the local bubble sort is significantly better than the bubble sort when the data volume is large. For n unordered data, when we do a bubble sort, if the K data and the k+1 data are in reverse order, then a forward bubble sort of the k+1 data is moved to the appropriate position, which means that the front k+1 data is adjusted to a positive sequence. Because this bubble is only bubbling over the first k+1 data, we call it--local bubbling

I hope this article will help you with your Java programming.

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.