Sorting (bubble, direct insertion, and quick sorting)

Source: Internet
Author: User

Bubble Method

1st: Compare 0 and 1, 1 and 2, 2 and 3 in sequence... n-2 and n-1 index at the elements, found that the front is greater than the back, exchange them, so a trip down, the largest element ranked to the end.

2nd: continue to follow the 1st practices. The second largest element comes to the end.

......

In this way, N pieces of data are sorted after N-1 comparison and exchange. If a trip is not exchanged, it indicates that the sorting has been completed and the sorting can be completed in advance.

Code

int size = arr.length;        for (int i = 0; i < size - 1; i++) {            for (int j = 0; j < size - i - 1; j++) {
                if arr[j] > arr[j + 1]) {                    int temp = arr[j];                    arr[j] = arr[j + 1];                    arr[j + 1] = temp;                }
         }

The best time complexity of Bubble SortingO (N );The worst time complexity of Bubble Sorting isO (n2 ).

In summary, the average time complexity of Bubble Sorting isO (n2 ).

Direct insertion

First: Compare the 2nd elements with the 1st elements, with the small ones at the front and the big ones at the back.

Second: take out the 3rd elements and insert them into the first two elements so that they can be sorted.

Third: take out the 4th elements and insert them into the first three elements so that they can be sorted.

......

N-1-1: Take the n-th element and insert it into the first n-1 element. The sorting is complete.

Code

Int size = arr. length; // start from the second, traverse each array element for (INT I = 1; I <size; I ++) {// retrieves int temp = arr [I]; // traverses from the back to the front and finds the insert position Int J; For (j = I-1; j> = 0 & temp <arr [J]; j --) {arr [J + 1] = arr [J];} // Since J -- is executed after the preceding loop is completed, arr [J + 1] is assigned the value of ARR [J + 1] = temp ;}

Direct insertion of sorting is a stable sorting, and the worst time complexity isO (n2 ),The space complexity isO (1 ).

Quick sorting

Quick sorting is an improvement of Bubble sorting.

Data to be sorted is divided into two independent parts by one sort. All the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.

The quick sorting algorithm is as follows:

1) set two variables I, j, sorting start: I = 0, j = N-1;

2) Take the first array element as the key data and assign it to the key, that is, key = A [0];

3) search forward from J, that is, search forward from the back (j --), find the first value less than the key a [J], assign a [J] to a [I];

4) Search backward from I, that is, search backward from the beginning (I ++), find the first a [I] greater than the key, assign a [I] to a [J];

5) Repeat steps 3rd and 4 until I = J; (in step 3 and 4, no matching value is found, that is, in step 3, a [J] is not smaller than the key, in 4, when a [I] is not greater than the key, the value of J and I is changed so that J = J-1, I = I + 1 until it is found. Find the value that meets the condition. When switching, the position of the J pointer remains unchanged. In addition, the process I = J must be exactly when I + or J-is completed, and the loop ends at this time ).

Code

Public class main {/*** @ Param pdata * the position on the left of the array to be sorted * @ Param left *. The initial value is 0 * @ Param right, the initial value is the array length */public static void quicksort (INT [] pdata, int left, int right) {int I, j; int first, temp; I = left; j = right; first = pdata [left]; // select another number here, but select the first one. // a Quick Sort while (true) {// start from the second number to find the number greater than the center, and start from the front to find the number greater than pdata [left] While (++ I) <right-1 & pdata [I] <first); // start from the last number and find the first number smaller than the central pdata [left] While (-- j)> left & pdata [J]> first); if (I> = J) break; // switch the number found on both sides temp = pdata [I]; pdata [I] = pdata [J]; pdata [J] = temp;} // exchange center pdata [left] = pdata [J]; pdata [J] = first; // If (left <j) quicksort (pdata, left, J); // If (Right> I) quicksort (pdata, I, right);} public static void main (string [] ARGs) {int [] pdata = new int [5]; for (INT I = 0; I <5; I ++) pdata [I] = (INT) (math. random () * 100); // produce 10 random integers for (INT I = 0; I <pdata. length; I ++) {system. out. print (pdata [I] + "");} Main. quicksort (pdata, 0, pdata. length); system. out. println ("\ n ***********************"); For (INT I = 0; I <pdata. length; I ++) {system. out. print (pdata [I] + "");}}}

The time complexity of quick sorting is worst caseO (n2)The average time complexity isO (N * lgn).

I am the dividing line of tiantiao

Sorting (bubble, direct insertion, 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.