Analysis of Java bubbling sorting and quick sorting

Source: Internet
Author: User

Ashamed to say, yesterday when the interview encountered a quick sort of pen questions did not answer. The long-term contact with Java is the business aspect, especially the web direction of Java, over time in the school to learn the most basic things to forget. Online search for these two algorithms, the basic is the interpretation of the book, the individual is not very like. Now the self-reinforcing interpretation of the explanation, strengthen memory, recently interviewed comrades may wish to look at.

1. Bubble sort

The basic idea in the book is that the left element is contrasted with the right element, and the smaller one is exchanged.

I'll break this down into:

A. Place the smallest element of the array on the left side;

B. Recursive to the right;

This is simpler, implementing A's code:

A. Place the smallest element of the array on the left public static void Bubblebasic (int[] list) {//Basic premises if (list! = null && list.length > 1) {//from second Elements start comparing for (int i=1;i<list.length;i++) {//Find smaller replace if (list[0] > List[i]) {int tmp = list[0];list[0] = list[i];list[i ] = tmp;}}}

A+b's Code:

A+b. Full bubbling sort public static void Bubblesort (int[]list, int begin) {if (list = null && (list.length-begin) > 1 {for (int i=begin+1;i<list.length;i++) {if (List[begin] > List[i]) {int tmp = List[begin];list[begin] = List[i];lis T[i] = tmp;}} Bubblesort (list, begin + 1);}}

There is, of course, a simpler book code that does not have a recursive approach:

The bubble sort of the book 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] = t MP;}}}}


2. Quick Sort

The basic idea in the book is a high-low cross-contrast, dividing the array into two parts, the left side being less than an element a, and the right greater than a.

Personal feeling this description is very faint, actually consists of two parts, 1. Cross contrast, 2. The left side is small and the right side is big. I block out the 1th, fix A to the first element, and break the problem down into:

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

B. To the left and right to do recursion;

For a, do not limit the way of implementation, I believe most people can do. In fact, the algorithm is to understand the idea, the realization of the way is a lot of, of course, I am also here with cross-contrast to achieve, I prefer to call the right and left compression contrast.

Implement the code for a:

A. Place the first element of the array in a position so that the left side is less than it, and the right is greater than it public static void QuickBasic (int[] list) {//Basic premises if (list! = null && List.leng Th > 1) {///The first element current position int i_current = 0;//get left and right boundary int i_left = 0;int I_right = list.length-1;//As long as it is greater than the left boundary, there is also a compressed space whil E (I_left < i_right) {//compress the right boundary continuously until there is no compressed space or there is an element smaller than the first element while (I_current < i_right && List[i_current] < List[i_right]) {i_right--;} Swap elements (even if no smaller element is found to exchange, because there is no way to find the words i_right will eventually be equal to I_current, this is the self exchange with itself, when the redundancy step is ok) 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;//----------element is switched to the right after the start of compression on the left, Exactly the opposite of the above----------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 above procedure----------}//-----------program run to here, there is no compressed space, function complete, this time I_left = I_current = I_right}}

A+b's Code:

A+b. Complete quick sort public static void QuickSort (int[] arr, int leftindex, int rightindex) {if (arr! = null && Leftindex & Lt Rightindex) {//Back up the left and right boundaries of the initialization, use int _leftindex = Leftindex;int _rightindex = Rightindex;int Currentindex = leftindex;w in recursion Hile (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;} Start around recursive quickSort (arr, _leftindex, currentIndex-1); QuickSort (arr, currentindex+1, _rightindex);}}

Quick sort does not find a way to implement without recursion at the moment.


Test examples of the above methods:

public static void Main (string[] args) {int[] list = new Int[]{2,1,4,5,8,7,6,3,9,0};bubblebasic (list);//result is 0, 2,4,5,8,7,6,3,9,1 (printing method will not write) List = new Int[]{2,1,4,5,8,7,6,3,9,0};bubblesort (list, 0);//result is 0,1,2,3,4,5,6,7,8,9list = New Int[]{2,1,4,5,8,7,6,3,9,0};bubblebook (list);//The result is 0,1,2,3,4,5,6,7,8,9list = new int[]{2,1,4,5,8,7,6,3,9,0}; QuickBasic (list);//The result is 0,1,2,5,8,7,6,3,9,4list = new Int[]{2,1,4,5,8,7,6,3,9,0};quicksort (list, 0, list.length-1);// The result is 0,1,2,3,4,5,6,7,8,9}


Analysis of Java bubbling sorting and quick sorting

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.