Last active
December 24, 2015 04:19
-
-
Save dtinth/6743484 to your computer and use it in GitHub Desktop.
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 ng-app="sort"> | |
<body ng-controller="SortingController"> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.1.0/lodash.min.js"></script> | |
<table border="1"> | |
<tr ng-repeat="c in data"> | |
<th>data[{{ $index }}]</th> | |
<th>{{ c }}</th> | |
<th><div style="width: {{ c * 30 }}px; height: 30px; background: black;"></div></th> | |
<th><input type="radio" ng-model="$parent.a" value="{{ $index }}"></th> | |
<th><input type="radio" ng-model="$parent.b" value="{{ $index }}"></th> | |
</tr> | |
</table> | |
<p> | |
<button type="button" ng-click="shuffle()">Shuffle</button> | |
<button type="button" ng-click="swap()">Swap</button> | |
<button type="button" ng-click="insertionSort()">Insertion Sort</button> | |
<button type="button" ng-click="selectionSort()">Selection Sort</button> | |
<button type="button" ng-click="bubbleSort()">Bubble Sort</button> | |
</p> | |
<script> | |
function swap(array, a, b) { | |
var temp = array[a] | |
array[a] = array[b] | |
array[b] = temp | |
} | |
angular.module('sort', []).controller('SortingController', function($scope) { | |
$scope.data = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] | |
$scope.shuffle = function() { $scope.data = _.shuffle($scope.data) } | |
$scope.swap = function() { swap($scope.data, $scope.a, $scope.b) } | |
$scope.insertionSort = function() { | |
var data = $scope.data | |
var n = data.length | |
// do insertion sort | |
for (var i = 1; i < n; i ++) { | |
var j = i | |
while (j > 0 && data[j] < data[j - 1]) { | |
swap(data, j, j - 1) | |
j = j - 1 | |
} | |
} | |
} | |
$scope.selectionSort = function() { | |
var data = $scope.data | |
var n = data.length | |
// do selection sort | |
for (var i = 0; i < n - 1; i ++) { | |
for (var j = i + 1; j < n; j ++) { | |
if (data[j] < data[i]) swap(data, i, j) | |
} | |
} | |
} | |
$scope.bubbleSort = function() { | |
var data = $scope.data | |
var n = data.length | |
// do bubble sort | |
for (var m = n - 2; m >= 0; m --) { | |
for (var i = 0; i <= m; i ++) { | |
if (data[i] > data[i + 1]) { | |
swap(data, i, i + 1) | |
} | |
} | |
} | |
} | |
}) | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment