JavaScript implements a common sorting algorithm

Source: Internet
Author: User

▓▓▓▓▓▓ approximate Introduction

  Due to the recent exam review, so the time to learn JS less-_-| |, The exam will continue to study hard, this time using native JavaScript to achieve the usual sorting algorithm used to learn, bubble sort, quick sort, Direct insertion sort, Hill sort, Direct Selection sort

▓▓▓▓▓▓ Interchange Sort

Exchange sorting is a kind of sorting process with the help of the exchange operation to complete the sorting method, the basic idea is 22 comparison of sorting records of the keyword, if two keywords are found in reverse order, the two record position is reversed, Repeat this process until all the keywords in the row sequence, followed by the exchange sort of the common Bubble sort and quick sort

▓▓▓▓▓▓ Bubble Sort

The name of the bubble sort is because the larger element is slowly "floating" through the interchange to the top of the sequence, so the name

The bubbling sort algorithm works as Follows: (from back to Forward)
1, Compare the adjacent Elements. If the first one is bigger than the second one, swap them both.
2, for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last Pair. At this point, the last element should be the maximum number.
3. Repeat the above steps for all elements except the last One.
4. Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to Compare.

The time complexity of the bubble sort is O (n^2)

Code:

1         //Bubble Sort2         functionbubblesort (arr) {3 4              for(vari=0;i<arr.length;i++){5                  for(varj=0;j<arr.length-i-1;j++){6                     if(arr[j] > Arr[j+1]){7                         varArrmax = arr[j+1];8arr[j+1] =arr[j];9arr[j] =arrmax;Ten                     } one                 } a             } -  -             returnarr; the}

▓▓▓▓▓▓ Quick SortQuick Sort (Quicksort) is an improvement to the bubbling sort. Quick Sort by C. A. R. Hoare was introduced in 1962. 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. a quick sort of algorithm is:1) set two variables i, j, at the beginning of the Order: i=0,j=n-1;2) with the first array element as the key data, assign the value to key, i.e. key=a[0];3) forward Search from j, that is, after starting the forward search (j--), find the first value less than key a[j], will a[j] and a[i] interchange;4) Backward Search from i, that is, start backward search (i++), find the first a[i] greater than key], interchange a[i] and a[j];5) repeat 3rd, 4, until i=j, (3,4 step, did not find the qualifying value, that is, 3 a[j] is not less than the key,4 a[i] is not larger than the time to change the value of j, i, so j=j-1,i=i+1, until Found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, i==j This process must be exactly when the i+ or J completes, at which time the loop ends). the time complexity of the quick sort is O (n*logn)code:
1         //Quick Sort2         functionquickSort (arr) {3             //End recursion Condition4             if(arr.length <= 1){5                 returnarr;6             }7 8             varleft = [];9             varright = [];Ten             varKey = Arr[0]; one  a             //Grouping -              for(vari=1;i<arr.length;i++){ -                 if(arr[i] <Key) { the Left.push (arr[i]); -}Else{ - Right.push (arr[i]); -                 } +             } -  +             //Recursive grouping a             returnquickSort (left). concat (key,quicksort (right)); at}

▓▓▓▓▓▓ Insert Sort

  The basic idea of inserting a sort is that each time a record to be sorted is inserted into the appropriate position in the previously sorted sequence by its keyword size until all the records have been inserted, the following two commonly used insertion sorting methods are introduced: Direct insert sort and Hill sort

▓▓▓▓▓▓ Direct Insert Sortthe practice of direct Insert sorting (straight insertion sort) is:each time the first element is taken out of the unordered table, it is inserted into the proper position of the ordered table so that the ordered table remains orderly. the first two numbers are compared, then the second number is inserted into the ordered table by size, and the second pass scans the third data with the first two numbers, inserts the third number into the ordered table by size, and then goes through the n-1 scan to complete the sorting process.   the time complexity of direct insert sorting is  code:
1         //Direct Insert Sort2         functioninsertsort (arr) {3             //The default first element is ordered4              for(vari=1;i<arr.length;i++){5 6                 if(arr[i] < Arr[i-1]){7                     varGuard =arr[i];8                     varj = I-1;9                     //expands an ordered array by one bitTenarr[i] =arr[j]; one  a                     //Traverse an ordered group to find your position -                      while(j >= 0 && Guard <Arr[j]) { -arr[j+1] =arr[j]; thej--; -                     } -  -                     //inserting the checked elements +arr[j+1] =guard; -                 } +             } a             returnarr; at}

▓▓▓▓▓▓ Hill Sort

The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill sort is a non-stable sorting algorithm. The method is due to Dl. The shell was named after it was introduced in 1959.

the Hill sort belongs to the insertion class ordering, which divides the entire ordered sequence into several small sub-sequences for insertion sorting. sorting process: First take a positive integer d1<n, put all the array elements separated by D1 in a group, the group is directly inserted into the sort, and then take d2<d1, repeat the grouping and sorting operations, until the di=1, that is, all records in a group to sort up. the time complexity of hill sequencing is time complexity o (n*logn)code:
1         //Hill Sort2         functionshellsort (arr) {3             varLen =arr.length;4             varSteplen = 1;5             //determining the stride length of the first traversal6              while(steplen < Len/3) {7Steplen = 3*steplen + 1;8             }9             //Start TraversalTen              while(steplen >= 1){ one                  for(vari=steplen;i<len;i++){ a                      for(varJ=i;j>=steplen && arr[j] < arr[j-steplen];j-=Steplen) { -Swap (arr,j,j-steplen); -                     } the                 } -Steplen = (stepLen-1)/3; -             } -  +             returnarr; -         } +  a         functionswap (arr,i,j) { at             vartemp =arr[j]; -arr[j] =arr[i]; -arr[i] =temp; -}

▓▓▓▓▓▓ Select Sort

 The basic idea of choosing a sort is: each order of n-i+1 the smallest record of a keyword in a sorted record, as the first record of an ordered sequence, until all records have been Sorted. Commonly used sorting methods have a direct choice of sorting and heap sorting (because JS can not be very good to reflect the characteristics of the heap sorting is not written)

  

▓▓▓▓▓▓ Direct Selection Sort

 The basic idea is: the first time from r[0]~r[n-1] to select the minimum value, and r[0] exchange, the second time from r[1]~r[n-1] to select the minimum value, and r[1] exchange, ...., I time from the r[i-1]~r[n-1] select the minimum value, and r[i-1] exchange, ... , the first n-1 from r[n-2]~r[n-1], with r[n-2], a total of n-1 times to get a sort code from small to large ordered sequence

Time complexity for direct selection of sorting: O (n^2)

Code:

1         //Direct Select Sort2         functionstraightselectsort (arr) {3             //The default first element is the smallest one4             varmin;5             vark;6             varLarr = [];7              for(vari=0;i<arr.length-1;i++){8Min =arr[i];9K =i;Ten                  for(varj=i+1;j<arr.length;j++){ one                     if(arr[j] <Min) { aMin =arr[j]; -K =j; -                     } the                 } -                 if(k! =I) { -arr[k] =arr[i]; -arr[i] =min; +                 } -             } +             returnarr; a}

JavaScript implements a common sorting algorithm

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.