Skip to content

Instantly share code, notes, and snippets.

@denpalrius
Last active January 24, 2018 14:10
Show Gist options
  • Save denpalrius/9dfc8a7097c1b2704e4a654c9879842b to your computer and use it in GitHub Desktop.
Save denpalrius/9dfc8a7097c1b2704e4a654c9879842b to your computer and use it in GitHub Desktop.
Sorting Algorithms in Typescript
module ThingSpeak.Directives {
"use strict";
interface sortTechnique {
name?: string;
code?: number;
}
interface IScope extends ng.IScope {
list?: string;
numberList?: number[];
sortedList?: number[];
sortTypes?: sortTechnique[];
selectSortType?: any;
selectedSortType?: sortTechnique;
keyDownFn?: any;
}
export function dsSortingAlgorithms(): ng.IDirective {
return {
restrict: "AE",
scope: {
list: '=?list',
numberList: '=?numberList',
sortedList: '=?sortedList',
sortTypes: '=?sortTypes',
selectedSortType: "=?selectedSortType",
selectSortType: "&selectSortType",
keyDownFn: "&keyDownFn"
},
templateUrl: "/app/views/templates/ds-sorting-algorithms.html",
link(scope: IScope) {
init(scope);
scope.selectSortType = function () {
sort(scope);
}
scope.keyDownFn = function () {
sort(scope);
}
}
};
}
function init(scope: IScope) {
scope.list = "";
scope.numberList = [];
scope.sortedList = [];
scope.sortTypes = [
{ name: "Bubble Sort", code: 1 },
{ name: "Insertion Sort", code: 2 },
{ name: "Selection Sort", code: 3 },
{ name: "Merge Sort", code: 4 },
{ name: "Shell Sort", code: 5 },
{ name: "Quick Sort", code: 6 }
];
scope.selectedSortType = scope.sortTypes[0];
}
function sort(scope: IScope) {
scope.numberList = scope.list.split(',').map((s) => {
if (s) {
let n = parseInt(s);
if (!isNaN(n)) {
return n;
}
}
});
if (scope.selectedSortType && scope.numberList) {
switch (scope.selectedSortType.code) {
case 1:
scope.sortedList = bubbleSort(scope.numberList);
break;
case 2:
scope.sortedList = insertionSort(scope.numberList);
break;
case 3:
scope.sortedList = selectionSort(scope.numberList);
break;
case 4:
scope.sortedList = mergeSort(scope.numberList);
break;
case 5:
scope.sortedList = shellSort(scope.numberList);
break;
case 6:
scope.sortedList = quickSort(scope.numberList);
break;
}
}
}
function bubbleSort(numberList:number[]): number[] {
if (numberList) {
let listToSort = angular.copy(numberList);
let i = 0, j = 0, len = listToSort.length, currentValue = 0, nextValue=0, swapped = false;
for (i=0; i < len; i++){
swapped = false;
for (j=0; j < len-1; j++) {
currentValue = listToSort[j];
nextValue = listToSort[j + 1];
if (currentValue > nextValue) { /* compare the adjacent elements */
listToSort[j] = nextValue; /* swap them */
listToSort[j + 1] = currentValue;
swapped = true;
}
}
if (!swapped) {/*if no number was swapped that means array is sorted now, break the loop.*/
break;
}
}
return listToSort;
}
return [];
}
function insertionSort(numberList: number[]): number[] {
if (numberList) {
let listToSort = angular.copy(numberList);
let i = 0, j = 0, len = listToSort.length, holePosition = 0, valueToInsert = 0;
for (i = 0; i < len; i++){
valueToInsert = listToSort[i]; /* select value to be inserted */
holePosition = i;
/*locate hole position for the element to be inserted */
while (holePosition > 0 && listToSort[holePosition - 1] > valueToInsert) {
listToSort[holePosition] = listToSort[holePosition - 1];
holePosition = holePosition - 1;
}
listToSort[holePosition] = valueToInsert; /* insert the number at hole position */
}
return listToSort;
}
return [];
}
function selectionSort(numberList: number[]): number[] {
if (numberList) {
return numberList;
}
return [];
}
function mergeSort(numberList: number[]): number[] {
if (numberList) {
return numberList;
}
return [];
}
function shellSort(numberList: number[]): number[] {
if (numberList) {
return numberList;
}
return [];
}
function quickSort(numberList: number[]): number[] {
if (numberList) {
return numberList;
}
return [];
}
}
<div class="container">
<br />
<br />
<h1>Sorting Algorithms</h1>
<br />
<hr />
<h5>Select sorting algorithm:</h5>
<select title="Sort Types" style="width:400px"
ng-model="selectedSortType"
ng-options="sortType.name for sortType in sortTypes track by sortType.code"
ng-change="selectSortType()">
</select>
<br />
<br />
<h3>{{selectedSortType.name}}</h3>
<br />
<label>Enter comma (,) separated numbers</label>
<br />
<input type="text" ng-change="keyDownFn(list)" ng-model="list" style="width:400px"/>
<br /> <br />
<label>Unsorted List Sort</label>
<br />
<span style="font-weight:200" ng-repeat="number in numberList track by $index">{{$index >=1?',':''}} {{number}}</span>
<br /> <br />
<label>Sorted List</label>
<br />
<span style="font-weight:200" class="blink" ng-repeat="number in sortedList track by $index">{{$index >=1?',':''}} {{number}}</span>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment