幸运泡泡
Lucky Bubble
题目详情
Steven 收到了一个大小为 的数组,其中填充了数字 的随机排列。他执行一轮冒泡排序算法,数组就已排序。发生这种情况的概率是多少?
Steven received an array of size filled with a random permutation of the numbers . He performs one pass of the bubble sort algorithm and the array becomes sorted. What is the probability of this happening?
解析
总共有 可能的 数字排列,我们只需要在一次冒泡排序算法之后最终出现在排序数组中的排列。
单遍运行可编码为 (无交换)或 (交换)的 操作,为冒泡排序算法的最后一遍留下总共 可能的交换组合。
为了得到概率,我们将该数字除以排列总数:
Original Explanation
In total there are possible permutations of numbers and we want only the permutations that end up in a sorted array after a single pass of the bubble sort algorithm.
A single pass runs operations that could be encoded as a (no swap) or (swap), leaving a total of possible swap combinations for the last pass of the bubble sort algorithm.
To get the probability, we divide this number by the total number of permutations: