Summary of Six Common Algorithms of JavaScript Array Sorting

  • 2021-08-06 20:38:19
  • OfStack

Preface

If you are in a hurry, just choose the first two, and just look at the back.

In the development, the demand of array sorting is very frequent. This article will introduce several common sorting ideas.

1. Hill sort (best performance)

If you want to arrange from large to small, while (arr [n] > arr[n - interval] & & n > 0).


//  Hill sorting algorithm 
function xier(arr){
 var interval = parseInt(arr.length / 2);// Grouping interval setting 
 while(interval > 0){
  for(var i = 0 ; i < arr.length ; i ++){
   var n = i;
   while(arr[n] < arr[n - interval] && n > 0){
    var temp = arr[n];
    arr[n] = arr[n - interval];
    arr[n - interval] = temp;
    n = n - interval;
   }
  }
  interval = parseInt(interval / 2);
 }
 return arr;
}

// Array
var arr = [10, 20, 40, 60, 60, 0, 30]

//  Print the sorted array 
console.log(xier(arr))//[0, 10, 20, 30, 40, 60, 60]

2. sort sort (normal arrays/array nested objects)

1 heap array sorting


// Array
var arr = [10, 20, 40, 60, 60, 0, 30]

//  Sorting method 
arr.sort(function(a,b){

 /*
 * return b-a;  - >  Descending sort 
 * return a-b;  - >  Ascending arrangement 
 */
 return a-b;
})// If callback functions are not written in parentheses, they are arranged in ascending alphabetical order by default 

//  Print the sorted array 
console.log(arr)//[0, 10, 20, 30, 40, 60, 60]

Object array sorting (array set objects)


// Object array sorting 
var arr = [
 {name:'syy', age:0},
 {name:'wxy', age:18},
 {name:'slj', age:8},
 {name:'wj', age:20}
];

//  Sorting method 
function compare(property) {//property: According to what attribute 
 return function(a,b){
  var value1 = a[property];
  var value2 = b[property];
  /*
  * value2 - value1;  - >  Descending order 
  * value1 - value2;  - >  Ascending order 
  */
  return value1 - value2;// Ascending sort 
 }
}

//  Print the sorted array 
console.log(arr.sort(compare('age')))
/*
0: {name: "syy", age: 0}
1: {name: "slj", age: 8}
2: {name: "wxy", age: 18}
3: {name: "wj", age: 20}
*/

3. Barrel sorting

Features: Simple, but very wasteful of memory, almost unnecessary.

Array elements that appear in the bucket are marked with 1, and then the elements marked with 1 in the bucket array are printed in turn.


// Array
var arr = []

//  Marking each array item (1)
for(let i = 0; i < arr.length; i++) {
 let key = arr[i]
 arr[key] = 1
}

//  Traverse and print out each item 
for(let j in arr) {
 debugger
 console.log(j)
}

4. Bubble sort

Performance: 1-like (each item needs to be compared).

Find the maximum value every 1 trip.


// Array
var arr = [10, 20, 40, 60, 60, 0, 30]

/*
*  The total number of comparisons is arr.length-1 Times 
*  The number of comparisons each time is arr.length-1 Times 
*  Decrease in turn 
*/
var temp;// Exchange variable identification 

//  Two floors for Represents the current item and the 2 Items 
for(let i = 0; i < arr.length - 1; i++) {
 for(let j = 0; j < arr.length - 1; j++) {

  //  If the current item is greater than the 2 Items ( Posterior 1 Items ) Then exchange 
  if(arr[j] > arr[j+1]) {
   temp = arr[j]
   arr[j] = arr[j+1];
   arr[j+1] = temp;
  }
 }
}

//  Print the sorted array 
console.log(arr)//[0, 10, 20, 30, 40, 60, 60]

Step 5 Select a sort

Performance: 1-like (each item needs to be compared).

Assume that the value at a certain position is the minimum value, which is similar to bubble sorting.


// Array
var arr = [10, 20, 40, 60, 60, 0, 30]


var temp;// Exchange variable identification 

//  Two floors for Represents the current item and the 2 Items 
for(let i = 0; i < arr.length - 1; i++) {
 for(let j = i + 1; j < arr.length; j++) {

  //  Hypothetical number 2 Items are minimum values ( Yes, then exchange / Otherwise continue to compare )
  if(arr[i] > arr[j]) {
   temp = arr[i];
   arr[i] = arr[j];
   arr[j] = temp;
  }
 }
}

//  Print the sorted array 
console.log(arr)//[0, 10, 20, 30, 40, 60, 60]

6. Insert sort


// Array
var arr = [10, 20, 40, 60, 60, 0, 30]

//  Sorting algorithm 
for(var i = 0; i < arr.length; i++) {
 var n = i;
 while(arr[n] > arr[n+1] && n >= 0) {
 	 var temp = arr[n];
	 arr[n] = arr[n+1];
 	 arr[n+1] = temp;
	 n--;
 }
}

//  Print the sorted array 
console.log(arr)//[0, 10, 20, 30, 40, 60, 60]

Summarize


Related articles: