File tree Expand file tree Collapse file tree 1 file changed +14
-14
lines changed
04_quicksort/javascript/quick_sort Expand file tree Collapse file tree 1 file changed +14
-14
lines changed Original file line number Diff line number Diff line change 6
6
* @returns {Array } Sorted array
7
7
*/
8
8
function quicksort ( array ) {
9
- // base case, arrays with 0 or 1 element are already "sorted"
10
- if ( array . length < 2 ) return array ;
11
- // recursive case
12
- let pivot = array [ 0 ] ;
13
- // sub-array of all the elements less than the pivot
14
- let less = array . slice ( 1 ) . filter ( function ( el ) {
15
- return el <= pivot ;
16
- } ) ;
17
- // sub-array of all the elements greater than the pivot
18
- let greater = array . slice ( 1 ) . filter ( function ( el ) {
19
- return el > pivot ;
20
- } ) ;
21
- return quicksort ( less ) . concat ( [ pivot ] , quicksort ( greater ) ) ;
9
+ // base case, arrays with 0 or 1 element are already "sorted"
10
+ if ( array . length < 2 ) return array ;
11
+ // recursive case
12
+ let pivot = array [ 0 ] ;
13
+ // sub-array of all the elements less than the pivot
14
+ let less = array . slice ( 1 ) . filter ( function ( el ) {
15
+ return el <= pivot ;
16
+ } ) ;
17
+ // sub-array of all the elements greater than the pivot
18
+ let greater = array . slice ( 1 ) . filter ( function ( el ) {
19
+ return el > pivot ;
20
+ } ) ;
21
+ return quicksort ( less ) . concat ( pivot , quicksort ( greater ) ) ;
22
22
}
23
23
24
- console . log ( quicksort ( [ 10 , 5 , 2 , 3 ] ) ) ;
24
+ console . log ( quicksort ( [ 10 , 5 , 2 , 3 ] ) ) ;
You can’t perform that action at this time.
0 commit comments