Java Bubble sorting and quick sorting analysis

Source: Internet
Author: User

Java Bubble sorting and quick sorting analysis

I am ashamed to say that I did not answer the Quick Sort pen questions during the interview yesterday. Java has long-term access to business-related things, especially java in the web direction. Over time, some of the most basic things learned in school have been forgotten. I searched for these two algorithms on the Internet, which are basically the explanations of books of the current year. I do not like them very much. Now I want to describe my enhanced explanation and strengthen my memory. Recently, I have been interviewed by some experts.

 

1. Bubble Sorting

The basic idea in books is to compare the elements on the left with those on the right one by one, so that if there are smaller elements, they can be exchanged.

I split this:

A. Place the minimum element of the array on the left;

B. perform recursion on the right side;

This is relatively simple and implements the code of:

 

// A. Place the minimum element of the array on the left side of the public static void bubbleBasic (int [] list) {// basic premise if (list! = Null & list. length> 1) {// compare the for (int I = 1; I from the second element
 
  
List [I]) {int tmp = list [0]; list [0] = list [I]; list [I] = tmp ;}}}}
 

 

A + B code:

 

// A + B. Complete Bubble Sorting public static void bubbleSort (int [] list, int begin) {if (list! = Null & (list. length-begin)> 1) {for (int I = begin + 1; I
 
  
List [I]) {int tmp = list [begin]; list [begin] = list [I]; list [I] = tmp ;}} bubbleSort (list, begin + 1 );}}
 

 

Of course, there is a simpler book code without recursion:

 

// Book Bubble Sorting public static void bubbleBook (int [] list) {if (list! = Null & list. length> 1) {for (int I = 0; I <list. length-1; I ++) {for (int j = I + 1; j <list. length; j ++) {if (list [I]> list [j]) {int tmp = list [I]; list [I] = list [j]; list [j] = tmp ;}}}}}

 

 

2. Quick sorting

The basic concept in books is the high and low position crossover comparison. The array is divided into two parts. The left side is smaller than A, and the right side is greater than.

I personally feel that this description is dizzy. In fact, it contains two parts: 1. Cross comparison; 2. Small on the left and big on the right. I shielded 1st points, fixed A as the first element, and decomposed the problem:

A. Place the first element of the array at a certain position so that the left side is smaller than it, and the right side is greater than it;

B. recursion of left and right;

For a, there is no restriction on the implementation method. I believe most people can do it. In fact, algorithms are designed to understand ideas, and there are many implementation methods. Of course, I also use cross comparisons here. I prefer to call left and right compression for comparison.

Code for implementing:

 

//. Place the first element of the array at a certain position so that the left side is smaller than it, and the right side is greater than it public static void quickBasic (int [] list) {// basic premise if (list! = Null & list. length> 1) {// the current position of the first element int I _current = 0; // you can obtain the left and right boundary int I _left = 0; int I _right = list. length-1; // if the right boundary is greater than the left boundary, it indicates that there is still a compressed space while (I _left <I _right) {// The right boundary is continuously compressed, until there is no space to compress or the while (I _current <I _right & list [I _current] <list [I _right]) {I _right --;} element is smaller than the first element --;} // exchange elements (even if no smaller elements are found, I _right will eventually be equal to I _current if it cannot be found all the time. In this case, it is the exchange between itself and itself, so the redundancy step is better) int tmp = list [I _current]; list [I _current] = list [I _right]; list [I _right] = tmp; // The Position of the first element has changed I _current = I _right; // ---------- the element is switched to the right and then compressed to the left. The process is the opposite. ---------- while (I _current> I _left & list [I _current]> list [I _left]) {I _left ++;} tmp = list [I _current]; list [I _current] = list [I _left]; list [I _left] = tmp; I _current = I _left; // ----------- return while repeat the above process ----------} // ----------- the program runs here and no space is compressed. The function is complete. At this time, I _left = I _current = I _right }}

 

A + B code:

 

// A + B. Complete quick sorting public static void quickSort (int [] arr, int leftIndex, int rightIndex) {if (arr! = Null & leftIndex <rightIndex) {// back up the initialization left and right boundary. int _ leftIndex = leftIndex; int _ rightIndex = rightIndex; int currentIndex = leftIndex; is used for recursion; while (leftIndex <rightIndex) {while (currentIndex <rightIndex & arr [currentIndex] <arr [rightIndex]) {rightIndex --;} int tmp = arr [currentIndex]; arr [currentIndex] = arr [rightIndex]; arr [rightIndex] = tmp; currentIndex = rightIndex; while (leftIndex <currentIndex & arr [leftIndex] <arr [currentIndex]) {leftIndex ++;} tmp = arr [currentIndex]; arr [currentIndex] = arr [leftIndex]; arr [leftIndex] = tmp; currentIndex = leftIndex ;} // recursive quickSort (arr, _ leftIndex, currentIndex-1); quickSort (arr, currentIndex + 1, _ rightIndex );}}

 

No recursive method is required for quick sorting.

 

Test example of the above method:

 

Public static void main (String [] args) {int [] list = new int [] {,}; bubbleBasic (list ); // The result is, 9, 1 (the print method is not written) list = new int }; bubbleSort (list, 0); // The result is, list = new int }; bubbleBook (list); // The result is, list = new int }; quickBasic (list); // The result is, list = new int [] {,}; quickSort (list, 0, list. length-1); // The result is 0, 1, 2, 3, 4, 5, 6, 8, 9}


 

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.