Skip to content

Instantly share code, notes, and snippets.

@xypaul
Created May 28, 2015 00:58
Show Gist options
  • Save xypaul/f9504f455ad97e343639 to your computer and use it in GitHub Desktop.
Save xypaul/f9504f455ad97e343639 to your computer and use it in GitHub Desktop.
Quick sort implementation in JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="ex"></div>
<script id="jsbin-javascript">
function qsort(ls){
var pivot = ls.length-1;
if (ls.length <=1) return ls;
var left = [];
var right= [];
for(var i=0;i<ls.length;i++){
if(i==pivot) continue;
if(ls[i] <= ls[pivot]){
left.push(ls[i]);
}else{
right.push(ls[i]);
}
}
return qsort(left).concat(ls[pivot],qsort(right));
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">function qsort(ls){
var pivot = ls.length-1;
if (ls.length <=1) return ls;
var left = [];
var right= [];
for(var i=0;i<ls.length;i++){
if(i==pivot) continue;
if(ls[i] <= ls[pivot]){
left.push(ls[i]);
}else{
right.push(ls[i]);
}
}
return qsort(left).concat(ls[pivot],qsort(right));
}</script></body>
</html>
function qsort(ls){
var pivot = ls.length-1;
if (ls.length <=1) return ls;
var left = [];
var right= [];
for(var i=0;i<ls.length;i++){
if(i==pivot) continue;
if(ls[i] <= ls[pivot]){
left.push(ls[i]);
}else{
right.push(ls[i]);
}
}
return qsort(left).concat(ls[pivot],qsort(right));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment