当前位置:网站首页>js 简单快排实现
js 简单快排实现
2022-07-13 17:48:00 【校园苦行生】
<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div style="width: 940px; margin: auto;"> <h4 id="original"></h4> <ol id="result"></ol> <button onclick="quick();">快排</button> </div> <script> function quick() {
var a = [1,2,3,4,5,6,7,8,9,10], html = "", cnt = 0; for( var i = 0; i < 10; i++ ) { a[i] = randomNum(0, 100); } document.getElementById("original").innerText = "随机10位数组:" + a.toString().replace(/,/g, ', '); function randomNum(min, max) {
return min + Math.floor( Math.random() * (max-min) ); } function quickSort(a, b, e) {
if( e - b <= 1) return; var m = randomNum(b, e); var i = b, j = b + 1; var tmp = a[i]; a[i] = a[m]; a[m] = tmp; html += '<li><ol><li>第 ' + (i+1) + '(' + tmp + ')' + ' 与第 ' + (m+1) + '(' + a[i] + ')' + ' 交换,结果' + a.slice(b, e).toString().replace(/,/g, ', ') + '</li>'; while( i < e && j < e ) { console.log(++cnt); if( a[b] <= a[j] ) { j++; } if( a[b] > a[j] ) { tmp = a[j]; a[j++] = a[++i]; a[i] = tmp; html += '<li>第 ' + (j) + '(' + tmp + ')' + ' 与第 ' + (i+1) + '(' + a[j-1] + ')' + ' 交换,结果' + a.slice(b, e).toString().replace(/,/g, ', ') + '</li>'; } } tmp = a[b]; a[b] = a[i]; a[i] = tmp; html += '<li>第 ' + (b+1) + '(' + tmp + ')' + ' (随机数a['+ m + ']=' + tmp + ')与第 ' + (i+1) + '(' + a[b] + ')' + ' 交换,结果:' + a.slice(b, e).toString().replace(/,/g, ', ')+'</li></ol></li>'; quickSort(a, b, i); quickSort(a, i+1, e); } quickSort(a, 0, a.length); document.getElementById("result").innerHTML = html; console.log(a.toString()); } </script> </body> </html>
边栏推荐
- For some problems encountered in using crontab, errors are reported /var/spool/cron: permission denied and bash: /usr/bin/chattr: permission denied
- JS -- built in function of data system
- 【Missing Semester L5】命令行环境Command-line Environment(进程控制、tmux、别名、配置、SSH)
- Arthas introduction and idea plug-in quick start
- ES6 -- object
- JS time object
- Use tkmapper to add, delete, modify and query
- Notes 2 of network communication security
- redux 源码分析
- MySQL-约束
猜你喜欢

Intranet penetration notes - MSF

Network security emergency response terminal detection and response technology

错误监控原理解析

密态数字经济:用密码去“深海淘珍珠”
利用Spark预测回头客实验报告

Convert list data to tree data

ES6 -- object

Unity experiment - control the movement of game objects

Automatic deployment of server docking code cloud webhooks -- ultra detailed -- PHP

微信小程序开发二三事
随机推荐
Compilation principle - lexical analyzer design
unity实验-控制游戏物体运动
For some problems encountered in using crontab, errors are reported /var/spool/cron: permission denied and bash: /usr/bin/chattr: permission denied
《代码整洁之道》读后笔记
JS scope chain
利用RAC实现验证码发送逻辑
What is slow query? How to optimize?
微信小程序开发二三事
39.js-- scope
Get the set of parent node IDs from top to bottom
The technology once selected in the top meeting completed the commercialization of ant chain and launched the copyright AI computing engine
Some small problems encountered in using Baidu lightweight server LS
Expanding knowledge -- hijacking technology of JS
Converting string to date is convenient and fast
ES6 -- class
【MIT Missing Semester 2】Shell Tools
微服务架构九大特性
解读AFNetworking4.0请求原理
ES6 -- symbol() and map()
录音、上传、播放音频微信小程序实践