插入排序
1 INSERTION-SORT(A) 2 for j←2 to length(A) 3 do key←A[j] 4 ?Insert A[j] into the sorted sequence A[1..j-1] 5 i←j-1 6 while i>0 and A[i]>key 7 do A[i+1]←A[i] 8 i←i-1 9 A[i+1]←key
合并排序
1 MERGE(A,p,q,r) 2 n1←q-p+1 3 n2←r-q 4 create arrays L[1..n1+1] and R[1..n2+1] 5 for i←1 to n1 6 do L[i]←A[p+i-1] 7 for j←1 to n2 8 do R[j]←A[q+j] 9 L[n1+1]←∞ 10 R[n2+1]←∞ 11 i←1 12 j←1 13 for k←p to r 14 do if L[i]<=R[j] 15 then A[k]←L[i] 16 i←i+1 17 else A[k]←R[j] 18 j←j+1 19 20 MERGE-SORT(A,p,r) 21 if p<r 22 then q←floor((p+r)/2) 23 MERGE-SORT(A,p,q) 24 MERGE-SORT(A,q+1,r) 25 MERGE(A,p,q,r)
冒泡排序
1 BUBBLE-SORT(A) 2 for i←1 to length[A] 3 do for j←length[A] downto i+1 4 do if A[j]<A[j-1] 5 then exchange A[j]←→A[j-1]
时间: 2024-10-10 19:46:37