v8 - sort-source insertion sort method

v8 - sort of source method for shorter length of the array using insertion sort.

Section Source:

function InsertionSort(a, from, to) {
    for (var i = from + 1; i < to; i++) {
      var element = a[i];
      // Pre-convert the element to a string for comparison if we know
      // it will happen on each compare anyway.
      var key =
          (custom_compare || %_IsSmi(element)) ? element : ToString(element);
      // place element in a[from..i[
      // binary search
      var min = from;
      var max = i;
      // The search interval is a[min..max[
      while (min < max) {
        var mid = min + ((max - min) >> 1);
        var order = Compare(a[mid], key);
        if (order == 0) {
          min = max = mid;
          break;
        }
        if (order < 0) {
          min = mid + 1;
        } else {
          max = mid;
        }
      }
      // place element at position min==max.
      for (var j = i; j > min; j--) {
        a[j] = a[j - 1];
      }
      a[min] = element;
    }
  }

Source understanding:

Parameters: a-- array. form the starting point of an index that is 0. to the end of the index.

The length of the array source are calculated, such as value is undefined and may be moved to the end of the array length is subtracted.

Usually if a decent array is to a.length.

Insertion sort values ​​to be compared is compared with its previous value, so that the outer loop is started from the second value.

Began circulating the array, the initial value of the index is 1. Create a sentry, save the value of the current cycle corresponding to the index.

I.e. key = a [i], the value needs to be inserted into a location that loop array.

 

Creating max, record the number of cycles. I.e. max = i

Creating min, record insertion position. I.e., the initial value is 0 min = 0

An inner loop, the purpose to find the inserted location. Cycling conditions min <max

Within this cycle, the min and max create a saved value mid. Then calculates the size of the array of values ​​corresponding sentinel value. I.e., compare (arr [mid], key)

Note that the index is an array of mid min max.

 If the calculation result is 0, i.e., equal to the value of the index corresponding to the described value and the sentinel value.

So this is the position the insertion point. Assigned to the mid min, to obtain insertion point. Exit the loop

 If the calculated value is less than 0, the value of the index assigned to the mid + 1 min. I.e., min = mid + 1. Continue to cycle

If the calculated value is greater than 0, the value of the index assigned to mid max. Continue to cycle

 The loop continues after the end of the min = max. Get min value.

Then the current number of cycles has i as an initial value, save a copy j = i, in j> min as the determination condition.

The front turn an index value assigned to the current index value, such as a [3] = a [2], a [2] = a [1] 

Finally, the sentry assigned to the insertion point. I.e., a [min] = key

 As for the sort is descending liters. Determined by comparison function 

The simple comparison function: (x, y) => return x - y

A return parameter value obtained by subtracting 2 parameters. I.e. the value corresponding to the index value minus sentinel 

Index value corresponds to a value less than the sentinel, the calculated result is less than 0. According to the above logic, assigned to the mid + 1 min. Then an insertion point in the back of the index, it is ascending

The median value is greater than the index corresponding to sentinel, the calculation result is greater than 0. According to the above logic, assigned to the mid max. Then an insertion point in front of the index, or ascending

Diagram

 

 Code:

function insertSort (ARR) { 

  for (I = the let. 1; I <arr.length; I ++ ) { 
    the let Key = ARR [I] // Sentinel, save the current cycle index value corresponding to 

    the let min = 0 // interpolation position 
    let max = I // is the number of cycles 

    // array traverses the cycle to find the interpolation position 
    the while (min < max) {
       // value >> signed right shift is similar Math.floor (int / 2) int is a positive integer 
      let mid min + = ((max - min) >>. 1 )         
       // comparison function, passing the value corresponding to the index value and the Sentinel 
      const = Order Compare (a [MID], Key) 

      // if equal to 0 
      IF (Order == = 0 ) {
         //Assigned to the mid min. As the interpolation position, the loop is exited 
        min = max = mid
         BREAK ; 
      } 
      / * * 
       * has cycled array is an ordered array, the index value corresponding to the comparison Sentinel 
       * will be less than assigning mid min, and continued to take min max median values. Then the sentinel value in the magnitude comparing the corresponding value 
       * value will be greater than the value assigned to the max, the min and the new value taking the max, then comparing the value corresponding to the magnitude of the Sentinel 
       * cycle, until min = max, the loop is exited, locate the insertion point 
      * / 
      IF (order <0 ) { 
        min = MID +. 1 
      } the else { 
        max = MID 
      } 
    } 

    / * * 
     * insertion cycle, from the end has cycled array, sequentially the index value is assigned to the current index value 
     * such as: a [3] = a [ 2] a [2] = a [1], until the interpolation position min.
     * After the cycle, the sentinel assigned to ARR [min] 
     * 
    * / 
    for (the let J = I; J> min; J, ) { 
      ARR [J] = ARR [J -. 1 ] 
    } 
    ARR [min] = Key 
  } 
}

debugging:

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/caimuguodexiaohongmao/p/11863648.html