标签:nes sort func java lse fun for 算法 iss
// 鸡尾酒排序算法 function wineSort(arr) { console.log(‘鸡尾酒算法第0次:‘ + arr.join(‘,‘)); let len = arr.length; for (let i = 0; i < len / 2; i++) { // 奇数次 let isSort = true; for (let j = i; j < len - 1 - i; j++) { if (arr[j + 1] < arr[j]) { let temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; isSort = false; } } console.log(‘鸡尾酒算法第‘ + (i + 1) + ‘次:‘ + arr.join(‘,‘)); if (isSort) { break; } // 偶数次 isSort = true; for (let k = len - 1 - i; k > i; k--) { if (arr[k] < arr[k - 1]) { let temp = arr[k]; arr[k] = arr[k - 1]; arr[k -1] = temp; isSort = false; } } console.log(‘鸡尾酒算法第‘ + (i + 1) + ‘次:‘ + arr.join(‘,‘)); if (isSort) { break; } } console.log(‘鸡尾酒算法:‘ + arr.join(‘,‘)); } let array = [3, 4, 1, 2, 5, 6, 7, 8]; wineSort(array);
标签:nes sort func java lse fun for 算法 iss
原文地址:https://www.cnblogs.com/peter-web/p/12550150.html