对链表进行插入排序

147. 对链表进行插入排序对链表进行插入排序。 插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。 插入排序算法: 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适...
resolve(res); }).catch((error) => { console.error('Failed to save visitor count', error); reject(error); }); }); }, // 构建自增请求体 buildIncrement(objectId) { return { "method": "PUT", "path": `/1.1/classes/Counter/${ objectId }`, "body": { "times": { '__op': 'Increment', 'amount': 1 } } } }, // 校验是否为有效的 UV validUV() { var key = 'LeanCloudUVTimestamp'; var flag = localStorage.getItem(key); if (flag) { // 距离标记小于 24 小时则不计为 UV if (new Date().getTime() - parseInt(flag) <= 86400000) { return false; } } localStorage.setItem(key, new Date().getTime().toString()); return true; }, addCount(Counter) { var enableIncr = '' === 'true' && window.location.hostname !== 'localhost'; enableIncr = true; var getterArr = []; var incrArr = []; // 请求 PV 并自增 var pvCtn = document.querySelector('#lc-sv'); if (pvCtn || enableIncr) { var pvGetter = this.getRecord(Counter, 'https://xmmarlowe.github.io' + '/#lc-sv', 'Visits').then((record) => { incrArr.push(this.buildIncrement(record.objectId)) var eles = document.querySelectorAll('#lc-sv #number'); if (eles.length > 0) { eles.forEach((el,index,array)=>{ el.innerText = record.times + 1; if (pvCtn) { pvCtn.style.display = 'inline'; } }) } }); getterArr.push(pvGetter); } // 请求 UV 并自增 var uvCtn = document.querySelector('#lc-uv'); if (uvCtn || enableIncr) { var uvGetter = this.getRecord(Counter, 'https://xmmarlowe.github.io' + '/#lc-uv', 'Visitors').then((record) => { var vuv = this.validUV(); vuv && incrArr.push(this.buildIncrement(record.objectId)) var eles = document.querySelectorAll('#lc-uv #number'); if (eles.length > 0) { eles.forEach((el,index,array)=>{ el.innerText = record.times + (vuv ? 1 : 0); if (uvCtn) { uvCtn.style.display = 'inline'; } }) } }); getterArr.push(uvGetter); } // 请求文章的浏览数,如果是当前页面就自增 var allPV = document.querySelectorAll('#lc-pv'); if (allPV.length > 0 || enableIncr) { for (i = 0; i < allPV.length; i++) { let pv = allPV[i]; let title = pv.getAttribute('data-title'); var url = 'https://xmmarlowe.github.io' + pv.getAttribute('data-path'); if (url) { var viewGetter = this.getRecord(Counter, url, title).then((record) => { // 是当前页面就自增 let curPath = window.location.pathname; if (curPath.includes('index.html')) { curPath = curPath.substring(0, curPath.lastIndexOf('index.html')); } if (pv.getAttribute('data-path') == curPath) { incrArr.push(this.buildIncrement(record.objectId)); } if (pv) { var ele = pv.querySelector('#lc-pv #number'); if (ele) { if (pv.getAttribute('data-path') == curPath) { ele.innerText = (record.times || 0) + 1; } else { ele.innerText = record.times || 0; } pv.style.display = 'inline'; } } }); getterArr.push(viewGetter); } } } // 如果启动计数自增,批量发起自增请求 if (enableIncr) { Promise.all(getterArr).then(() => { incrArr.length > 0 && this.increment(Counter, incrArr); }) } }, fetchData(api_server) { var Counter = (method, url, data) => { return fetch(`${ api_server }/1.1${ url }`, { method, headers: { 'X-LC-Id': this.app_id, 'X-LC-Key': this.app_key, 'Content-Type': 'application/json', }, body: JSON.stringify(data) }); }; this.addCount(Counter); }, refreshCounter() { var api_server = this.app_id.slice(-9) !== '-MdYXbMMI' ? this.custom_api_server : `https://${ this.app_id.slice(0, 8).toLowerCase() }.api.lncldglobal.com`; if (api_server) { this.fetchData(api_server); } else { fetch('https://app-router.leancloud.cn/2/route?appId=' + this.app_id) .then(resp => resp.json()) .then(({api_server}) => { this.fetchData('https://' + api_server); }); } } }; LCCounter.refreshCounter(); document.addEventListener('pjax:complete', function () { LCCounter.refreshCounter(); });