Last active
September 21, 2017 02:41
-
-
Save swathiPaipalle/4d1a4a72de0ebf884183c9b1fd4d4670 to your computer and use it in GitHub Desktop.
Quick sort using Javascript// source https://jsbin.com/kacayap
This file contains hidden or 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 name="description" content="Quick sort using Javascript"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div> | |
<h1>Quicksort using Javascript</h1> | |
<ul> | |
<li>Divide: Break problem down during each call</li> | |
<li>Conquer: Do work on each subset</li> | |
<li>Combine: none</li> | |
</ul> | |
<h4>Coding explanation</h4> | |
<p> | |
<ul> | |
<li>Choose a pivot point</li> | |
<li>Rearrange all elements greater than pivot point to the right</li> | |
<li>Rearrange all elements lesser than pivot point to the left</li> | |
<li>Recursively apply above steps to the sub arrays on either side of the point</li> | |
</p> | |
</div> | |
<script id="jsbin-javascript"> | |
function quicksort(arr){ | |
if(arr.length < 2){ | |
return arr; | |
} | |
var pivot = arr.length-1; | |
var lesser = []; | |
var greater = []; | |
for(var i=0; i<arr.length-1; i++){ | |
if(arr[i] < arr[pivot]){ | |
lesser.push(arr[i]); | |
}else{ | |
greater.push(arr[i]); | |
} | |
} | |
console.log(lesser); | |
console.log(greater); | |
return quicksort(lesser).concat(arr[pivot], quicksort(greater)); | |
} | |
var arr = [3,5,1,3,7,2,8,3,9,4]; | |
console.log("Sorted", quicksort(arr)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function quicksort(arr){ | |
if(arr.length < 2){ | |
return arr; | |
} | |
var pivot = arr.length-1; | |
var lesser = []; | |
var greater = []; | |
for(var i=0; i<arr.length-1; i++){ | |
if(arr[i] < arr[pivot]){ | |
lesser.push(arr[i]); | |
}else{ | |
greater.push(arr[i]); | |
} | |
} | |
console.log(lesser); | |
console.log(greater); | |
return quicksort(lesser).concat(arr[pivot], quicksort(greater)); | |
} | |
var arr = [3,5,1,3,7,2,8,3,9,4]; | |
console.log("Sorted", quicksort(arr));</script></body> | |
</html> |
This file contains hidden or 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 quicksort(arr){ | |
if(arr.length < 2){ | |
return arr; | |
} | |
var pivot = arr.length-1; | |
var lesser = []; | |
var greater = []; | |
for(var i=0; i<arr.length-1; i++){ | |
if(arr[i] < arr[pivot]){ | |
lesser.push(arr[i]); | |
}else{ | |
greater.push(arr[i]); | |
} | |
} | |
console.log(lesser); | |
console.log(greater); | |
return quicksort(lesser).concat(arr[pivot], quicksort(greater)); | |
} | |
var arr = [3,5,1,3,7,2,8,3,9,4]; | |
console.log("Sorted", quicksort(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment