We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
// 从小到大排序: function bubblingSort(list){ let temp; for(let i=0; i<list.length; i++){ for(let j=i; j<list.length; j++){ if(list[i] > list[j]){ temp = list[i]; list[i] = list[j]; list[j] = temp; } } } return list; } let res = bubblingSort([10, 8, 2, 23, 30, 4, 7, 1]) console.log(res); // [1, 2, 4, 7, 8, 10, 23, 30]
从小到大排序: function selectSort(list){ let r,temp; for(let j=0; j<list.length; j++){ for(let i = j+1; i<list.length; i++){ if(list[j] > list[i]){ temp = list[j]; list[j] = list[i]; list[i] = temp; } } } return list; } let res = selectSort([10, 8, 2, 23, 30, 4, 7, 1]) console.log(res); // [1, 2, 4, 7, 8, 10, 23, 30]
整个排序过程为n-1趟插入,即先将序列中第1个记录看成是一个有序子序列,然后从第2个记录开始,逐个进行插入,直至整个序列有序。
function insertSort(list) { let flag; for(let index = 1; index < list.length; index++) { flag = list[index]; let j = index - 1; while (flag < list[j]) { list[j + 1] = list[j] j--; } list[j + 1] = flag; } return list; } let res = insertSort([10, 8, 2, 23, 30, 4, 7, 1]) console.log(res); // [1, 2, 4, 7, 8, 10, 23, 30]
排序过程:先取一个正整数d1<n,把所有相隔d1的记录放一组,组内进行直接插入排序;然后取d2<d1,重复上述分组和排序操作;直至di=1,即所有记录放进一个组中排序为止
function shellSort(list) { const length = list.length; let j, temp; for (let d = parseInt(length / 2); d >= 1; d = parseInt(d / 2)) { for (let i = d; i < length; i++) { temp = list[i]; j = i - d; while (j >= 0 && temp < list[j]) { list[j + d] = list[j]; j -= d; } list[j + d] = temp; } } return list; } let res = shellSort([10, 8, 2, 23, 30, 4, 7, 1]) console.log(res); // [1, 2, 4, 7, 8, 10, 23, 30]
通过一次排序,将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可对这两部分记录进行排序,以达到整个序列有序。
function quickSort(v,left,right){ if(left < right){ var key = v[left]; var low = left; var high = right; while(low < high){ while(low < high && v[high] > key){ high--; } v[low] = v[high]; while(low < high && v[low] < key){ low++; } v[high] = v[low]; } v[low] = key; quickSort(v,left,low-1); quickSort(v,low+1,right); } } let list = [10, 8, 2, 23, 30, 4, 7, 1] quickSort(list, 0, 7) console.log(list); // [1, 2, 4, 7, 8, 10, 23, 30]
The text was updated successfully, but these errors were encountered:
dravenww
No branches or pull requests
计算机基础
http系列
排序
整个排序过程为n-1趟插入,即先将序列中第1个记录看成是一个有序子序列,然后从第2个记录开始,逐个进行插入,直至整个序列有序。
排序过程:先取一个正整数d1<n,把所有相隔d1的记录放一组,组内进行直接插入排序;然后取d2<d1,重复上述分组和排序操作;直至di=1,即所有记录放进一个组中排序为止
通过一次排序,将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可对这两部分记录进行排序,以达到整个序列有序。
其他
The text was updated successfully, but these errors were encountered: