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
// Takes in an array that has two sorted subarrays, | |
// from [p..q] and [q+1..r], and merges the array | |
var merge = function(array, p, q, r) { | |
var lowHalf = []; | |
var highHalf = []; | |
var k = p; | |
var i; | |
var j; | |
for (i = 0; k <= q; i++, k++) { |
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
// Takes in an array that has two sorted subarrays, | |
// from [p..q] and [q+1..r], and merges the array | |
var merge = function(array, p, q, r) { | |
// This code has been purposefully obfuscated, | |
// as you'll write it yourself in next challenge. | |
var a=[],b=[],c=p,d,e;for(d=0;c<=q;d++,c++){a[d]=array[c];}for(e=0;c<=r;e++,c++){b[e]=array[c];}c=p;for(e=d=0;d<a.length&&e<b.length;){if(a[d]<b[e]){array[c]=a[d];d++;} else {array[c]=b[e]; e++;}c++; }for(;d<a.length;){array[c]=a[d];d++;c++;}for(;e<b.length;){array[c]=b[e];e++;c++;} | |
}; | |
// Takes in an array and recursively merge sorts it |
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
// This library exposes 3 functions: | |
// hanoi.moveDisk(fromPeg, toPeg); This moves the top disk from the fromPeg to the toPeg | |
// hanoi.getSparePeg(fromPeg, toPeg); This returns the remaining peg that isn't the fromPeg or the toPeg | |
// hanoi.isSolved(toPeg); This returns true if all disks are on toPeg and no invalid moves have been used | |
var hanoi = (function() { | |
var sprites = function() { | |
"use strict"; | |
// A minimalist sprite library for KA visualizations. | |
// Devin Balkcom, June 2014. |
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
var isEven = function(n) { | |
return n % 2 === 0; | |
}; | |
var isOdd = function(n) { | |
return !isEven(n); | |
}; | |
var power = function(x, n) { | |
//println("Computing " + x + " raised to power " + n + "."); |
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
// Returns the first character of the string str | |
var firstCharacter = function(str) { | |
return str.slice(0, 1); | |
}; | |
// Returns the last character of a string str | |
var lastCharacter = function(str) { | |
return str.slice(-1); | |
}; |
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
var factorial = function(n) { | |
// n이 0일 경우 1을 리턴(항등원) | |
if(n===0){ | |
return 1; | |
} | |
// 0이 아닐 경우, n보다 작은 수의 팩토리얼 계산(재귀함수 호출) | |
// 최종의 경우 n*(n-1)!=n! 와동일 | |
return n*factorial(n-1); | |
}; |
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
var insert = function(array, rightIndex, value) { | |
for(var j = rightIndex; | |
j >= 0 && array[j] > value; | |
j--) { | |
array[j + 1] = array[j]; | |
} | |
array[j + 1] = value; | |
}; | |
var insertionSort = function(array) { |
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
var insert = function(array, rightIndex, value) { | |
//Compare values between array[rightIndex] and value. | |
//if value is smaller than array[rightIndex], that array's value will be copied to array[rightIndex+1]. | |
//This work keep going until finding value is bigger than array[i]("for" loop) | |
//if find smaller array value or i reach 0, loop will be end and value will be copied to array[i+1] | |
var i; | |
for(i=rightIndex;i>=0&&array[i]>value;i--){ | |
array[i+1]=array[i]; | |
} | |
array[i+1]=value; |
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
var insert = function(array, rightIndex, value) { | |
//Compare values between array[rightIndex] and value. | |
//if value is smaller than array[rightIndex], that array's value will be copied to array[rightIndex+1]. | |
//This work keep going until finding value is bigger than array[i]("for" loop) | |
//if find smaller array value or i reach 0, loop will be end and value will be copied to array[i+1] | |
var i; | |
for(i=rightIndex;i>=0&&array[i]>value;i--){ | |
array[i+1]=array[i]; | |
} | |
array[i+1]=value; |
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
/*하위 내용은 칸 아카데미-알고리즘 강의 내 이진 검색 부분의 퀴즈 내용을 정리한 | |
* https://ko.khanacademy.org/computing/computer-science/algorithms/binary-search/e/running-time-of-binary-search | |
*Below source is a way by me about Khan academy.org 's algorithms-binary search quiz | |
*/ | |
/* Returns either the index of the location in the array, | |
or -1 if the array did not contain the targetValue */ | |
var doSearch = function(array, targetValue) { | |
var min = 0; | |
var max = array.length - 1; | |
var guess; |