Open In App

D3.js quickselect() Method

The quickselect() method in D3.js is used to partially reorder an array in the quickest way possible.

Syntax:



d3.quickselect( array, k, left, right, compare )

Parameters: This method accepts five parameters as mentioned above and described below:

Return value: It returns the array after quick reordering.



Note: To execute the below examples you have to install the d3 library. The following command prompt we have to execute the following command.

npm install d3

Example 1: In this example, we can see that by using this method, we are able to get the array after reordering it in the quickest way possible.




// Defining d3 contrib variable  
var d3 = require('d3');
  
var reordered_array =
  d3.quickselect([3, 2, 1, 14, 5], 2);
  
console.log(reordered_array);

Output :

[ 1, 2, 3, 14, 5 ]

Example 2: In this example we are using the Math.random() function to generate different values and store it in an array. Then by applying the d3.quickselect() we are performing reordering in the array.




// Defining d3 contrib variable  
var d3 = require('d3');
  
var arr = [];
for(var i = 0; i < 5; i++) {
    arr.push(Math.random());
}
  
var reordered_array =
  d3.quickselect(arr, 4);
  
console.log(reordered_array);

Output :

[ 0.1504847356911596,
  0.42489989693286034,
  0.8801036441469585,
  0.5837860241062365,
  0.9175021021124463
]
Article Tags :