Friday, May 12, 2017

Quick Sort

Basic quick sort.
Recursively sorts a list of elements


function quicksort(list)= 
!(len(list)>0)?[]:
let(
pivot = list[floor(len(list)/2)],
list_lesser =[ for(i=[0:len(list)-1])if( y[i] < pivot )y[i] ],
list_equal  =[ for(i=[0:len(list)-1])if( y[i]== pivot )y[i] ],
list_greater=[ for(i=[0:len(list)-1])if( y[i] > pivot )y[i] ])

concat(
quicksort(list_lesser),
list_equal,
quicksort(list_greater));

Sorts a list of elements by column col.

function quicksort_col(list,col=0)= 
!(len(list)>0)?[]:
let(
pivot = list[floor(len(list)/2)][col],
list_lesser =[ for(i=[0:len(list)-1])if( list[i][col] < pivot )list[i] ],
list_equal  =[ for(i=[0:len(list)-1])if( list[i][col]== pivot )list[i] ],
list_greater=[ for(i=[0:len(list)-1])if( list[i][col] > pivot )list[i] ])

concat(
quicksort_col(list_lesser),
list_equal,
quicksort_col(list_greater));





No comments:

Post a Comment