Woo*_*oow 9 python sorting algorithm list
所以我一直在做一些排序算法,我想运行一个测试函数,生成不同类型的列表来运行我的排序算法.
一个这样的列表将是已经排序的列表,其中列表中的n个项目随机地被洗牌(但不是全部,列表仍然应该被排序而不是n个项目)
testlist = random.sample(range(0,10),10)
testlist.sort()
Run Code Online (Sandbox Code Playgroud)
这给了我一个大小为10的独特项目的排序列表,但后来我不确定如何将列表中的这10个项目中的n个移动到随机位置,只是为了混合排序
这是在列表中随机播放一些项目的一种方法:
import random
import numpy as np
# Make a sorted list of integers.
x = np.array(range(100))
# Choose 10 indices at random.
r = random.sample(range(len(x)), 10)
# Copy this list and shuffle.
s = r.copy()
random.shuffle(s)
# Replace the indices with the shuffled ones.
x[r] = x[s]
Run Code Online (Sandbox Code Playgroud)
请注意,这可能会保留一些索引不变