-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_quicksort.js
74 lines (64 loc) · 1.91 KB
/
example_quicksort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* I read a post about quicksort with Javascript,
* comparing the functions 'splice' and 'push' when coding the QuickSort
* https://www.linkedin.com/feed/update/urn%3Ali%3Aactivity%3A7046704923937284098/
* https://pt.wikipedia.org/wiki/Quicksort
*
*
* Javascript array methods:
* https://www.w3schools.com/js/js_array_methods.asp
* - The pop() method removes the last element from an array
* - The push() method adds a new element to an array (at the end)
* - The shift() method removes the first array element and "shifts" all other elements to a lower index.
* - The concat() method creates a new array by merging (concatenating) existing arrays
* - The flat() method creates a new array with sub-array elements concatenated to a specified depth.
* - The splice() method adds new items to an array.
* - The slice() method slices out a piece of an array.
*
*/
// create an array with 1000 random items
// Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
const list = [];
while (list.length < 100)
{
list.push( Math.floor(Math.random() * 10000) );
}
console.log( "ORIGINAL/RANDOM List:", list );
// Let's do it
quick_sort_01( list, 0, list.length-1 );
function quick_sort_01( aList, iBegin, jEnd )
{
let i = iBegin;
let j = jEnd;
const pivot = aList[ Math.floor( (i+j) / 2 ) ];
while(i < j)
{
while(aList[i] < pivot && i < jEnd)
{
i++;
}
while(aList[j] > pivot && j > iBegin)
{
j--;
}
if(i <= j)
{
// switch...
var aux = aList[i];
aList[i] = aList[j];
aList[j] = aux;
i++;
j--;
}
}
if(iBegin < j)
{
quick_sort_01(aList, iBegin, j);
}
if(i < jEnd)
{
quick_sort_01(aList, i, jEnd);
}
}
console.log( "Q-SORTED List : ", list );
// TODO: quick_sort_02