0 views

Uploaded by ZhichaoWang

f

f

© All Rights Reserved

- 262585680 Abe Cofnas Sentiment Indicators Renko Price Break Kagi Point and Figure 2010 292 Pages.85
- 262585680 Abe Cofnas Sentiment Indicators Renko Price Break Kagi Point and Figure 2010 292 Pages.1
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..131
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..166
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..121
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..127
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..233
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..200
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..257
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..312
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..11
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..302
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..189
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..58
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..192
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..270
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..310
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..311
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..315
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..181

You are on page 1of 1

We use induction to reason about loop invariants.

Base case: At the start of the first iteration, the prefix is empty, i.e., the

suffix is the entire list. The invariant is (trivially) true.

Induction step: At each step of the algorithm, we move one element from

the suffix to the prefix. We do this by appending a minimum element of

the suffix to the end of the prefix. Because the invariant held before we

moved the element, we know that after we append the element the prefix

is still sorted. We also know that since we removed the smallest element

in the suffix, no element in the prefix is larger than the smallest element

in the suffix.

When the loop is exited, the prefix includes the entire list, and the suffix

is empty. Therefore, the entire list is now sorted in ascending order.

def selSort(L):

"""Assumes that L is a list of elements that can be

compared using >.

Sorts L in ascending order"""

suffixStart = 0

while suffixStart != len(L):

#look at each element in suffix

for i in range(suffixStart, len(L)):

if L[i] < L[suffixStart]:

#swap position of elements

L[suffixStart], L[i] = L[i], L[suffixStart]

suffixStart += 1

Its hard to imagine a simpler or more obviously correct sorting algorithm.

Unfortunately, it is rather inefficient.52 The complexity of the inner loop is

O(len(L)). The complexity of the outer loop is also O(len(L)). So, the complexity of

the entire function is O(len(L)2). I.e., it is quadratic in the length of L.

Fortunately, we can do a lot better than quadratic time using a divide-andconquer algorithm. The basic idea is to combine solutions of simpler instances

of the original problem. In general, a divide-and-conquer algorithm is

characterized by

1. A threshold input size, below which the problem is not subdivided,

2. The size and number of sub-instances into which an instance is split,

and

3. The algorithm used to combine sub-solutions.

The threshold is sometimes called the recursive base. For item 2 it is usual to

consider the ratio of initial problem size to sub-instance size. In most of the

examples weve seen so far, the ratio was 2.

52 But not the most inefficient of sorting algorithms, as suggested by a successful

candidate for the U.S. Presidency. See http://www.youtube.com/watch?v=k4RRi_ntQc8.

- 262585680 Abe Cofnas Sentiment Indicators Renko Price Break Kagi Point and Figure 2010 292 Pages.85Uploaded byZhichaoWang
- 262585680 Abe Cofnas Sentiment Indicators Renko Price Break Kagi Point and Figure 2010 292 Pages.1Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..131Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..166Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..121Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..127Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..233Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..200Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..257Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..312Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..11Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..302Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..189Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..58Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..192Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..270Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..310Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..311Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..315Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..181Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..162Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..123Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..223Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..293Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..160Uploaded byZhichaoWang
- Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..126Uploaded byZhichaoWang