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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>JSALGO</title> | |
<script src="./lib/sort.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
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
1 2 3 4 5 6 | |
が正解なら、 | |
6 5 4 3 2 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
// MergeSort | |
mergeSort: function(items){ | |
var self = this; | |
// 受け取ったdata(配列)が、最小単位になるまで分割し続ける。 | |
if (items.length < 2) { | |
return items; | |
} | |
var middle = Math.floor(items.length / 2), |
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
combSort: function(data) { | |
var N = data.length; | |
var gap = N; | |
while((gap > 1) || flag) { | |
// 収縮率(間隔調整の度合い)は1.3。 | |
var gap = gap*10/13; | |
if (gap < 1) { gap = 1 }; | |
var flag = 0; |
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
// 選択ソート。計算量O(n^2)。 | |
selectionSort: function(data) { | |
var min; | |
for (var i = 0; i < data.length - 1; i++) { | |
// 先頭を最小値とする。 | |
min = i; | |
// 先頭から順に、それより小さい値があった場合そのうち一番小さいものと交換する。 | |
// | |
for (var j = i + 1; j < data.length; j++) { |
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
// 単純挿入ソート。最悪計算量O(n^2)。ループ回数から考えるとまあわかる。 | |
insersionSort: function(data) { | |
// 最初から最後までソート完了になるまで繰り返す | |
var tmp, i; | |
for (var sorted = 0; sorted < data.length - 1; sorted++) { | |
// ソート完了している領域の直後の値を取り出す。 | |
insert = data[sorted+1]; | |
// ソート済みの中で、挿入できる場所があるかを調べる。 | |
for (i = 0; i <= sorted; i++) { | |
if (data[i] > insert) { |
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 $floor = MathFloor; | |
function MathFloor(x) { | |
x = TO_NUMBER_INLINE(x); | |
// It's more common to call this with a positive number that's out | |
// of range than negative numbers; check the upper bound first. | |
if (x < 0x80000000 && x > 0) { | |
// Numbers in the range [0, 2^31) can be floored by converting | |
// them to an unsigned 32-bit value using the shift operator. | |
// We avoid doing so for -0, because the result of Math.floor(-0) |
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
// 2分挿入ソート | |
binaryInsertionSort: function(data) { | |
var left, right, mid, temp; | |
for (var sorted = 1; sorted < data.length; sorted++) { | |
var insert = data[sorted]; | |
// ここからバイナリサーチ | |
// どこに値を挿入するべきかを探す | |
left = 0; |
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
// バイナリサーチ(2分探索) | |
// 基準点の前後を分けて探ることで、計算量をO(logN)に抑えることができる。 | |
// バイナリサーチはあたいの大小を元に捜索するため、対象がソート済みの配列でなくてはならない。 | |
binarySearch: function(data, target) { | |
var left = 0, | |
right = data.length - 1, | |
middle; | |
while(left <= right) { | |
// 基準点を毎回更新する。 |
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 request = require('request'); | |
var gurl = 'http://google.com' | |
var yurl = 'http://yahoo.co.jp' | |
var testreq = function(url) { | |
return new Promise(function(resolve, reject) { | |
request(url, function (error, response, body) { | |
if (error) { | |
reject(err); | |
} else { | |
resolve(body); |
OlderNewer