前端js 算法收集

js模拟螺旋矩形算法
代码片段 1

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
    <meta http-equiv=”content-type” content=”text/html;charset=utf-8″ />
    <meta name=”generator” content=”editplus” />
    <meta name=”author” content=”” />
    <meta name=”keywords” content=”” />
    <meta name=”description” content=”” />
    <title> new document </title>
</head>
<body>
    <div id=”container”></div>
    <script type=”text/javascript”>
        var cal = function (len) {
            var helix = [[], [], [], [], [], []];
            var min = 0;
            var max = len – 1;
            var row = 0;
            var col = 0;
            for (var i = 0; i < len * len; i++) {
                helix[row][col] = i + 1;
                if (row == min && col < max) {
                    col = col + 1;
                }
                else if (row < max && col == max) {
                    row = row + 1;
                }
                else if (row == max && col > min) {
                    col = col – 1;
                }
                else if (row > min && col == min) {
                    row = row – 1;
                }
                if (row – 1 == min && col == min) {
                    min = min + 1;
                    max = max – 1;
                }
            }
            return helix;
        };
        var helix = cal(6);
        var html = ”;
        for (var i = 0; i < helix.length; i++) {
            for (var j = 0; j < helix[i].length; j++) {
                if (helix[i][j] < 10) {
                    helix[i][j] = “0” + helix[i][j];
                }
                html += helix[i][j] + ‘ ‘;
            }
            html += ”;
        }
        var container = document.getElementById(“container”);
        container.innerHTML = html;
    </script>
</body>
</html>
    js最短路径

代码片段 2

<html>
<head>
    <title>use A* to find path…</title>
</head>
<body style=”margin:0px”>
    <script>
        /*
        written by hjjboy
        email:tianmashuangyi@163.com
        qq:156809986
        */
        var closelist = new Array(), openlist = new Array();
        var gw = 10, gh = 10, gwh = 14;
        var p_start = new Array(2), p_end = new Array(2);
        var s_path, n_path = “”;
        var num, bg, flag = 0;
        var w = 30, h = 20;
        function GetRound(pos) {
            var a = new Array();
            a[0] = (pos[0] + 1) + “,” + (pos[1] – 1);
            a[1] = (pos[0] + 1) + “,” + pos[1];
            a[2] = (pos[0] + 1) + “,” + (pos[1] + 1);
            a[3] = pos[0] + “,” + (pos[1] + 1);
            a[4] = (pos[0] – 1) + “,” + (pos[1] + 1);
            a[5] = (pos[0] – 1) + “,” + pos[1];
            a[6] = (pos[0] – 1) + “,” + (pos[1] – 1);
            a[7] = pos[0] + “,” + (pos[1] – 1);
            return a;
        }
        function GetF(arr) {
            var t, G, H, F;
            for (var i = 0; i < arr.length; i++) {
                t = arr[i].split(“,”);
                t[0] = parseInt(t[0]); t[1] = parseInt(t[1]);
                if (IsOutScreen([t[0], t[1]]) || IsPass(arr[i]) || InClose([t[0], t[1]]) || IsStart([t[0], t[1]]) || !IsInTurn([t[0], t[1]]))
                    continue;
                if ((t[0] – s_path[3][0]) * (t[1] – s_path[3][1]) != 0)
                    G = s_path[1] + gwh;
                else
                    G = s_path[1] + gw;
                if (InOpen([t[0], t[1]])) {
                    if (G < openlist[num][1]) {
                        openlist[num][0] = (G + openlist[num][2]);
                        openlist[num][1] = G;
                        openlist[num][4] = s_path[3];
                    }
                    else { G = openlist[num][1]; }
                }
                else {
                    H = (Math.abs(p_end[0] – t[0]) + Math.abs(p_end[1] – t[1])) * gw;
                    F = G + H;
                    arr[i] = new Array();
                    arr[i][0] = F; arr[i][1] = G; arr[i][2] = H; arr[i][3] = [t[0], t[1]]; arr[i][4] = s_path[3];
                    openlist[openlist.length] = arr[i];
                }
                if (maptt.rows[t[1]].cells[t[0]].style.backgroundColor != “#cccccc” && maptt.rows[t[1]].cells[t[0]].style.backgroundColor != “#0000ff” && maptt.rows[t[1]].cells[t[0]].style.backgroundColor != “#ff0000” && maptt.rows[t[1]].cells[t[0]].style.backgroundColor != “#00ff00”) {
                    maptt.rows[t[1]].cells[t[0]].style.backgroundColor = “#FF00FF”;
                    //maptt.rows[t[1]].cells[t[0]].innerHTML=”<font color=white>”+G+”</font>”;
                }
            }
        }
        function IsStart(arr) {
            if (arr[0] == p_start[0] && arr[1] == p_start[1])
                return true;
            return false;
        }
        function IsInTurn(arr) {
            if (arr[0] > s_path[3][0]) {
                if (arr[1] > s_path[3][1]) {
                    if (IsPass((arr[0] – 1) + “,” + arr[1]) || IsPass(arr[0] + “,” + (arr[1] – 1)))
                        return false;
                }
                else if (arr[1] < s_path[3][1]) {
                    if (IsPass((arr[0] – 1) + “,” + arr[1]) || IsPass(arr[0] + “,” + (arr[1] + 1)))
                        return false;
                }
            }
            else if (arr[0] < s_path[3][0]) {
                if (arr[1] > s_path[3][1]) {
                    if (IsPass((arr[0] + 1) + “,” + arr[1]) || IsPass(arr[0] + “,” + (arr[1] – 1)))
                        return false;
                }
                else if (arr[1] < s_path[3][1]) {
                    if (IsPass((arr[0] + 1) + “,” + arr[1]) || IsPass(arr[0] + “,” + (arr[1] + 1)))
                        return false;
                }
            }
            return true;
        }
        function IsOutScreen(arr) {
            if (arr[0] < 0 || arr[1] < 0 || arr[0] > (w – 1) || arr[1] > (h – 1))
                return true;
            return false;
        }
        function InOpen(arr) {
            var bool = false;
            for (var i = 0; i < openlist.length; i++) {
                if (arr[0] == openlist[i][3][0] && arr[1] == openlist[i][3][1]) {
                    bool = true; num = i; break;
                }
            }
            return bool;
        }
        function InClose(arr) {
            var bool = false;
            for (var i = 0; i < closelist.length; i++) {
                if ((arr[0] == closelist[i][3][0]) && (arr[1] == closelist[i][3][1])) {
                    bool = true; break;
                }
            }
            return bool;
        }
        function IsPass(pos) {
            if ((“;” + n_path + “;”).indexOf(“;” + pos + “;”) != -1)
                return true;
            return false;
        }
        function Sort(arr) {
            var temp;
            for (var i = 0; i < arr.length; i++) {
                if (arr.length == 1) break;
                if (arr[i][0] <= arr[i + 1][0]) {
                    temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                }
                if ((i + 1) == (arr.length – 1))
                    break;
            }
        }
        function main() {
            GetF(GetRound(s_path[3]));
            Sort(openlist);
            s_path = openlist[openlist.length – 1];
            closelist[closelist.length] = s_path;
            openlist[openlist.length – 1] = null;
            if (openlist.length == 0) { alert(“找不到路径”); return; }
            openlist.length = openlist.length – 1;
            if ((s_path[3][0] == p_end[0]) && (s_path[3][1] == p_end[1])) {
                getPath();
            }
            else { maptt.rows[s_path[3][1]].cells[s_path[3][0]].style.backgroundColor = “#00ff00”; setTimeout(“main()”, 100); }
        }
        function getPath() {
            var str = “”;
            var t = closelist[closelist.length – 1][4];
            while (1) {
                str += t.join(“,”) + “;”;
                maptt.rows[t[1]].cells[t[0]].style.backgroundColor = “#ffff00”;
                for (var i = 0; i < closelist.length; i++) {
                    if (closelist[i][3][0] == t[0] && closelist[i][3][1] == t[1])
                        t = closelist[i][4];
                }
                if (t[0] == p_start[0] && t[1] == p_start[1])
                    break;
            }
            alert(str);
        }
        function setPos() {
            var h = (Math.abs(p_end[0] – p_start[0]) + Math.abs(p_end[1] – p_start[1])) * gw;
            s_path = [h, 0, h, p_start, p_start];
        }
        function set(id, arr) {
            switch (id) {
                case 1:
                    p_start = arr;
                    maptt.rows[arr[1]].cells[arr[0]].style.backgroundColor = “#ff0000”; break;
                case 2:
                    p_end = arr; maptt.rows[arr[1]].cells[arr[0]].style.backgroundColor = “#0000ff”; break;
                case 3:
                    n_path += arr.join(“,”) + “;”; maptt.rows[arr[1]].cells[arr[0]].style.backgroundColor = “#cccccc”; break;
                default:
                    break;
            }
        }
        function setflag(id) { flag = id; }
    </script>
    <table id=”maptt” cellspacing=”1″ cellpadding=”0″ border=”0″ bgcolor=”#000000″>
        <script>
            for (var i = 0; i < h; i++) {
                document.write(“<tr>”);
                for (var j = 0; j < w; j++) {
                    document.write(‘<td onclick=”set(flag,[‘ + j + ‘,’ + i + ‘]);” bgcolor=”#ffffff” width=”20″ height=”20″></td>’);
                }
                document.write(“</tr>”);
            }
        </script>
    </table>
    <a href=”javascript:setflag(1);”>设置起点</a><br>
    <a href=’javascript:setflag(2);’>设置终点</a><br>
    <a href=’javascript:setflag(3);’>设置障碍点</a><br>
    <input type=”button” onclick=”setPos();main();this.disabled=true;” value=”find”>
</body>
</html>
   js_常见排序算法

代码片段 3

<html>
<head>
    <script>
        Array.prototype.swap = function (i, j) {
            var temp = this[i];
            this[i] = this[j];
            this[j] = temp;
        }
        Array.prototype.bubbleSort = function () {
            for (var i = this.length – 1; i > 0; –i) {
                for (var j = 0; j < i; ++j) {
                    if (this[j] > this[j + 1]) this.swap(j, j + 1);
                }
            }
        }
        Array.prototype.selectionSort = function () {
            for (var i = 0; i < this.length; ++i) {
                var index = i;
                for (var j = i + 1; j < this.length; ++j) {
                    if (this[j] < this[index]) index = j;
                }
                this.swap(i, index);
            }
        }
        Array.prototype.insertionSort = function () {
            for (var i = 1; i < this.length; ++i) {
                var j = i, value = this[i];
                while (j > 0 && this[j – 1] > value) {
                    this[j] = this[j – 1];
                    –j;
                }
                this[j] = value;
            }
        }
        Array.prototype.shellSort = function () {
            for (var step = this.length >> 1; step > 0; step >>= 1) {
                for (var i = 0; i < step; ++i) {
                    for (var j = i + step; j < this.length; j += step) {
                        var k = j, value = this[j];
                        while (k >= step && this[k – step] > value) {
                            this[k] = this[k – step];
                            k -= step;
                        }
                        this[k] = value;
                    }
                }
            }
        }
        Array.prototype.quickSort = function (s, e) {
            if (s == null) s = 0;
            if (e == null) e = this.length – 1;
            if (s >= e) return;
            this.swap((s + e) >> 1, e);
            var index = s – 1;
            for (var i = s; i <= e; ++i) {
                if (this[i] <= this[e]) this.swap(i, ++index);
            }
            this.quickSort(s, index – 1);
            this.quickSort(index + 1, e);
        }
        Array.prototype.stackQuickSort = function () {
            var stack = [0, this.length – 1];
            while (stack.length > 0) {
                var e = stack.pop(), s = stack.pop();
                if (s >= e) continue;
                this.swap((s + e) >> 1, e);
                var index = s – 1;
                for (var i = s; i <= e; ++i) {
                    if (this[i] <= this[e]) this.swap(i, ++index);
                }
                stack.push(s, index – 1, index + 1, e);
            }
        }
        Array.prototype.mergeSort = function (s, e, b) {
            if (s == null) s = 0;
            if (e == null) e = this.length – 1;
            if (b == null) b = new Array(this.length);
            if (s >= e) return;
            var m = (s + e) >> 1;
            this.mergeSort(s, m, b);
            this.mergeSort(m + 1, e, b);
            for (var i = s, j = s, k = m + 1; i <= e; ++i) {
                b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++];
            }
            for (var i = s; i <= e; ++i) this[i] = b[i];
        }
        Array.prototype.heapSort = function () {
            for (var i = 1; i < this.length; ++i) {
                for (var j = i, k = (j – 1) >> 1; k >= 0; j = k, k = (k – 1) >> 1) {
                    if (this[k] >= this[j]) break;
                    this.swap(j, k);
                }
            }
            for (var i = this.length – 1; i > 0; –i) {
                this.swap(0, i);
                for (var j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1) {
                    if (k == i || this[k] < this[k – 1])–k;
                    if (this[k] <= this[j]) break;
                    this.swap(j, k);
                }
            }
        }
        function generate() {
            var max = parseInt(txtMax.value), count = parseInt(txtCount.value);
            if (isNaN(max) || isNaN(count)) {
                alert(“个数和最大值必须是一个整数”);
                return;
            }
            var array = [];
            for (var i = 0; i < count; ++i) array.push(Math.round(Math.random() * max));
            array.push(“99”);
            txtInput.value = array.join(” “);
            txtOutput.value = “”;
        }
        function demo(type) {
            var array = txtInput.value == “” ? [] : txtInput.value.replace().split(” “);
            for (var i = 0; i < array.length; ++i) array[i] = parseInt(array[i]);
            var t1 = new Date();
            eval(“array.” + type + “Sort()”);
            var t2 = new Date();
            lblTime.innerText = t2.valueOf() – t1.valueOf();
            txtOutput.value = array.join(” “);
        }
        function search() {
            var txtSearch = document.getElementById(“txtSearch”);
            var array = txtInput.value == “” ? [] : txtInput.value.replace().split(” “);
            var arrayResult = new Array();
            var t1 = new Date();
            alert(array.length);
            for (var i = 0; i < array.length; ++i) {
                if (array[i].indexOf(txtSearch.value) >= 0) {
                    arrayResult.push(array[i]);
                }
            }
            //eval(“array.” + type + “Sort()”);
            var t2 = new Date();
            lblTime.innerText = t2.valueOf() – t1.valueOf();
            txtOutput.value = arrayResult.join(” “);
        }
    </script>
</head>
<body onload=generate()>
    <table style=”width:100%;height:100%;font-size:12px;font-family:宋体”>
        <tr>
            <td align=right>
                <textarea id=txtInput readonly style=”width:100px;height:100%”></textarea>
            </td>
            <td width=150 align=center>
                检索 <input id=”txtSearch” value=”” style=”width:50px”><br><br>
                随机数个数<input id=txtCount value=500 style=”width:50px”><br><br>
                最大随机数<input id=txtMax value=1000 style=”width:50px”><br><br>
                <button onclick=generate()>重新生成</button><br><br><br><br>
                耗时(毫秒):<label id=lblTime></label><br><br><br><br>
                <button onclick=demo(“bubble”)>冒泡排序</button><br><br>
                <button onclick=demo(“selection”)>选择排序</button><br><br>
                <button onclick=demo(“insertion”)>插入排序</button><br><br>
                <button onclick=demo(“shell”)>谢尔排序</button><br><br>
                <button onclick=demo(“quick”)>快速排序(递归)</button><br><br>
                <button onclick=demo(“stackQuick”)>快速排序(堆栈)</button><br><br>
                <button onclick=demo(“merge”)>归并排序</button><br><br>
                <button onclick=demo(“heap”)>堆排序</button><br><br>
                <button onclick=search()>检索</button><br><br>
            </td>
            <td align=left>
                <textarea id=txtOutput readonly style=”width:100px;height:100%”></textarea>
            </td>
        </tr>
    </table>
</body>
</html>
    这是网上的例子,感觉好头疼啊,有没有算法教程,我看那书里面都没有讲解的啊!
    --------------------------------------------------------------------------------------
    [图像混合(溶图)算法的javascript实现](http://www.html-js.com/article/1801)
    [各种数据结构与算法知识入门经典](http://www.html-js.com/blog/2316)
    数据结构与算法JavaScript描述 [试读](http://images.china-pub.com/ebook3800001-3805000/3804002/ch01.pdf)
    [常用算法](http://book.51cto.com/art/201311/415262.htm)

iphone前端开发是什么 web前端开发是什么语言 web前端开发要什么软件
» 本文来自:前端开发者 » 《前端js 算法收集》
» 本文链接地址:https://www.rokub.com/5610.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!