Last active
November 26, 2015 00:38
-
-
Save RoloRobot/b2e15af9ab0d8c1bdbdd to your computer and use it in GitHub Desktop.
QuickSort
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 quickSort(array, left, right){ | |
var len = array.length, | |
pivot, | |
partitionIndex; | |
if(left < right){ | |
pivot = right; | |
partitionIndex = partition(array, pivot, left, right); | |
quickSort(array, left, partitionIndex - 1); | |
quickSort(array, partitionIndex + 1, right); | |
} | |
return array; | |
} | |
function partition(array, pivot, left, right){ | |
var pivotValue = array[pivot], | |
partitionIndex = left; | |
for(var i = left; i < right; i++){ | |
if(array[i] < pivotValue){ | |
swap(array, i, partitionIndex); | |
partitionIndex++; | |
} | |
} | |
swap(array, right, partitionIndex); | |
return partitionIndex; | |
} | |
function swap(array, i, j){ | |
var temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} | |
function RandNum(array, quantity) { | |
var num; | |
for (var i = 0; i < quantity; i++) { | |
num = Math.floor(Math.random() * (100 - 50 + 1)) + 10; | |
if (num !== array[i - 1]) { | |
array.push(num); | |
} else { | |
i--; | |
} | |
} | |
} | |
function sort(array){ | |
quickSort(array,0,array.length - 1); | |
document.getElementById("QuickTimes").insertAdjacentHTML("beforeend",array+"<br/>"); | |
} | |
function string(array){ | |
var test_array = ["Santa", "Arrow", "God", "Solo"]; | |
var sorted_array = sort(test_array, function(a,b) { return a<b; }); | |
console.log(array); | |
} |
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 xml:lang="en-US"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>Quicksort Test</title> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<script src="Puravsquick.js"></script> | |
<button onclick="var a = []; RandNum(a, 9); sort(a);" "sort()">Quicksort Commence!</button> | |
<button onclick="string()" "sort()">String Quicksort Commence!</button> | |
<div id="QuickTimes"> | |
</div> | |
<head> | |
<html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment