{"id":26611,"student_id":70,"content":"var nums = shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]);\nvar chart = new Bars(nums);\n\nfor (var a = 0; a \u003c nums.length; a++) {\n let best = a;\n for (var b = a + 1; b \u003c nums.length; b++) {\n if (nums[b] \u003e nums[best]) {\n best = b;\n }\n chart.lightUp([b, best]);\n }\n var tmp = nums[a];\n nums[a] = nums[best]; \n nums[best] = tmp;\n chart.swap(a, best);\n}","created_at":"2018-04-16T16:20:52.974+08:00","updated_at":"2019-11-10T04:26:17.901+08:00","name":"selection sort","language":"javascript","screenshot":{"url":"https://cdn5.koding.school/uploads/project/screenshot/26611/588a27670805cefaaa16270842f3842d.jpg"},"parent_id":2,"plugin":"function Bars (arr) {\n this.data = []\n this.animate = [];\n this.playing = false;\n\n let that = this;\n arr.slice().forEach((n, idx) =\u003e {\n that.data.push({\n x: idx*30,\n y: 250,\n num: n,\n select: false,\n color: 'black'\n })\n });\n\n forever(() =\u003e {\n that.data.forEach((b, idx) =\u003e {\n pen.color = pen.fillColor = b.color;\n pen.drawRect(b.x + 10, b.y + 100, 15, -b.num*10 - 10);\n print(b.num, b.x + 10, b.y + 100 + 10, 'black', 12);\n });\n\n if (that.animate.length \u003e 0 \u0026\u0026 that.playing == false) that.play();\n\n if (that.playing) {\n that.data.forEach((b) =\u003e {\n if (b.move != 0) {\n if (b.move \u003e 0) {\n b.move -= 3;\n b.x += 3;\n }\n if (b.move \u003c 0) {\n b.move += 3;\n b.x -= 3; \n }\n if (Math.abs(b.move) \u003c 3) {\n b.x += b.move;\n b.move = 0;\n b.color = 'black';\n that.playing = false;\n }\n }\n });\n }\n });\n}\n\nBars.prototype.play = function () {\n\n if (this.playing) return;\n this.playing = true;\n\n let args = this.animate.shift();\n\n if (args[0] == 'swap') {\n let a = this.data[args[1]];\n let b = this.data[args[2]];\n a.color = b.color = 'red';\n let v = a.x - b.x;\n a.move = -v;\n b.move = v;\n this.data[args[1]] = b\n this.data[args[2]] = a;\n }\n\n if (args[0] == 'lightUp') {\n let arr = args[1];\n let that = this;\n let data = this.data;\n arr.forEach((dix) =\u003e {\n data[dix].color = 'red';\n that.playing = true;\n });\n setTimeout(()=\u003e {\n arr.forEach((dix) =\u003e {\n data[dix].color = 'black';\n that.playing = false; \n });\n }, 100);\n }\n}\n\nBars.prototype.lightUp = function (arr) {\n this.animate.push(['lightUp', arr]);\n}\n\nBars.prototype.swap = function (a, b) {\n if (a == b) return;\n this.animate.push(['swap', a, b]);\n}\n\nfunction shuffle(arr) {\n let newArr = [];\n while (arr.length \u003e 0) {\n let rand = Math.floor(Math.random() * arr.length);\n newArr.push(arr.splice(rand, 1)[0]);\n }\n return newArr;\n}","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":257,"hashid":"p93s4gm3","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦