Skip to content

Instantly share code, notes, and snippets.

View scriptonian's full-sized avatar

I-am Konstantin scriptonian

View GitHub Profile
@scriptonian
scriptonian / selection-sort.js
Last active December 9, 2017 16:42
Javascript Selection Sort
function ScriptoniteSort() {
this.arr = [];
}
ScriptoniteSort.prototype = {
swap: function(arr, index_one, index_two) {
//swap
var temp = arr[index_one];
arr[index_one] = arr[index_two];
arr[index_two] = temp;
@scriptonian
scriptonian / bubblesort.js
Last active December 4, 2017 20:02
Javascript Bubble Sort
function ScriptoniteSort() {
this.arr = [];
}
ScriptoniteSort.prototype = {
swap: function(arr, index_one, index_two) {
//swap
var temp = arr[index_one];
arr[index_one] = arr[index_two];
arr[index_two] = temp;
@scriptonian
scriptonian / binary-search.js
Last active November 17, 2017 19:47
Javascript Binary Search
var myList = [1, 3, 5, 7, 9];
function binarySearch(myList, itemToFind) {
var low = 0,
high = myList.length - 1,
mid,
guessed;
while(low <= high) {
mid = Math.floor((low + high) / 2),
@scriptonian
scriptonian / sequential-search-organize.js
Created November 10, 2017 03:42
Javascript self organizing data
var items = [2, 5, 3, 7, 8, 10, 15, 18, 24, 111, 12, 19, 87];
function itemSearch(itemToFind) {
items.forEach(function(itemValue, index, array){
if(itemValue === itemToFind) {
//as long as the current index value is not the first index in the array
if(index > 0) {
selfOrganize(array, index, index -1);
}
}
@scriptonian
scriptonian / max-min.js
Created November 8, 2017 15:04
Using Sequential Search To Return Max/Min Values
var items = [2, 5, 3, 7, 8, 10, 15, 18, 24, 111, 12, 19, 87];
//Returns the Min Value
function findMinValue(items){
var currentMin = items[0];
items.forEach(function(item){
if(currentMin > item) {
currentMin = item;
}
});
@scriptonian
scriptonian / sequential-search-a.js
Last active September 10, 2020 19:44
Performing sequential search in javascript
//Using a for loop
var items = [2, 5, 3, 7, 8, 10, 15, 18, 24, 111, 12, 19, 87];
function itemSearch(item) {
for(var i=0; i < items.length; i++) {
if(items[i] === item) {
console.log("Found item " + item + " at index " + i);
return true;
}
//ES5 VERSION
DFS : function(v, color) {
//change the color to grey
color[v] = 'grey';
//get its adjacency list
var curVertexAdjLst = this.getAdjacencyListVertex(v);
var that = this;
curVertexAdjLst.forEach(function(listVertex){
if(color[listVertex] === 'white') {
@scriptonian
scriptonian / graph-es5.js
Last active October 15, 2017 17:26
Javascript Graph - Shortest Path
//ES5 VERSION
function Graph() {
this.vertices = [];
this.adjacentList = new Map();
this.numberOfEdges = 0;
}
Graph.prototype = {
/*
@scriptonian
scriptonian / graph-es5.js
Last active October 1, 2017 23:23
Adding Breadth First Search to Graph Implementation
//ES5 Version
function Graph() {
this.vertices = [];
this.adjacentList = new Map();
}
Graph.prototype = {
/*
other methods go here
*/
@scriptonian
scriptonian / graph-es5.js
Last active September 25, 2017 01:35
Adding toString to graph implementation
function Graph() {
this.vertices = [];
this.adjacentList = new Map();
}
Graph.prototype = {
/*
other methods go here
*/