简体   繁体   English

选择排序的总和

[英]Summation of Selection Sort

I'm working through the algorithm design manual and stuck on understanding the summation behind selection sort in Chapter 2.我正在阅读算法设计手册,并坚持理解第 2 章中选择排序背后的总和。

  1. Can someone explain how this summation is simplifed?有人可以解释这个总结是如何简化的吗? For example, How do we get to n - i - 1?例如,我们如何得到 n - i - 1?

  2. if n = 8, then s(n) is (8 - 1) + (8 - 2) + (8 - 3) etc. What does the result of this mean in relation to the formula?如果 n = 8,则 s(n) 为 (8 - 1) + (8 - 2) + (8 - 3) 等。与公式相关的结果是什么? I've included a screenshot of the paragrah and my notes for context.我已经包含了段落的屏幕截图和我的上下文注释。 screenshot of selection sort选择排序截图

\sum_{i=0}^{j-1} \sum_{j=i+1}^{n-1} 1 = \sum_{i=0}^{j-1} n - i - 1 , why this, where is j ? \sum_{i=0}^{j-1} \sum_{j=i+1}^{n-1} 1 = \sum_{i=0}^{j-1} n - i - 1 ,为什么这个, j在哪里?

You were asking about the simplifying step with which the index j is summed over.您在询问对索引j求和的简化步骤。 This link goes more in detail, but in general,这个链接更详细,但总的来说,

\sum_{j=u}^{v} 1 = -u + v + 1

is equivalent to this pseudo-code,相当于这个伪代码,

sum = 0
for(int j = u; j <= v; j++)
    sum += (f(j) = 1)
return sum

For example, with u=3 and v=22 , it would be sum = u - v + 1 = 22 - 3 + 1 = 20 .例如,对于u=3v=22 ,它将是sum = u - v + 1 = 22 - 3 + 1 = 20

图形表示。

With -u + v + 1 at u = i+1 and v = n-1 it simplifies to -(i+1) + (n-1) + 1 = n - i - 1 .u = i+1v = n-1处使用-u + v + 1 1 它简化为-(i+1) + (n-1) + 1 = n - i - 1

if n=8 does \sum_{i=0}^{n-1} n - i - 1 refer to (8-1) + (8-2) + (8-3) + ... + 2 + 1 [+ 0] , why two different kinds of values?如果n=8确实\sum_{i=0}^{n-1} n - i - 1指的是(8-1) + (8-2) + (8-3) + ... + 2 + 1 [+ 0] ,为什么有两种不同的值?

It has been simplified from (8-0-1) + (8-1-1) + (8-2-1) + ... (8-5-1) + (8-6-1) + (8-7-1) .它已简化为(8-0-1) + (8-1-1) + (8-2-1) + ... (8-5-1) + (8-6-1) + (8-7-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM