Classic sort of bubble sort and quick sort

Source: Internet
Author: User

First, bubble sort

Bubble sort (Bubble sort) is a relatively classic sorting algorithm.

This is called "bubble sort" because the larger (descending) or smaller (ascending) adjacent elements in the sort are gradually "floated" to the front by swapping, like bubbles in the water, so they are visually named "bubble sort".

The bubble sort is primarily compared to two adjacent elements.

In terms of time complexity, if the initial state of the data set is positive, a single scan can be done in order. At this point, the number of comparisons and movements in the sort reaches the minimum, with the time Complexity O (N).

If the initial state of the data set is reversed, a n-1 order is required, and a comparison of n-i keywords is performed for each order (1≤i≤n-1). In this case, the comparison and the number of moves have reached the maximum, with a time complexity of O (N2).

Overall, the average time complexity of the bubble sort is O (N2).

We then look at the bubble sort by code.

Import Java.util.Arrays;

/*** Not optimized for bubble sorting*/public class Demo1 {public static void Main (string[] args) {Int[] Arr=new int[]{15,32,14,86,54,78,36};SYSTEM.OUT.PRINTLN ("Array before sorting:" +arrays.tostring (arr));System.out.println ("In order:");int k=0;for (int j=0;j<arr.length-1;j++) {for (int i=0;i<arr.length-1;i++) {if (Arr[i]>arr[i+1]) {int temp=arr[i];ARR[I]=ARR[I+1];Arr[i+1]=temp;}System.out.println ("First" + (++k) + "Times:" +arrays.tostring (arr));}}System.out.println ("sorted array:" +arrays.tostring (arr));}}

The result of the above code operation is as follows:

Array before sorting: [15, 32, 14, 86, 54, 78, 36]

Sort in:

1th time: [15, 32, 14, 86, 54, 78, 36]

2nd time: [15, 14, 32, 86, 54, 78, 36]

3rd time: [15, 14, 32, 86, 54, 78, 36]

4th time: [15, 14, 32, 54, 86, 78, 36]

5th time: [15, 14, 32, 54, 78, 86, 36]

6th time: [15, 14, 32, 54, 78, 36, 86]

7th time: [14, 15, 32, 54, 78, 36, 86]

8th time: [14, 15, 32, 54, 78, 36, 86]

9th time: [14, 15, 32, 54, 78, 36, 86]

10th time: [14, 15, 32, 54, 78, 36, 86]

11th time: [14, 15, 32, 54, 36, 78, 86]

12th time: [14, 15, 32, 54, 36, 78, 86]

13th time: [14, 15, 32, 54, 36, 78, 86]

14th time: [14, 15, 32, 54, 36, 78, 86]

15th time: [14, 15, 32, 54, 36, 78, 86]

16th time: [14, 15, 32, 36, 54, 78, 86]

17th time: [14, 15, 32, 36, 54, 78, 86]

18th time: [14, 15, 32, 36, 54, 78, 86]

19th time: [14, 15, 32, 36, 54, 78, 86]

20th time: [14, 15, 32, 36, 54, 78, 86]

21st Time: [14, 15, 32, 36, 54, 78, 86]

22nd time: [14, 15, 32, 36, 54, 78, 86]

23rd time: [14, 15, 32, 36, 54, 78, 86]

24th time: [14, 15, 32, 36, 54, 78, 86]

25th Time: [14, 15, 32, 36, 54, 78, 86]

26th time: [14, 15, 32, 36, 54, 78, 86]

27th time: [14, 15, 32, 36, 54, 78, 86]

28th time: [14, 15, 32, 36, 54, 78, 86]

29th time: [14, 15, 32, 36, 54, 78, 86]

30th time: [14, 15, 32, 36, 54, 78, 86]

31st Time: [14, 15, 32, 36, 54, 78, 86]

32nd time: [14, 15, 32, 36, 54, 78, 86]

33rd time: [14, 15, 32, 36, 54, 78, 86]

34th time: [14, 15, 32, 36, 54, 78, 86]

35th time: [14, 15, 32, 36, 54, 78, 86]

36th time: [14, 15, 32, 36, 54, 78, 86]

Sorted array: [14, 15, 32, 36, 54, 78, 86]

The above results are not optimized and the optimized code is as follows:

Import Java.util.Arrays;

public class Demo2 {static int k=0;static void Bublesort (int[] arr) {Boolean boo=false;//determines if there is a sort occurrence, if it does not occur, indicates that the order has been sorted and avoids repeated sortingfor (int i=0;i<arr.length-1;i++) {for (int j=0;j<arr.length-1;j++) {if (Arr[j]>arr[j+1]) {Boo=true;int TEMP=ARR[J];ARR[J]=ARR[J+1];Arr[j+1]=temp;System.out.println ("First" + (++k) + "Times:" +arrays.tostring (arr));}}if (!boo) {Break}}}public static void Main (string[] args) {Int[] Arr=new int[]{15,32,14,86,54,78,36};SYSTEM.OUT.PRINTLN ("Array before sorting:" +arrays.tostring (arr));Bublesort (arr);System.out.println ("sorted array:" +arrays.tostring (arr));}}

The result of the above code operation is as follows:

Array before sorting: [15, 32, 14, 86, 54, 78, 36] 1th time: [15, 14, 32, 86, 54, 78, 36] 2nd time: [15, 14, 32, 54, 86, 78, 36] 3rd times: [15, 14, 32, 54, 78, 86, 36] 4th time: [15, 14, 32, 54, 78, 36, 86] 5th time: [14, 15, 32, 54, 78, 36, 86] 6th times: [14, 15, 32, 54, 36, 78, 86] 7th times: [14, 1 5, 32, 36, 54, 78, 86] sorted array: [14, 15, 32, 36, 54, 78, 86]

It can be seen that the optimized code is 29 times less repetitive than the original (36-7=29).

Second, quick sort

Quick Sort (Quicksort) is an improvement to the bubbling sort.

Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

We also use the above-sorted datasets to demonstrate quick sorting.

/** * Quick Sort * @author Li Zhangyong */import java.util.arrays;public class Demo1 {static int k=0;public static void QuickSort (int[] arr,int _left,int _right) {if (_left>_right) {Return}int left=_left;int right=_right;int Temp=arr[left];while (Left!=right) {while (Right>left && arr[right]>=temp) {right--;}Arr[left]=arr[right];while (Left<right && arr[left]<=temp) {left++;}Arr[right]=arr[left];}//Arr[right]=temp;arr[left]=temp;//at this time, left and right are equalSystem.out.println ("First" + (++k) + "Times:" +arrays.tostring (arr));QuickSort (arr,_left,left-1);QuickSort (Arr,left+1,_right);}public static void Main (string[] args) {Int[] Arr=new int[]{15,32,14,86,54,78,36};SYSTEM.OUT.PRINTLN ("Array before sorting:" +arrays.tostring (arr));QuickSort (arr, 0, arr.length-1);System.out.println ("sorted array:" +arrays.tostring (arr));}After the above code is run, the result is as follows:
Array before sorting: [15, 32, 14, 86, 54, 78, 36] 1th time: [14, 15, 32, 86, 54, 78, 36] 2nd time: [14, 15, 32, 86, 54, 78, 36] 3rd time: [14, 15, 32, 86 , 54, 78, 36] 4th time: [14, 15, 32, 36, 54, 78, 86] 5th time: [14, 15, 32, 36, 54, 78, 86] 6th times: [14, 15, 32, 36, 54, 78, 86] 7th times: [14, 15, 32, 36, 54, 78, 86] sorted array: [14, 15, 32, 36, 54, 78, 86]

Finally, in terms of time complexity, the worst condition for fast sequencing is O (N2), with an average time complexity of O (Nlogn).

Classic sort of bubble sort and quick sort

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.