Java bubble Sort and binary lookup implementation _java

Source: Internet
Author: User
Tags array length

Bubble Sort

Bubble sort (Bubble sort), see this algorithm, I think of a word "decimal floating, large number of sinks," through the comparison of layers to make the decimal surface, and the large number of "stone sink bottom." So as to achieve the effect of sorting. Bubble sort is a simple sort algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The algorithm is named because the smaller elements float slowly through the exchange to the top of the sequence.

The bubble sort algorithm works as follows:

1. Compare adjacent elements. If the first one is bigger than the second one, swap them both.

2. For each pair of adjacent elements to do the same work, from the beginning to the end of the first pair. At this point, the final element should be the largest number.

3. Repeat the above steps for all elements except the last one.

4. Continue to repeat the steps above for fewer elements at a time until no pair of digits need to be compared.

Process diagram of bubbling sort:

Instance Code

public class bubblesort{public

  static int[] Bubblesort (int[] array) {for
    (int i = 0;i < array.length;i++) {
   
    for (int j = 0; J < array.length-i-1;j++) {
        if (Array[j] > Array[j+1]) {            
          int temp = array[j]; 
          ARRAY[J] = array[j+1]; 
          ARRAY[J+1] = temp;  
        }
      }
      System.out.println ("First" + (i+1) + "trip Order");
      for (int k = 0;k < array.length;k++) {
        System.out.print (array[k]+ "");
      }
      System.out.println ();
    }
    return array;
  }


  /**
   * @param args
   *
  /public static void main (string[] args) {
    int[] array = {7,3,9,5,6,8,1};
    Bubblesort (array);
  }

}
   

Print results:

1th Trip Sort

3 7 5 6 8 1 9 2nd trip Sort 3 5 6 7 1 8 9 3rd trip Sort 3 5 6 1 7 8

4th trip Sort

9

3 5 1 6 7 8 5th Trip Sort

3 1 5 6 7 8 9 6th trip Sort 1 3 5 6 7 8 9

7th trip Sort

1 3 5 6 7 8

Two-point Search

In order, we also need to find the data we want, and the binary search is one of the commonly used, section-time, the basis of an algorithm. Two-point search is from the middle of the sorted data to find comparisons, similar to the middle of the stick to cut, so also called binary lookup, it is a more efficient way to find.

"Two-point search requirements": 1. Sequential storage structure must be used 2. Ordered by keyword size.

"Advantages and disadvantages" binary search method has the advantage of less number of comparisons, faster search speed, good average performance; its disadvantage is that the table of discovery is ordered, and the insertion deletion is difficult. Therefore, the binary lookup method is suitable for frequently ordered lists that are infrequently changed.

"Algorithmic thinking" First, compares the keywords in the middle position record of the table to the lookup key, and if the two are equal, the search succeeds; otherwise, the table is divided into the preceding and the last two child tables using the middle position record, and the previous child table is further searched if the key of the middle position record is greater than the lookup keyword Otherwise, the next child table is further searched.

Repeat the process until you find a record that satisfies the criteria, make the lookup successful, or until the child table does not exist, the lookup is unsuccessful.

"Algorithmic complexity" assumes that its array length is n, and its algorithm complexity o(log(n)), is the worst case of time complexity isO(n)。

Instance Code

Package Com.somnus.array; 
    /** * Binary Search method * @author COMPAQ * * * * * * * * * * */public class binarysearch{public static int BinarySearch (int[] array, int value) {
    int low = 0;
    int high = array.length-1;
    int middle = 0; while (low <= high) {middle = (Low+high)/2;//0 6 4 6 6 6 for (int i = 0;i < array.length;i++) {S
        Ystem.out.print (array[i]+ "");
        if (i = = middle)//3 5 6 follows the most intermediate point of the print separator {System.out.print ("# #");
      } System.out.println ();
      if (array[middle] = = value) {return middle;
      } if (value < Array[middle]) {high = middle-1;
      } if (Value > Array[middle]) {low = middle + 1;
  }} return-1;
    }/** * @param args */public static void main (string[] args) {int[] array = {7,3,9,5,6,8,1};

    int[] Array1 = bubblesort.bubblesort (array);
    int index = BinarySearch (array1,1);

  System.out.println ("Location:" +index); }

}

Print results:

1th Trip Sort 3 7 5 6 8 1 9 2nd trip Sort 3 5 6 7 1 8 9 3rd trip Sort 3 5 6 1 7 8 4th trip Sort 9 3 5 1 6 7 8

5th trip Sort 
   
    3 1 5 6 7 8 9 6th trip Sort 1 3 5 6 7 8 9

7th trip Sort 1 3 5 6 7 8 9 1 3 5 6

# # 7 8 9 1 *

# # 3 5-6 7 9
    1 # 3 5 6 7 8 9

Location: 0
   

Analysis and summary

In the lookup algorithm, the binary is the fastest, but it must be an ordered sequence. These are the basis of the algorithm, but also need us to experiment, to sum up, to absorb, adhere to the algorithm learning.

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.