Source: Internet
Author: User
Tags benchmark

Easy to review and consult, special record.

1. Bubble sort (Ascending)

Idea: Each round will be two consecutive number comparison size, if the latter is greater than the previous one, to exchange, each round of bubbling can find a maximum value;

Then, the comparison of the number of rounds is length-1-i, explained as follows: Each round produces the largest is fixed, so to subtract the already ordered number, 1 means, I only loop to the last one, and the latter to compare the line

`1     Public Static voidBubblesort (int[] array) {//Bubble Sort2         inttemp;3          for(inti = 0; i < Array.Length; i++) {//Number of wheels4              for(intj = 0; J < array.length-i-1; J + +) {//number of comparisons per round5                 if(Array[j] > array[j + 1]) {6temp = array[j + 1];7Array[j + 1] =Array[j];8ARRAY[J] =temp;9                 }Ten             } One         } A}`

2. Fast sorting algorithm (recursive)

Idea: Find a benchmark value, generally with the first value as key, according to the size of the key, the elements to be sorted into two groups, the number on the left is less than key, the middle is key, the value on the right is greater than key;

Use the left and right array subscript to find the i,j, first find the first value less than key, the value of I override, and then i++, find the first value greater than key, the J corresponding value is overwritten, and finally find the value of the reference value, the key is placed. Then recursion.

`     Public Static voidQucikquery (int[] Array,intLeftintright) {//Fast-queue recursive implementation        intkey = Array[left];//used to preserve the first value;        inti =Left ; intj =Right ; if(Left >=Right ) {            return; }         while(I <j) { while(I < J && Array[j] >key) J--; if(I < J) {//find the first value greater than key to swapArray[i] =Array[j]; }             while(I < J && Array[i] <key) I++; if(I <j) {Array[j]=Array[i]; }} Array[i]=key; Qucikquery (array, left, I-1); Qucikquery (Array, I+ 1, right); }`

Another quick sort

` Public Static voidQuickSort (int[] A,intLeftintRight ) {        if(Left >Right )return; intPivot = A[left];//define a Datum value the first number of an array        inti =Left ; intj =Right ;  while(I <j) { while(Pivot <= A[j] && i < J)//find a number smaller than the base value from right to leftj--;  while(Pivot >= A[i] && i < J)//find a number larger than the base value from left to righti++; if(I < J)//if i<j, swap them            {                inttemp =A[i]; A[i]=A[j]; A[J]=temp; }} A[left]=A[i]; A[i]= pivot;//Put the benchmark value in the right placeQuickSort (A, left, i-1);//quickly sort the sub-array on the leftQuickSort (A, i + 1, right);//quickly sort the sub-array on the right}`

Follow-up, will continue to supplement.

Related Keywords:

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.