Advanced sorting algorithms

Source: Internet
Author: User

Hill sort

The hill sort is named after its creator, and the algorithm makes a great improvement on the basis of the insertion sort. The core concept of the Hill sort differs from the insertion sort, which first compares elements farther apart than adjacent elements. Using this scheme allows elements that are far from the correct location to return to the right place faster than simply comparing adjacent elements. When you start traversing a dataset with this algorithm, the distance between all the elements is continuously reduced until the end of the data set is processed, which is an algorithm that compares the adjacent elements.

function CArray (numelements) {    var dataStore = [];      for var i = 0; i < numelements; + +i)        {= i    ;     } return DataStore;} function SetData (Numelements,arr) {    forvar i = 0; i < numelements; + +i) {        = Math.floor (Math.random () * (numelements + 1));}    }
/** * Hill (Shell) sort is also known as narrowing the incremental sort, which is an insert sort. It is the enhanced version of the direct insert sorting algorithm * Contrast: Many data when Hill sort is more efficient than insert sorting, when the amount of data is less, insert sort is more efficient*/functionswap (arr, index1, index2) {vartemp =ARR[INDEX1]; ARR[INDEX1]=Arr[index2]; ARR[INDEX2]=temp;}functionShellsort (arr) {varN =arr.length; varH = 1; Console.time (' Hill sort time-consuming ');  while(H < N/4) {h= 4 * H + 1; }       while(H >= 1) {           for(vari = h; i < N; i++){               for(varj = i; J >= H && Arr[j] < arr[j-h]; J-=h) {Swap (arr, J, J-h); }} H= (h-1)/4;} console.timeend (' Hill sort time-consuming '); returnarr;}varNumelements = 10000; vararr =NewCArray (numelements);  SetData (Numelements,arr); Shellsort (arr);

10,000 digit sequencing is time consuming compared to the above insert sort 5~10ms

Quick Sort

Fast sorting is one of the fastest sorting algorithms for working with large data sets. It is a divide-and-conquer algorithm that recursively decomposes data into distinct sub-sequences that contain smaller and larger elements. The algorithm repeats this step until all the data is in order

The algorithm first selects an element in the list as the Datum value (pivot). Data sorting is done around the base value, moving elements that are less than the base value in the list to the bottom of the array, moving elements that are larger than the base value to the top of the array

/** * Quick Sort * Parse: * 1, in the dataset, select an element as the "datum" (pivot). * 2, all elements less than "datum" are moved to the left of "datum", all elements greater than "datum" are moved to the right of "datum". * 3, two subsets to the left and right of the "datum", repeating the first and second steps until only one element is left in all the subsets. * Focus on understanding: * until all subsets are left with only one y element * Contrast: * Quick Sort is suitable for large data sets, but performance degrades when processing small data sets. * Fast sorting is one of the fastest sorting algorithms for large datasets, which is a divide-and-conquer algorithm that recursively decomposes data into different sub-sequences that contain smaller and larger elements.*/functionQSort (list) {if(List.length = = 0){          return []; }      varLesser = []; varGreater = []; varPivot = list[0];  for(vari = 1; i < list.length; i++){          if(List[i] <pivot)          {Lesser.push (list[i]); }Else{Greater.push (list[i]); }      }      returnQSort (Lesser). Concat (Pivot, QSort (greater)); } varNumelements = 10000; vararr =NewCArray (numelements);  SetData (Numelements,arr); Console.time (' Quick sorting time '); QSort (arr); Console.timeend (' Quick sort time ')

Compared to the above Hill sort, 10,000 digital sequencing time-consuming 20~25ms, greater than the hill sort time-consuming, in the JS computable range, practice out, hill sort of efficiency than fast sorting high

Advanced sorting algorithms

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.