Skip to content

Instantly share code, notes, and snippets.

@gohilumesh
Last active March 12, 2017 15:58
Show Gist options
  • Select an option

  • Save gohilumesh/d71a0404f4adc338ed891aa76114f639 to your computer and use it in GitHub Desktop.

Select an option

Save gohilumesh/d71a0404f4adc338ed891aa76114f639 to your computer and use it in GitHub Desktop.
function quickSortIterative(arr) {
let sorted = [],
stacked =[arr];
while (stacked.length) {
let temp = stacked.pop(),
tempLen = temp.length;
if (tempLen === 1) {
sorted.push(temp[0]);
continue;
}
let pivot = temp[0],
left = [],
right = [];
for (let i =1; i < tempLen; i++) {
if(temp[i] < pivot){
left.push(temp[i]);
}else {
right.push(temp[i]);
}
}
left.push(pivot);
if (right.length) {
stacked.push(right);
}
if (left.length) {
stacked.push(left);
}
}
return sorted;
}
console.log(quickSortIterative([8,4,6,2,1,9,5,5,4,3,4,3]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment