- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
尝试使用 Python 实现 Introsort。
给出的伪代码是:
1 n ←|A|
2 if n ≤ 1
3 return
4 elseif d = 0
5 Heap-Sort(A)
6 else
7 p ← Partition(A) // Partitions A and returns pivot position
8 Intro-Sort(A[0:p],d−1)
9 Intro-Sort(A[p+1:n],d−1)
我的源代码是:
import math
def introSort(a,d):
n = len(a)
if n <= 1:
return
elif d == 0:
heapSort(a)
else:
p = partition(a)
a1 = a[0:p]
a2 = a[p+1:n]
introSort(a1, d-1)
introSort(a2, d-1)
a = a1 + [a[p]] + a2
def heapSort (a):
END = len(a)
for k in range (math.floor(END/2) - 1, -1, -1):
heapify(a, END, k)
for k in range(END, 1, -1):
swap(a, 0, k-1)
heapify(a, k-1, 0)
def partition(a):
x = a[len(a) - 1]
i = -1
for j in range(0, len(a) - 2):
if a[j] <= x:
i = i + 1
swap(a, i, j)
swap(a, i + 1, len(a) - 1)
return i + 1
def swap(a, i, j):
tmp = a[i]
a[i] = a[j]
a[j] = tmp
def heapify(a,iEnd,iRoot):
iL = 2*iRoot + 1
iR = 2*iRoot + 2
if iR < iEnd:
if (a[iRoot] >= a[iL] and a[iRoot] >= a[iR]):
return
else:
if(a[iL] > a[iR]):
j = iL
else:
j = iR
swap(a, iRoot, j)
heapify(a, iEnd, j)
elif iL < iEnd:
if (a[iRoot] >= a[iL]):
return
else:
swap(a, iRoot, iL)
heapify(a,iEnd,iL)
else:
return
a = [3,5,6,1,23,521,6243,632,123,53,62,421,15,672,7,435,21]
introSort(a,2)
print(a)
给出的结果是错误的:
>python introsort.py
[3, 5, 6, 1, 15, 7, 21, 632, 123, 53, 62, 421, 23, 672, 521, 435, 6243]
似乎它在分区后立即停止并且子列表上的排序不起作用。很明显,21 是枢轴,分区工作得很好。
有人能指出我的错误吗?非常感谢!
最佳答案
实际上,我通过使用对列表的一部分调用堆排序的辅助函数来修复此问题。我没有通过修改 heapsort 函数来做到这一点,而是在一个单独的副本中这样做,并将排序后的元素更新回原始列表中。代码显示为:
import math
def introSort(a, d, start, end):
n = end - start
if n <= 1:
return
elif d == 0:
introHS(a, start, end)
else:
p = partition(a, start, end)
introSort(a, d-1, start, p)
introSort(a, d-1, p+1, end)
def introHS (a, start, end):
b = a[start:end]
heapSort(b)
for i in range(0,len(b)):
a[start+i] = b[i]
def heapSort (a):
END = len(a)
for k in range (math.floor(END/2) - 1, -1, -1):
heapify(a, END, k)
for k in range(END, 1, -1):
swap(a, 0, k-1)
heapify(a, k-1, 0)
def partition(a, start, end):
x = a[end-1]
i = start-1
for j in range(start, end-1):
if a[j] <= x:
i=i+1
swap(a, i, j)
swap(a, i+1, end-1)
return i+1
def swap(a, i, j):
tmp = a[i]
a[i] = a[j]
a[j] = tmp
def heapify(a,iEnd,iRoot):
iL = 2*iRoot + 1
iR = 2*iRoot + 2
if iR < iEnd:
if (a[iRoot] >= a[iL] and a[iRoot] >= a[iR]):
return
else:
if(a[iL] > a[iR]):
j = iL
else:
j = iR
swap(a, iRoot, j)
heapify(a, iEnd, j)
elif iL < iEnd:
if (a[iRoot] >= a[iL]):
return
else:
swap(a, iRoot, iL)
heapify(a,iEnd,iL)
else:
return
通过测试它工作正常:
a = [3,5,6,1,23,521,6243,632,123,53,62,421,15,672,7,435,21,123,41,52,6234,11,55,6345,324,58,46,2,123,152,6156,46,34,3426,5341,16,3314,34,73416,345]
print("Original:")
print(a)
for i in range(0,15):
introSort(a,i,0,len(a))
print("d=" + str(i))
print(a)
给予:
>Original:
>[3, 5, 6, 1, 23, 521, 6243, 632, 123, 53, 62, 421, 15, 672, 7, 435, 21, 123, 41, 52, 6234, 11, 55, 6345, 324, 58, 46, 2, 123, 152, 6156, 46, 34, 3426, 5341, 16, 3314, 34, 73416, 345]
>d=0
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=1
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=2
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=3
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=4
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=5
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=6
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=7
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=8
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=9
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=10
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=11
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=12
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=13
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
>d=14
>[1, 2, 3, 5, 6, 7, 11, 15, 16, 21, 23, 34, 34, 41, 46, 46, 52, 53, 55, 58, 62, 123, 123, 123, 152, 324, 345, 421, 435, 521, 632, 672, 3314, 3426, 5341, 6156, 6234, 6243, 6345, 73416]
关于python - 在 Python 中进行 Introsort,有人可以指出我的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48614165/
Introsort从快速排序开始,当递归深度超过基于被排序元素数量的级别时切换到堆排序。那个数字是多少?有没有具体的范围或限值? 最佳答案 Introsort algorithm从 Quicksort
我知道当使用 T[] 之类的东西调用 IntroSort 时,请说 Integer[] x;它将对数组进行排序,直到递归深度太多(在大多数实现中为 0),然后它将切换到 HeapSort。但是,当递归
我读到 C++ 对其内置的 std::sort 使用 introsort(内省(introspection)排序),它从快速排序开始,并在达到深度限制时切换到堆排序。 我还读到深度限制应该是 2*lo
我正在用 C++ 编写一个简单的就地 introsort,其中我试图在分区函数中手动展开一个循环以进行优化。我将在下面包含的程序可以编译,但无法正确排序随机列表。 这个程序正在为 RISC-V 架构向
尝试使用 Python 实现 Introsort。 给出的伪代码是: 1 n ←|A| 2 if n ≤ 1 3 return 4 elseif d = 0 5 Heap-Sort(A) 6 else
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我不太了解 Introsort 算法。如您所见,我添加了它的伪代码。最大深度是什么意思? “⌊log(length(A))⌋ × 2”是什么意思 希望有人能给我解释一下。 procedure sor
我一直在挑选和探索 Swift Array 的排序函数,我惊讶地发现对 500,000 个整数的数组进行就地排序比对 500,000 个元组的数组排序快得多(快 5 倍)。我对这一发现感到惊讶,因为在
List.Sort 的 .NET 文档提到渐近运行时:https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.l
我是一名优秀的程序员,十分优秀!