作者:微信小助手
<section style="box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"> <section style="padding-top: 10px;padding-right: 10px;padding-left: 10px;box-sizing: border-box;background-color: rgb(239, 239, 239);"> <span style="display: inline-block;width: 5%;line-height: 0.8;font-weight: bolder;font-size: 48px;box-sizing: border-box;"> <section style="box-sizing: border-box;"> “ </section></span> <section style="display: inline-block;vertical-align: top;float: right;width: 90%;line-height: 1.5;font-size: 15px;color: rgb(89, 89, 89);box-sizing: border-box;"> <p style="box-sizing: border-box;"><span style="letter-spacing: 1px;">何为锁?锁在现实中的意义为:封闭的器物,以钥匙或暗码开启。在计算机中的锁一般用来管理对共享资源的并发访问,比如我们 Java 同学熟悉的 Lock,synchronized 等都是我们常见的锁。</span></p> </section> <section style="clear: both;box-sizing: border-box;"></section> </section> </section> </section> </section> <h2 style="white-space: normal;line-height: 1.75em;"><br></h2> <p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 5px;"><img class="" data-copyright="0" data-ratio="0.6197387518142236" data-s="300,640" src="/upload/e99b87f1d08fa7b6262f92d7949aa8cd.png" data-type="png" data-w="689" style=""></p> <h2 style="white-space: normal;text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="line-height: 1.6;font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">当然在我们的数据库中也有锁用来控制资源的并发访问,这也是数据库和文件系统的区别之一。</span></h2> <p style="line-height: normal;"><br></p> <section style="box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: black;margin-top: 0.5em;margin-bottom: 0.5em;line-height: 1.2;box-sizing: border-box;"> <section style="display: inline-block;border-bottom-width: 6px;border-bottom-style: solid;border-color: rgb(89, 89, 89);margin-bottom: -1px;font-size: 20px;color: rgb(89, 89, 89);box-sizing: border-box;"> <p style="box-sizing: border-box;">为什么要懂数据库锁?</p> </section> </section> </section> </section> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">通常来说对于一般的开发人员,在使用数据库的时候一般懂点 DQL(select),DML(insert,update,delete)就够了。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">小明是一个刚刚毕业在互联网公司工作的 Java 开发工程师,平常的工作就是完成 PM 的需求。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">当然在完成需求的同时肯定逃脱不了 Spring,Spring MVC,Mybatis 的那一套框架,所以一般来说 SQL 还是自己手写,遇到比较复杂的 SQL 会从网上去百度一下。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">对于一些比较重要操作,比如交易啊这些,小明会用 Spring 的事务来对数据库的事务进行管理,由于数据量比较小目前还涉及不到分布式事务。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">前几个月小明过得都还风调雨顺,直到有一天,小明接了一个需求,商家有个配置项,叫优惠配置项,可以配置买一送一,买一送二等等规则。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">当然这些配置是批量传输给后端的,这样就有个问题每个规则都得去匹配,他到底是删除还是添加还是修改,这样后端逻辑就比较麻烦。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">聪明的小明想到了一个办法,直接删除这个商家的配置,然后全部添加进去。小明马上开发完毕,成功上线。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">开始上线没什么毛病,但是日志经常会出现一些 mysql-insert-deadlock 异常。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">由于小明经验比较浅,对于这类型的问题第一次遇见,于是去问了他们组的老司机大红。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">大红一看见这个问题,然后看了他的代码之后,输出了几个命令看了几个日志,马上定位了问题,告诉了小明:这是因为 delete 的时候会加间隙锁。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">但是间隙锁之间却可以兼容,但是插入新的数据的时候就会因为插入意向锁会被间隙锁阻塞,导致双方资源被互占,导致死锁。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">小明听了之后似懂非懂,由于大红的事情比较多,不方便一直麻烦大红,所以决定自己下来自己想。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">下班过后,小明回想大红说的话,什么是间隙锁,什么是插入意向锁,看来作为开发者对数据库不应该只会写 SQL 啊,不然遇到一些疑难杂症完全没法解决啊。想完,于是小明就踏上了学习 MySQL 锁这条不归之路。</span></p> <p style="line-height: normal;"><br></p> <section style="box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: black;margin-top: 0.5em;margin-bottom: 0.5em;line-height: 1.2;box-sizing: border-box;"> <section style="display: inline-block;border-bottom-width: 6px;border-bottom-style: solid;border-color: rgb(89, 89, 89);margin-bottom: -1px;font-size: 20px;color: rgb(89, 89, 89);box-sizing: border-box;"> <p style="box-sizing: border-box;">什么是 InnoDB?</p> </section> </section> </section> </section> <p style="line-height: normal;"><br></p> <section style="box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"> <section style="width: 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"> <span style="width: 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style="width: 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style="width: 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong> </section> <section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"> <p style="box-sizing: border-box;"><strong>MySQL 体系架构</strong></p> </section> </section> </section> </section> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">小明没有着急去了解锁这方面的知识,他首先先了解了下 </span><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">MySQL 体系架构:</span></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><img class="" data-ratio="0.7631103074141049" data-type="png" data-w="553" src="/upload/4289fb1925c4d24660ea00682bbf5b4f.png" style="box-sizing: border-box;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;word-wrap: break-word !important;width: 553px !important;visibility: visible !important;"></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">可以发现 MySQL 由连接池组件、管理服务和工具组件、SQL 接口组件、查询分析器组件、优化器组件、 缓冲组件、插件式存储引擎、物理文件组成。</span><br></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">小明发现在 <span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 26.25px;text-align: justify;">MySQL </span>中存储引擎是以插件的方式提供的,在 <span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 26.25px;text-align: justify;">MySQL</span> 中有多种存储引擎,每个存储引擎都有自己的特点。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">随后小明在命令行中打出了:</span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 0px;margin-bottom: 0px;padding: 0px;"><code class="hljs sql" style="margin-right: 2px;margin-left: 2px;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);padding: 0.5em;display: block !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;background: rgb(40, 43, 46);"><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">show</span> <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">engines</span> \G;<br></code></pre> </section> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">一看原来有这么多种引擎。<span style="letter-spacing: 1px;color: rgb(89, 89, 89);font-size: 15px;line-height: 1.6;">又打出了下面的命令,查看当前数据库默认的引擎:</span></span></p> <section class="output_wrapper" style="font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;"> <pre style="font-size: inherit;color: inherit;line-height: inherit;margin-top: 0px;margin-bottom: 0px;padding: 0px;"><code class="hljs sql" style="margin-right: 2px;margin-left: 2px;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);padding: 0.5em;display: block !important;word-wrap: normal !important;word-break: normal !important;overflow: auto !important;background: rgb(40, 43, 46);"><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">show</span> <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">variables</span> <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);word-wrap: inherit !important;word-break: inherit !important;">like</span> <span class="hljs-string" style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);word-wrap: inherit !important;word-break: inherit !important;">'%storage_engine%'</span>;<br></code></pre> </section> <p style="line-height: normal;"><br></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.5019011406844106" data-type="png" data-w="789" src="/upload/324b043de0bed32eb5d751a5ff7117c0.png" style="box-sizing: border-box;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;word-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">小明恍然大悟:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">原来自己的数据库是使用的 InnoDB,依稀记得自己在上学的时候好像听说过有个引擎叫 MyIsAM,小明想这两个有啥不同呢?</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">马上查找了一下资料:</span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 5px;"><img class="" data-copyright="0" data-ratio="0.40700808625336926" data-s="300,640" src="/upload/e82e61bb4bea9604445ffbdde808ecc9.png" data-type="png" data-w="742" style=""></p> <p style="margin-left: 8px;margin-right: 8px;"><span style="font-size: 15px;color: rgb(89, 89, 89);">小明大概了解了一下 InnoDB 和 MyIsAM 的区别,由于使用的是 InnoDB,小明就没有过多的纠结这一块。</span><br></p> <p style="line-height: normal;"><br></p> <section style="box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"> <section style="width: 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"> <span style="width: 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style="width: 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style="width: 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong> </section> <section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"> <p style="box-sizing: border-box;"><strong>事务的隔离性</strong></p> </section> </section> </section> </section> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">小明在研究锁之前,又回想到之前上学的时候教过的数据库事务隔离性,其实锁在数据库中其功能之一也是用来实现事务隔离性。</span><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">而事务的隔离性其实是用来解决脏读,不可重复读,幻读几类问题。</span></p> <p style="line-height: normal;"><br></p> <h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;">脏读</span></strong></span></h3> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">一个事务读取到另一个事务未提交的更新数据。什么意思呢?</span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 5px;"><img class="" data-copyright="0" data-ratio="0.363754889178618" data-s="300,640" src="/upload/e1a0ced2b71ffc4cdfb0cb6b1c6ad35f.png" data-type="png" data-w="767" style=""></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">在事务 A,B 中,事务 A 在时间点 2,4 分别对 user 表中 id = 1 的数据进行了查询。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">但是事务 B 在时间点 3 进行了修改,导致了事务 A 在 4 中的查询出的结果其实是事务 B 修改后的。这样就破坏了数据库中的隔离性。</span></p> <p style="line-height: normal;"><br></p> <h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;">不可重复读</span></strong></span></h3> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">在同一个事务中,多次读取同一数据返回的结果不同,不可重复读和脏读不同的是这里读取的是已经提交过后的数据。</span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 5px;"><img class="" data-copyright="0" data-ratio="0.4119170984455959" data-s="300,640" src="/upload/ac4b875b709399b42d6fa9e121a6d03a.png" data-type="png" data-w="772" style=""></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">在事务 B 中提交的操作在事务 A 第二次查询之前,但是依然读到了事务 B 的更新结果,也破坏了事务的隔离性。</span></p> <h3 style="line-height: normal;"><br></h3> <h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;">幻读</span></strong></span></h3> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">一个事务读到另一个事务已提交的 insert 数据。</span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 5px;"><img class="" data-copyright="0" data-ratio="0.4113573407202216" data-s="300,640" src="/upload/cae393aaab0271fb74d94f6bd8eafca7.png" data-type="png" data-w="722" style=""></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">在事务 A 中查询了两次 id 大于 1 的,在第一次 id 大于 1 查询结果中没有数据,但是由于事务 B 插入了一条 id = 2 的数据,导致事务 A 第二次查询时能查到事务 B 中插入的数据。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">事务中的隔离性:</span></p> <p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 5px;"><img class="" data-copyright="0" data-ratio="0.3127690100430416" data-s="300,640" src="/upload/d73343e23099319f5236a86819df5ef8.png" data-type="png" data-w="697" style=""></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">小明注意到在收集资料的过程中,有资料写到 InnoDB 和其他数据库有点不同,InnoDB 的可重复读其实就能解决幻读了,小明心想:这 InnoDB 还挺牛逼的,我得好好看看到底是怎么个原理。</span></p> <p style="line-height: normal;"><br></p> <section style="box-sizing: border-box;"> <section class="V5" style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"> <section style="width: 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"> <span style="width: 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style="width: 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style="width: 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong> </section> <section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"> <p style="box-sizing: border-box;"><strong>InnoDB 锁类型</strong></p> </section> </section> </section> </section> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">小明首先了解了 MySQL 中常见的锁类型有哪些:</span></p> <p style="line-height: normal;"><br></p> <h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;">S or X</span></strong></span></h3> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">在 InnoDB 中实现了两个标准的行级锁,可以简单的看为两个读写锁:</span></p> <ul class=" list-paddingleft-2" style="list-style-type: square;margin-left: 8px;margin-right: 8px;"> <li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">S 共享锁:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">又叫读锁,其他事务可以继续加共享锁,但是不能继续加排他锁。</span></p></li> <li><p style="text-align: justify;line-height: 1.75em;margin-bottom: 5px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">X 排他锁:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">又叫写锁,一旦加了写锁之后,其他事务就不能加锁了。</span></p></li> </ul> <p style="line-height: normal;text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 5px;"><img class="" data-copyright="0" data-ratio="0.21084337349397592" data-s="300,640" src="/upload/74fb2a185207a645856512187ce0c1dc.png" data-type="png" data-w="664" style="line-height: 25.6px;text-align: center;white-space: normal;"></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">兼容性:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">是指事务 A 获得一个某行某种锁之后,事务 B 同样的在这个行上尝试获取某种锁,如果能立即获取,则称锁兼容,反之叫冲突。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">纵轴是代表已有的锁,横轴是代表尝试获取的锁。</span></p> <p style="line-height: normal;"><br></p> <h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;">意向锁</span></strong></span></h3> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">意向锁在 InnoDB 中是表级锁,和它的名字一样它是用来表达一个事务想要获取什么。</span></p> <p style="line-height: normal;"><br></p> <p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">意向锁分为:</
作者:じ☆ve宝贝
** 最新tomcat http强制跳转https [最新tomcat http强制跳转https](https://www.studyjava.cn/post/750),支持tomcat配置301永久重定向和302临时重定向。匹配百度站长全站开启https认证 ** 1.编辑tomcat/conf/server.xml找到关于SSL的字段 ``` <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="D:\\home\\tomcat.keystore" keystorePass="123456" truststoreFile="D:\\home\\tomcat.keystore" truststorePass="123456" /> ``` (tomcat要与生成的服务端证书名一致) 属性说明: clientAuth:设置是否双向验证,默认为false,设置为true代表双向验证 keystoreFile:服务器证书文件路径 keystorePass:服务器证书密码 truststoreFile:用来验证客户端证书的根证书,此例中就是服务器证书 truststorePass:根证书密码 2.编辑 tomcat/conf/web.xml在</welcome- file-list>添加: ``` <login-config> <!-- Authorization setting for SSL --> <auth-method>CLIENT-CERT</auth-method> <realm-name>Client Cert Users-only Area</realm-name> </login-config> <security-constraint> <!-- Authorization setting for SSL --> <web-resource-collection > <web-resource-name >SSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> ```
作者:じ☆ve宝贝
## 存储过程: ``` DELIMITER $$ DROP PROCEDURE IF EXISTS `school`$$ CREATE PROCEDURE `school`() BEGIN DECLARE v_cnt DECIMAL (10) DEFAULT 0 ; DECLARE id DECIMAL (10) DEFAULT 0 ; dd:LOOP INSERT INTO user_school VALUES (NULL,id+1,'清华'), (NULL,id+2,'北大'), (NULL,id+3,'北航'), (NULL,id+4,'北电'), (NULL,id+5,'北邮'), (NULL,id+6,'华南理工'), (NULL,id+7,'花呗理工'), (NULL,id+8,'浙大'), (NULL,id+9,'汉大帮'), (NULL,id+10,'秘书帮') ; COMMIT; SET v_cnt = v_cnt+10 ; IF v_cnt = 10000 THEN LEAVE dd; END IF; SET id = id+10; END LOOP dd ; END$$ DELIMITER ; ``` 注意 IF v_cnt = 10000(插入条数,此处为10000条) THEN LEAVE dd; ``` 执行存储过程:call school ```
作者:じ☆ve宝贝
**MySQL导入文本数据LOAD DATA INFILE语法** ``` LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_name [PARTITION (partition_name [, partition_name] ...)] [CHARACTER SET charset_name] [{FIELDS | COLUMNS} [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char'] ] [LINES [STARTING BY 'string'] [TERMINATED BY 'string'] ] [IGNORE number {LINES | ROWS}] [(col_name_or_user_var [, col_name_or_user_var] ...)] [SET col_name={expr | DEFAULT}, [, col_name={expr | DEFAULT}] ...] ``` ** FIELDS参数含义:** TERMINATED BY ',' //描述字段的分隔符,默认情况下是tab字符(\\t) ENCLOSED BY '"' //描述的是字段的括起字符,比方以双引号括起每一个字段 ESCAPED BY '\\' //描述的转义字符,默认的是反些杠(backslash:\\ ) ** LINES参数含义:** TERMINATED BY '\n' // 描述行分隔符 STARTING BY '' // 行前缀,只加载该前缀开头的数据 实例: ``` mysql>LOAD DATA INFILE "${value}" INTO TABLE databases.table_name; ``` 注意${value}值查找方法[查找LOAD DATA INFILE文件存放目录](http://www.studyjava.cn/topic/view/a9f55279842f4918896f4831c1cb9cf8.html) ``` //LOCAL参数表示文件是本地的文件,服务器是远程已经登陆的服务器 mysql>LOAD DATA LOCAL INFILE "${value}" INTO TABLE databases.table_name; ``` ``` //设置插入语句的优先级(LOW_PRIORITY 低优先级),LOAD DATA语句的执行将会被延迟,直到没有其它的客户端读取表 mysql>LOAD DATA LOW_PRIORITY INFILE "${value}" INTO TABLE databases.table_name; ``` ``` //插入数据的时候,忽略文件与数据表中重复的键值 mysql>LOAD DATA LOW_PRIORITY INFILE "${value}" IGNORE INTO TABLE databases.table_name; ``` ``` //插入数据的时候,替代文件与数据表中重复的键值 mysql>LOAD DATA LOW_PRIORITY INFILE "${value}" REPLACE INTO TABLE databases.table_name; ``` ``` //字段以逗号分割,字段内容用双引号标记开始和结束 mysql>LOAD DATA INFILE "${value}" INTO TABLE databases.table_name FIELDS TERMINATED BY ',' ENCLOSED BY '"'; ``` ``` //按指定的列把文件导入到数据库中 mysql>LOAD DATA INFILE "${value}" INTO TABLE databases.table_name (column1, column2, column3); ``` ## 测试: **导出数据到文件:** ``` SELECT * INTO OUTFILE "C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/test.sql" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY'"' LINES TERMINATED BY '\n' FROM user ; ``` **从文件中加载数据** ``` LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/test.sql' REPLACE INTO TABLE test.ouser FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' ; ```
作者:じ☆ve宝贝
> 默认配置下,thymeleaf对.html的内容要求很严格,比如,如果少封闭符号/,就会报错而转到错误页。也比如你在使用Vue.js这样的库,然后有<div v-cloak></div>这样的html代码,也会被thymeleaf认为不符合要求而抛出错误。 #### 通过设置thymeleaf模板可以解决这个问题,下面是具体的配置: **properties中如下** ``` spring.thymeleaf.cache=false spring.thymeleaf.mode=LEGACYHTML5 ``` **ymal中如下** ``` spring: thymeleaf: cache: false mode: LEGACYHTML5 ``` LEGACYHTML5需要搭配一个额外的库NekoHTML才可用 项目中使用的构建工具是Maven添加如下的依赖即可完成: ``` <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency> ```
作者:じ☆ve宝贝
## jdk配置  ## Maven配置  ## Git配置 [idea使用GIT设置](https://www.studyjava.cn/topic/view/ed32ed164e224e2997575a0d1ff0a482.html) ## 全局设置编码UTF-8  建议将底部的 Transparent native-to-ASCII conversion 也勾选上,有Unicode显示的就可以自动转化为其应该显示的语言了. ## 设置显示行号 
作者:じ☆ve宝贝
my_common.lua ``` -- Copyright (C) 2006-2017 Alexey Kopytov <akopytov@gmail.com> -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -- ----------------------------------------------------------------------------- -- Common code for OLTP benchmarks. -- ----------------------------------------------------------------------------- function init() assert(event ~= nil, "this script is meant to be included by other OLTP scripts and " .. "should not be called directly.") end if sysbench.cmdline.command == nil then error("Command is required. Supported commands: prepare, prewarm, run, " .. "cleanup, help") end -- Command line options sysbench.cmdline.options = { table_size = {"Number of rows per table", 10000}, range_size = {"Range size for range SELECT queries", 100}, tables = {"Number of tables", 1}, point_selects = {"Number of point SELECT queries per transaction", 10}, simple_ranges = {"Number of simple range SELECT queries per transaction", 1}, sum_ranges = {"Number of SELECT SUM() queries per transaction", 1}, order_ranges = {"Number of SELECT ORDER BY queries per transaction", 1}, distinct_ranges = {"Number of SELECT DISTINCT queries per transaction", 1}, index_updates = {"Number of UPDATE index queries per transaction", 1}, non_index_updates = {"Number of UPDATE non-index queries per transaction", 1}, delete_inserts = {"Number of DELETE/INSERT combination per transaction", 1}, range_selects = {"Enable/disable all range SELECT queries", true}, auto_inc = {"Use AUTO_INCREMENT column as Primary Key (for MySQL), " .. "or its alternatives in other DBMS. When disabled, use " .. "client-generated IDs", true}, skip_trx = {"Don't start explicit transactions and execute all queries " .. "in the AUTOCOMMIT mode", false}, secondary = {"Use a secondary index in place of the PRIMARY KEY", false}, create_secondary = {"Create a secondary index in addition to the PRIMARY KEY", true}, mysql_storage_engine = {"Storage engine, if MySQL is used", "myisam"}, pgsql_variant = {"Use this PostgreSQL variant when running with the " .. "PostgreSQL driver. The only currently supported " .. "variant is 'redshift'. When enabled, " .. "create_secondary is automatically disabled, and " .. "delete_inserts is set to 0"} } -- Prepare the dataset. This command supports parallel execution, i.e. will -- benefit from executing with --threads > 1 as long as --tables > 1 function cmd_prepare() local drv = sysbench.sql.driver() local con = drv:connect() for i = sysbench.tid % sysbench.opt.threads + 1, sysbench.opt.tables, sysbench.opt.threads do create_table(drv, con, i) end end -- Preload the dataset into the server cache. This command supports parallel -- execution, i.e. will benefit from executing with --threads > 1 as long as -- --tables > 1 -- -- PS. Currently, this command is only meaningful for MySQL/InnoDB benchmarks function cmd_prewarm() local drv = sysbench.sql.driver() local con = drv:connect() assert(drv:name() == "mysql", "prewarm is currently MySQL only") -- Do not create on disk tables for subsequent queries con:query("SET tmp_table_size=2*1024*1024*1024") con:query("SET max_heap_table_size=2*1024*1024*1024") for i = sysbench.tid % sysbench.opt.threads + 1, sysbench.opt.tables, sysbench.opt.threads do local t = "sbtest" .. i print("Prewarming table " .. t) con:query("ANALYZE TABLE sbtest" .. i) con:query(string.format( "SELECT AVG(id) FROM " .. "(SELECT * FROM %s FORCE KEY (PRIMARY) " .. "LIMIT %u) t", t, sysbench.opt.table_size)) con:query(string.format( "SELECT COUNT(*) FROM " .. "(SELECT * FROM %s WHERE cn_name LIKE '%%0%%' LIMIT %u) t", t, sysbench.opt.table_size)) end end -- Implement parallel prepare and prewarm commands sysbench.cmdline.commands = { prepare = {cmd_prepare, sysbench.cmdline.PARALLEL_COMMAND}, prewarm = {cmd_prewarm, sysbench.cmdline.PARALLEL_COMMAND} } -- Template strings of random digits with 11-digit groups separated by dashes -- 10 groups, 119 characters local c_value_template = "###########-###########-###########-" .. "###########-###########-###########-" .. "###########-###########-###########-" .. "###########" -- 5 groups, 59 characters local pad_value_template = "###########-###########-###########-" .. "###########-###########" function get_c_value() return sysbench.rand.string(c_value_template) end function get_pad_value() return sysbench.rand.string(pad_value_template) end function create_table(drv, con, table_num) local id_index_def, id_def local engine_def = "" local extra_table_options = "" local query if sysbench.opt.secondary then id_index_def = "KEY xid" else id_index_def = "PRIMARY KEY" end if drv:name() == "mysql" or drv:name() == "attachsql" or drv:name() == "drizzle" then if sysbench.opt.auto_inc then id_def = "INTEGER NOT NULL AUTO_INCREMENT" else id_def = "INTEGER NOT NULL" end engine_def = "/*! ENGINE = " .. sysbench.opt.mysql_storage_engine .. " */" extra_table_options = mysql_table_options or "" elseif drv:name() == "pgsql" then if not sysbench.opt.auto_inc then id_def = "INTEGER NOT NULL" elseif pgsql_variant == 'redshift' then id_def = "INTEGER IDENTITY(1,1)" else id_def = "SERIAL" end else error("Unsupported database driver:" .. drv:name()) end print(string.format("Creating table 'sbtest%d'...", table_num)) query = string.format([[ CREATE TABLE sbtest%d( `id` char(100) DEFAULT NULL, `cn_name` varchar(200) DEFAULT NULL COMMENT '中文名', `cn_short` varchar(200) DEFAULT NULL COMMENT '中文简称', `en_name` varchar(200) DEFAULT NULL COMMENT '英文名', `en_short` varchar(200) DEFAULT NULL COMMENT '英文简称', `cn_once` varchar(200) DEFAULT NULL COMMENT '曾用名', `credit_code` varchar(200) DEFAULT NULL COMMENT '统一信用代码', `reg_institute` varchar(200) DEFAULT NULL COMMENT '登记机关', `scale` varchar(200) DEFAULT NULL COMMENT '公司规模', `organize_code` varchar(200) DEFAULT NULL COMMENT '企业注册码', `registration_number` varchar(200) DEFAULT NULL COMMENT '工商注册号', `legal_person` varchar(200) DEFAULT NULL COMMENT '法人', `company_type` varchar(200) DEFAULT NULL COMMENT '企业控股: 1:', `register_type` varchar(200) DEFAULT NULL COMMENT '企业注册类型 ', `estiblish_time` varchar(200) DEFAULT NULL COMMENT '营业期限开始', `to_time` varchar(200) DEFAULT NULL COMMENT '营业期限截止', `check_date` varchar(200) DEFAULT NULL COMMENT '发照日期', `area_type` varchar(200) DEFAULT NULL COMMENT '地区', `reg_location` varchar(200) DEFAULT NULL COMMENT '企业地址 ', `business_scope` varchar(200) DEFAULT NULL COMMENT '经营范围', `web_url` varchar(200) DEFAULT NULL COMMENT '网址', `company_logo` varchar(200) DEFAULT NULL COMMENT 'logo', `cn_desc` varchar(200) DEFAULT NULL COMMENT '中文简介', `en_desc` varchar(200) DEFAULT NULL COMMENT '中文简介', `begin_time` varchar(200) DEFAULT NULL COMMENT '成立时间', `stop_time` varchar(200) DEFAULT NULL COMMENT '停业时间', `registered_capital` varchar(200) DEFAULT NULL COMMENT '注册资本', `currency_type` varchar(200) DEFAULT NULL COMMENT '注册资本货币类型 ', `commerce_status` varchar(200) DEFAULT NULL COMMENT '商业状态', `remark` varchar(510) DEFAULT NULL, `creater_id` varchar(200) DEFAULT NULL COMMENT '修改人', `create_time` varchar(200) DEFAULT NULL COMMENT '创建时间', `status` varchar(200) DEFAULT NULL COMMENT '1:正常 2删除 3...', `modifier_id` varchar(200) DEFAULT NULL COMMENT '修改人', `modify_time` varchar(200) DEFAULT NULL COMMENT '修改时间', PRIMARY KEY (id) ) %s %s]], table_num, engine_def, extra_table_options) con:query(query) if (sysbench.opt.table_size > 0) then print(string.format("Inserting %d records into 'sbtest%d'", sysbench.opt.table_size, table_num)) end if sysbench.opt.auto_inc then query = "INSERT INTO sbtest" .. table_num .. "(id, cn_name, cn_short, en_name, en_short, cn_once, credit_code, reg_institute, scale, organize_code, registration_number, legal_person, company_type, register_type, estiblish_time, to_time, check_date, area_type, reg_location, business_scope, web_url, company_logo, cn_desc, en_desc, begin_time, stop_time, registered_capital, currency_type, commerce_status, remark, creater_id, create_time, status, modifier_id, modify_time) VALUES" else query = "INSERT INTO sbtest" .. table_num .. "(id, cn_name, cn_short, en_name, en_short, cn_once, credit_code, reg_institute, scale, organize_code, registration_number, legal_person, company_type, register_type, estiblish_time, to_time, check_date, area_type, reg_location, business_scope, web_url, company_logo, cn_desc, en_desc, begin_time, stop_time, registered_capital, currency_type, commerce_status, remark, creater_id, create_time, status, modifier_id, modify_time) VALUES" end con:bulk_insert_init(query) local c_val local pad_val for i = 1, sysbench.opt.table_size do c_val = get_c_value() pad_val = get_pad_value() if (sysbench.opt.auto_inc) then query = string.format("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size), c_val,get_pad_value(),getTimeStamp(),get_pad_value(),getTimeStamp(),get_pad_value(),getTimeStamp(), pad_val,guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size),guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size),get_pad_value(),get_pad_value(),getTimeStamp()) else query = string.format("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size), c_val,get_pad_value(),getTimeStamp(),get_pad_value(),getTimeStamp(),get_pad_value(),getTimeStamp(), pad_val,guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size),guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size),get_pad_value(),get_pad_value(),getTimeStamp()) end con:bulk_insert_next(query) end con:bulk_insert_done() if sysbench.opt.create_secondary then print(string.format("Creating a secondary index on 'sbtest%d'...", table_num)) con:query(string.format("CREATE INDEX k_%d ON sbtest%d(cn_name)", table_num, table_num)) end end local t = sysbench.sql.type local stmt_defs = { point_selects = { "SELECT cn_name FROM sbtest%u WHERE estiblish_time=?", {t.VARCHAR, 100}}, simple_ranges = { "SELECT cn_name FROM sbtest%u WHERE estiblish_time BETWEEN ? AND ?", {t.VARCHAR, 100}, {t.VARCHAR, 100}}, sum_ranges = { "SELECT SUM(cn_short) FROM sbtest%u WHERE estiblish_time BETWEEN ? AND ?", {t.VARCHAR, 100}, {t.VARCHAR, 100}}, order_ranges = { "SELECT cn_name FROM sbtest%u WHERE estiblish_time BETWEEN ? AND ? ORDER BY cn_name", {t.VARCHAR, 100}, {t.VARCHAR, 100}}, distinct_ranges = { "SELECT DISTINCT cn_name FROM sbtest%u WHERE estiblish_time BETWEEN ? AND ? ORDER BY cn_name", {t.VARCHAR, 100}, {t.VARCHAR, 100}}, index_updates = { "UPDATE sbtest%u SET en_name='test' WHERE estiblish_time=?", {t.VARCHAR, 100}}, non_index_updates = { "UPDATE sbtest%u SET cn_name='non_index_update' WHERE estiblish_time=?", {t.VARCHAR, 100}}, deletes = { "DELETE FROM sbtest%u WHERE estiblish_time=?", {t.VARCHAR, 100}}, inserts = { "INSERT INTO sbtest%u (id, cn_name, cn_short, en_name, estiblish_time) VALUES (?, ?, ?, ?, ?)", {t.VARCHAR, 100}, {t.VARCHAR, 200}, {t.VARCHAR, 200}, {t.VARCHAR, 200}, {t.VARCHAR, 200}}, } function prepare_begin() stmt.begin = con:prepare("BEGIN") end function prepare_commit() stmt.commit = con:prepare("COMMIT") end function prepare_for_each_table(key) for t = 1, sysbench.opt.tables do stmt[t][key] = con:prepare(string.format(stmt_defs[key][1], t)) -- print(string.format(stmt_defs[key][1], t)) local nparam = #stmt_defs[key] - 1 if nparam > 0 then param[t][key] = {} end -- print(string.format('----byte,typeby=%s,len=%s,key=%s',type(btype),len,key)) for p = 1, nparam do local btype = stmt_defs[key][p+1] local len if type(btype) == "table" then len = btype[2] btype = btype[1] end -- print(string.format('%s,%s---', btype,sysbench.sql.type.VARCHAR)) if btype == sysbench.sql.type.VARCHAR or btype == sysbench.sql.type.CHAR then -- print(string.format('----cahr,typeby=%s,key=%s,btype=%s,len=%s',type(btype),key,btype,len)) param[t][key][p] = stmt[t][key]:bind_create(btype, len) else -- print(string.format('----nochar,typeby=%s,key=%s,btype=%s',type(btype),key,btype)) param[t][key][p] = stmt[t][key]:bind_create(btype) end end if nparam > 0 then stmt[t][key]:bind_param(unpack(param[t][key])) end end end function prepare_point_selects() prepare_for_each_table("point_selects") end function prepare_simple_ranges() prepare_for_each_table("simple_ranges") end function prepare_sum_ranges() prepare_for_each_table("sum_ranges") end function prepare_order_ranges() prepare_for_each_table("order_ranges") end function prepare_distinct_ranges() prepare_for_each_table("distinct_ranges") end function prepare_index_updates() prepare_for_each_table("index_updates") end function prepare_non_index_updates() prepare_for_each_table("non_index_updates") end function prepare_delete_inserts() prepare_for_each_table("deletes") prepare_for_each_table("inserts") end function thread_init() drv = sysbench.sql.driver() con = drv:connect() -- Create global nested tables for prepared statements and their -- parameters. We need a statement and a parameter set for each combination -- of connection/table/query stmt = {} param = {} for t = 1, sysbench.opt.tables do stmt[t] = {} param[t] = {} end -- This function is a 'callback' defined by individual benchmark scripts prepare_statements() end function thread_done() -- Close prepared statements for t = 1, sysbench.opt.tables do for k, s in pairs(stmt[t]) do stmt[t][k]:close() end end if (stmt.begin ~= nil) then stmt.begin:close() end if (stmt.commit ~= nil) then stmt.commit:close() end con:disconnect() end function cleanup() local drv = sysbench.sql.driver() local con = drv:connect() for i = 1, sysbench.opt.tables do print(string.format("Dropping table 'sbtest%d'...", i)) con:query("DROP TABLE IF EXISTS sbtest" .. i ) end end local function get_table_num() return sysbench.rand.uniform(1, sysbench.opt.tables) end local function get_id() return string.format(sysbench.rand.default(1, sysbench.opt.table_size)) end function begin() for t = 1, sysbench.opt.tables do for k, s in pairs(stmt[t]) do -- print(k,s) end end stmt.begin:execute() end function commit() stmt.commit:execute() end function execute_point_selects() local tnum = get_table_num() local i for i = 1, sysbench.opt.point_selects do param[tnum].point_selects[1]:set(get_id()) stmt[tnum].point_selects:execute() end end local function execute_range(key) local tnum = get_table_num() for i = 1, sysbench.opt[key] do local id = get_id() param[tnum][key][1]:set(id) param[tnum][key][2]:set(guid()) stmt[tnum][key]:execute() end end function execute_simple_ranges() execute_range("simple_ranges") end function execute_sum_ranges() execute_range("sum_ranges") end function execute_order_ranges() execute_range("order_ranges") end function execute_distinct_ranges() execute_range("distinct_ranges") end function execute_index_updates() local tnum = get_table_num() for i = 1, sysbench.opt.index_updates do param[tnum].index_updates[1]:set(get_id()) stmt[tnum].index_updates:execute() end end function execute_non_index_updates() local tnum = get_table_num() for i = 1, sysbench.opt.non_index_updates do -- param[tnum].non_index_updates[1]:set_rand_str(c_value_template) -- param[tnum].non_index_updates[2]:set(guid()) param[tnum].non_index_updates[1]:set(get_id()) stmt[tnum].non_index_updates:execute() end end function execute_delete_inserts() local tnum = get_table_num() for i = 1, sysbench.opt.delete_inserts do -- print(i) local id = get_id() local k = get_id() local uid = get_id(); param[tnum].deletes[1]:set(uid) param[tnum].inserts[1]:set(uid) param[tnum].inserts[2]:set(k) param[tnum].inserts[3]:set_rand_str(c_value_template) param[tnum].inserts[4]:set_rand_str(pad_value_template) param[tnum].inserts[5]:set(uid) stmt[tnum].deletes:execute() stmt[tnum].inserts:execute() end end function guid() local seed = { '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z' } local sid = "" for i=1, 32 do -- math.randomseed(tostring(os.time()):reverse():sub(1, 6)) sid = sid .. seed[random()] end return string.format('%s%s%s%s%s', string.sub(sid, 1, 8), string.sub(sid, 9, 12), string.sub(sid, 13, 16), string.sub(sid, 17, 20), string.sub(sid, 21, 32) ) end function random() local ret=0 math.randomseed(os.time()) for i=1,3 do n = math.random(1,36) ret=n end return ret end function getTimeStamp() local t = os.time(); return os.date("%Y-%m-%d %H:%M:%S",t/1000) end ``` ## my_read_write.lua ``` #!/usr/bin/env sysbench -- Copyright (C) 2006-2017 Alexey Kopytov <akopytov@gmail.com> -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -- ---------------------------------------------------------------------- -- Read/Write OLTP benchmark -- ---------------------------------------------------------------------- require("my_common") function prepare_statements() if not sysbench.opt.skip_trx then prepare_begin() prepare_commit() end prepare_point_selects() if sysbench.opt.range_selects then prepare_simple_ranges() prepare_sum_ranges() prepare_order_ranges() prepare_distinct_ranges() end prepare_index_updates() prepare_non_index_updates() prepare_delete_inserts() end function event() if not sysbench.opt.skip_trx then begin() end execute_point_selects() if sysbench.opt.range_selects then execute_simple_ranges() execute_sum_ranges() execute_order_ranges() execute_distinct_ranges() end execute_index_updates() execute_non_index_updates() execute_delete_inserts() if not sysbench.opt.skip_trx then commit() end end ``` ## company_read_write.sh 执行脚本 ``` #!/bin/bash read -t 30 -p "请输入运行方式(prepare/run/cleanup)": status sysbench /data/lua/my_read_write.lua \ --mysql-host=127.0.0.1 \ --mysql-port=3306 \ --mysql-user=root \ --mysql-password='V5kNP47u28450Gpt' \ --mysql-db=sbtest \ --db-driver=mysql \ --tables=1 \ --table-size=1000000 \ --report-interval=10 \ --threads=128 \ --time=120 \ $status ```
作者:微信小助手
<section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"></section> <section data-role="outer" label="Powered by 135editor.com" data-mpa-powered-by="yiban.io"> <section class="_135editor" data-tools="135编辑器" data-id="91525"> <section> <section> <p>泛型,一个孤独的守门者。</p> <p><br></p> </section> </section> </section> </section> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">大家可能会有疑问,我为什么叫做泛型是一个守门者。这其实是我个人的看法而已,我的意思是说泛型没有其看起来那么深不可测,它并不神秘与神奇。泛型是 Java 中一个很小巧的概念,但同时也是一个很容易让人迷惑的知识点,它让人迷惑的地方在于它的许多表现有点违反直觉。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">文章开始的地方,先给大家奉上一道经典的测试题。</p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">List</span><String> l1 = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> ArrayList<String>();<br><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">List</span><Integer> l2 = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> ArrayList<Integer>();<br><br>System.out.println(l1.getClass() == l2.getClass());</code></pre> </section> <p><br></p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">请问,上面代码最终结果输出的是什么?不了解泛型的和很熟悉泛型的同学应该能够答出来,而对泛型有所了解,但是了解不深入的同学可能会答错。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">正确答案是 true。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">上面的代码中涉及到了泛型,而输出的结果缘由是<span style="outline: 0px;font-weight: 700;word-break: break-all;">类型擦除</span>。先好好说说泛型。</p> <h1 style="outline: 0px;margin-top: 8px;margin-bottom: 16px;font-size: 28px;color: rgb(79, 79, 79);font-weight: 700;line-height: 36px;word-break: break-all;">泛型是什么?</h1> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">泛型的英文是 generics,generic 的意思是通用,而翻译成中文,泛应该意为广泛,型是类型。所以泛型就是能广泛适用的类型。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;"><span style="outline: 0px;font-weight: 700;word-break: break-all;">但泛型还有一种较为准确的说法就是为了参数化类型,或者说可以将类型当作参数传递给一个类或者是方法。</span></p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">那么,如何解释类型参数化呢?</p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">class</span> <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span> {<br> Object <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span>;<br><br> <span class="hljs-default-function" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 175px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> Object <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 56px;text-decoration: none;font-weight: 400;font-style: normal;">getValue</span>(<span class="hljs-default-params" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 0px;text-decoration: none;font-weight: 400;font-style: normal;"></span>) </span>{<br> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">return</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span>;<br> }<br><br> <span class="hljs-default-function" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 245px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">void</span> <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 56px;text-decoration: none;font-weight: 400;font-style: normal;">setValue</span>(<span class="hljs-default-params" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 84px;text-decoration: none;font-weight: 400;font-style: normal;">Object <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span></span>) </span>{<br> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">this</span>.<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span> = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span>;<br> }<br><br>}</code></pre> </section> <p><br></p> <p><span style="color: rgb(79, 79, 79);">假设 Cache 能够存取任何类型的值,于是,我们可以这样使用它。</span></p> <p><span style="color: rgb(79, 79, 79);"></span></p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">cache</span> = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span>();<br>cache.setValue(<span class="hljs-default-number" style="color: rgb(209, 154, 102);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">134</span>);<br>int value = (int) cache.getValue();<br>cache.setValue(<span class="hljs-default-string" style="color: rgb(152, 195, 121);background: rgba(0, 0, 0, 0);display: inline;width: 49px;text-decoration: none;font-weight: 400;font-style: normal;">"hello"</span>);<br>String value1 = (String) cache.getValue();</code></pre> </section> <p><span style="color: rgb(79, 79, 79);"></span><br></p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">使用的方法也很简单,只要我们做正确的强制转换就好了。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">但是,泛型却给我们带来了不一样的编程体验。</p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> <span class="hljs-default-class" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 112px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">class</span> <span class="hljs-default-title" style="color: rgb(230, 192, 123);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span><T> {</span><br> T value;<br><br> <span class="hljs-default-function" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 175px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> Object <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 56px;text-decoration: none;font-weight: 400;font-style: normal;">getValue</span><span class="hljs-default-params" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 14px;text-decoration: none;font-weight: 400;font-style: normal;">()</span> </span>{<br> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">return</span> value;<br> }<br><br> <span class="hljs-default-function" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 210px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">void</span> <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 56px;text-decoration: none;font-weight: 400;font-style: normal;">setValue</span><span class="hljs-default-params" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 63px;text-decoration: none;font-weight: 400;font-style: normal;">(T value)</span> </span>{<br> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">this</span>.value = value;<br> }<br><br>}</code></pre> </section> <p><br></p> <p><span style="color: rgb(79, 79, 79);">这就是泛型,它将 value 这个属性的类型也参数化了,这就是所谓的参数化类型。再看它的使用方法。</span></p> <p><span style="color: rgb(79, 79, 79);"></span></p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span><<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">String</span>> cache1 = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span><<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">String</span>>();<br>cache1.setValue("123");<br>String value2 = cache1.getValue();<br><br><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span><<span class="hljs-default-built_in" style="color: rgb(230, 192, 123);background: rgba(0, 0, 0, 0);display: inline;width: 49px;text-decoration: none;font-weight: 400;font-style: normal;">Integer</span>> cache2 = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span><<span class="hljs-default-built_in" style="color: rgb(230, 192, 123);background: rgba(0, 0, 0, 0);display: inline;width: 49px;text-decoration: none;font-weight: 400;font-style: normal;">Integer</span>>();<br>cache2.setValue(456);<br>int value3 = cache2.getValue();</code></pre> </section> <p><span style="color: rgb(79, 79, 79);"></span><br></p> <p><span style="color: rgb(79, 79, 79);">最显而易见的好处就是它不再需要对取出来的结果进行强制转换了。但,还有另外一点不同。 </span></p> <p><span style="color: rgb(79, 79, 79);"><br></span></p> <p><img class="" data-copyright="0" data-ratio="0.2890932982917214" data-s="300,640" data-type="png" data-w="761" src="/upload/f928dd3d710e77c19
作者:微信小助手
<p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;text-align: center;"><span class="" style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;max-width: 100%;white-space: pre-wrap;font-size: 14px;color: rgb(255, 41, 65);line-height: 22.4px;box-sizing: border-box !important;overflow-wrap: break-word !important;">(给</span><span style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;max-width: 100%;white-space: pre-wrap;font-size: 14px;line-height: 22.4px;color: rgb(0, 128, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">算法爱好者</span><span class="" style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;max-width: 100%;white-space: pre-wrap;font-size: 14px;color: rgb(255, 41, 65);line-height: 22.4px;box-sizing: border-box !important;overflow-wrap: break-word !important;">加星标,修炼编程内功</span><span class="" style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;max-width: 100%;white-space: pre-wrap;color: rgb(255, 41, 65);font-size: 14px;line-height: 22.4px;box-sizing: border-box !important;overflow-wrap: break-word !important;">)</span></p> <p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"><br></p> <blockquote style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"> <p style="max-width: 100%;min-height: 1em;color: rgba(0, 0, 0, 0.5);font-size: 15px;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">作者:KaelQ,</span></p> <p style="max-width: 100%;min-height: 1em;color: rgba(0, 0, 0, 0.5);font-size: 15px;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">www.jianshu.com/p/5e171281a387</span></p> </blockquote> <p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"><span style="font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;"></span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">1.直接插入排序</strong></span></p> <blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="31" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">经常碰到这样一类排序问题:把新的数据插入到已经排好的数据列中。</p> </section> </blockquote> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将第一个数和第二个数排序,然后构成一个有序序列</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将第三个数插入进去,构成一个新的有序序列。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">对第四个数、第五个数……直到最后一个数,重复第二步。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.033282904689864" data-s="300,640" data-type="jpeg" data-w="661" src="/upload/682a4375bf02696564c83e8ab49373a3.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 661px !important;visibility: visible !important;"></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写写成代码:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先设定插入次数,即循环次数,for(int i=1;i<length;i++),1个数的那次不用插入。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">设定插入数和得到已经排好序列的最后一个数的位数。insertNum和j=i-1。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">从最后一个数开始向前循环,如果插入数小于当前数,就将当前数向后移动一位。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将当前数放置到空着的位置,即j+1。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">insertSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] a)</span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> length=a.length;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//数组长度,将这个提取出来是为了提高速度。</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> insertNum;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//要插入的数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>;i<length;i++){<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//插入的次数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> insertNum=a[i];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//要插入的数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> j=i<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//已经排序好的序列元素个数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">while</span>(j>=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>&&a[j]>insertNum){<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//序列从后到前循环,将大于insertNum的数向后移动一格</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>]=a[j];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//元素移动一格</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> j--;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>]=insertNum;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//将需要插入的数放在要插入的位置。</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">2.希尔排序</span></strong></span></p> <blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="18" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">对于直接插入排序问题,数据量巨大时。</p> </section> </blockquote> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将数的个数设为n,取奇数k=n/2,将下标差值为k的书分为一组,构成有序序列。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">再取k=k/2 ,将下标差值为k的书分为一组,构成有序序列。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第二步,直到k=1执行简单插入排序。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.332618025751073" data-s="300,640" data-type="jpeg" data-w="466" src="/upload/22c91e3f1857928356b0b6f700f48b3d.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 466px !important;visibility: visible !important;"></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写成代码:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先确定分的组数。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">然后对组中元素进行插入排序。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">然后将length/2,重复1,2步,直到length=0为止。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">sheelSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] a)</span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> d = a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">while</span> (d!=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> d=d/<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">2</span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> x = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>; x < d; x++) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//分的组数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i = x + d; i < a.length; i += d) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//组中的元素,从第二个数开始</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> j = i - d;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//j为有序序列最后一位的位数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> temp = a[i];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//要插入的元素</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (; j >= <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span> && temp < a[j]; j -= d) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//从后往前遍历。</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j + d] = a[j];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//向后移动d位</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j + d] = temp;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">3.简单选择排序</strong></span></p> <blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="17" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">常用于取序列中最大最小的几个数时。</p> </section> </blockquote> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">(如果每次比较都交换,那么就是交换排序;如果每次比较完一个循环再交换,就是简单选择排序。)</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">遍历整个序列,将最小的数放在最前面。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">遍历剩下的序列,将最小的数放在最前面。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第二步,直到只剩下一个数。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.0056497175141244" data-s="300,640" data-type="jpeg" data-w="531" src="/upload/f2dc18a8e562698d53eba1ac06b6bb37.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 531px !important;visibility: visible !important;"></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写成代码:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先确定循环次数,并且记住当前数字和当前位置。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将当前位置后面所有的数与当前数字进行对比,小数赋值给key,并记住小数的位置。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">比对完成后,将最小的值与第一个数的值交换。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复2、3步。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">selectSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] a)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> length = a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>; i < length; i++) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//循环次数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> key = a[i];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> position=i;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> j = i + <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>; j < length; j++) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//选出最小的值和位置</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> (a[j] < key) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> key = a[j];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> position = j;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[position]=a[i];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//交换位置</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[i]=key;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">4.堆排序</span></strong></span></p> <blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="11" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">对简单选择排序的优化。</p> </section> </blockquote> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将序列构建成大顶堆。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将根节点与最后一个节点交换,然后断开最后一个节点。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第一、二步,直到所有节点断开。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="0.7598752598752598" data-s="300,640" data-type="jpeg" data-w="962" src="/upload/835d5978cf7becbdfcc32ca5f2e5c27.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">heapSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] a)</span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> System.out.println(<span class="" style="max-width: 100%;color: rgb(152, 195, 121);box-sizing: border-box !important;overflow-wrap: break-word !important;">"开始排序"</span>);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> arrayLength=a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//循环建堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>;i<arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>;i++){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//建堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> buildMaxHeap(a,arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>-i);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//交换堆顶和最后一个元素</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> swap(a,<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>,arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>-i);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> System.out.println(Arrays.toString(a));<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">private</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">swap</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] data, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> j)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">// TODO Auto-generated method stub</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> tmp=data[i];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> data[i]=data[j];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> data[j]=tmp;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//对data数组从0到lastIndex建大顶堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">private</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">buildMaxHeap</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] data, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> lastIndex)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">// TODO Auto-generated method stub</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//从lastIndex处节点(最后一个节点)的父节点开始</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i=(lastIndex<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>)/<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">2</span>;i>=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>;i--){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//k保存正在判断的节点</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> k=i;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//如果当前k节点的子节点存在</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">while</span>(k*<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">2</span>+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span><=lastIndex){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//k节点的左子节点的索引</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> biggerIndex=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">2</span>*k+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span>(biggerIndex<lastIndex){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//若果右子节点的值较大</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span>(data[biggerIndex]<data[biggerIndex+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>]){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//biggerIndex总是记录较大子节点的索引</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> biggerIndex++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//如果k节点的值小于其较大的子节点的值</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span>(data[k]<data[biggerIndex]){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//交换他们</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> swap(data,k,biggerIndex);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> k=biggerIndex;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">else</span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">break</span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow
作者:微信小助手
<section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn" data-mpa-powered-by="yiban.io"></section> <h3 class="" style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;text-align: center;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="__bg_gif " data-copyright="0" data-ratio="0.4573333333333333" data-type="gif" data-w="750" src="/upload/d3c1e8315082599dc463204daf9a9695.null" style="font-size: 17px;letter-spacing: 0.544px;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 677px !important;"><br style="max-width: 100%;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p><p neue="neue" pingfang="pingfang" sc="sc" hiragino="hiragino" gb="gb" ui="ui" microsoft="microsoft" arial="arial" sans-serif="sans-serif" px="px" normal="normal" em="em" important="important" rgb="rgb" helvetica="helvetica" sans="sans" yahei="yahei" break-word="break-word" border-box="border-box" style="max-width: 100%;min-height: 1em;letter-spacing: 0.54px;font-size: 17px;text-align: center;line-height: 1.75em;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p><p neue="neue" pingfang="pingfang" sc="sc" hiragino="hiragino" gb="gb" ui="ui" microsoft="microsoft" arial="arial" sans-serif="sans-serif" px="px" normal="normal" em="em" important="important" rgb="rgb" helvetica="helvetica" sans="sans" yahei="yahei" break-word="break-word" border-box="border-box" style="max-width: 100%;min-height: 1em;letter-spacing: 0.54px;font-size: 17px;text-align: center;line-height: 1.75em;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;user-select: text !i