Created
May 28, 2015 00:58
-
-
Save xypaul/f9504f455ad97e343639 to your computer and use it in GitHub Desktop.
Quick sort implementation in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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