Skip to content

Instantly share code, notes, and snippets.

@zeraf29
zeraf29 / merge.js
Created July 2, 2017 13:29
병합 구현
// 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++) {
@zeraf29
zeraf29 / mergesort.js
Created June 11, 2017 14:26
병합정렬
// 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
@zeraf29
zeraf29 / hanoi.js
Last active April 20, 2020 00:38
칸 아카데미 - 하노이의 탑 소스
// 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.
@zeraf29
zeraf29 / power.js
Created April 23, 2017 09:07
칸 아카데미. 재귀를 활용한 거듭제곱(정수일 경우)
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 + ".");
// 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);
};
@zeraf29
zeraf29 / recursive.js
Created April 2, 2017 14:36
재귀 팩토리얼
var factorial = function(n) {
// n이 0일 경우 1을 리턴(항등원)
if(n===0){
return 1;
}
// 0이 아닐 경우, n보다 작은 수의 팩토리얼 계산(재귀함수 호출)
// 최종의 경우 n*(n-1)!=n! 와동일
return n*factorial(n-1);
};
@zeraf29
zeraf29 / insertion_sort.js
Created March 26, 2017 15:13
칸아카데미-삽입정렬 구현 소스
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) {
@zeraf29
zeraf29 / insert_example.js
Created March 26, 2017 14:39
[칸 아카데미] 알고리즘-삽입정렬 : 삽입구현 소스
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;
@zeraf29
zeraf29 / insert_example.js
Created March 26, 2017 14:39
[칸 아카데미] 알고리즘-삽입정렬 : 삽입구현 소스
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;
@zeraf29
zeraf29 / binarysearch.js
Last active March 1, 2017 13:50
자바스크립트를 이용한 배열 내 요소 검색 이진검색 함수(칸 아카데미 응용: 이진검색 내용)
/*하위 내용은 칸 아카데미-알고리즘 강의 내 이진 검색 부분의 퀴즈 내용을 정리한
* 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;