Chap2 Insertion Sort

来源:互联网 发布:工作室监控软件 编辑:程序博客网 时间:2024/06/06 04:51

2.1 Insertion Sort

Loop Invariant

  • Initialization: It is true prior to the first iteration of the loop
  • Maintenance: If it is true before an iteration, it remains true before the next
  • Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct. 

e.g. In insertion sort, the loop invariant is: 

At the start of each iteration, A[1 .. j-1] consists of the elements originally in A[1 .. j-1] but in sorted order. 


 不太懂为什么Maintenance这步是正确的。

https://github.com/cucumberguagua/clrs/blob/master/InsertionSort.java


2.1-3

LINEAR-SEARCH (A, v)Input: A = <a1, a2, ... an>, and a value vOutput: An index i such that v = A[i] or NIL if v is not in Afor i = 1 to length[A]if(A[i] == v) return iend ifend forreturn NIL
But what is the loop invariant?

应该是:none of the elements at index A[1; : : : ; i - 1] are equal to v.


2.1-4 //不会做。要进位吗?


2.2 Analyzing Algorithms
2.2-1

Theta(n^3)


2.2-2

我的写法:

Selection-Sortfor i = 1 to length[A]-1for j = i to length[A]minIndex = jif A[j]] < A[minIndex]minIndex = jend ifend forswap(A[i], A[j])end for

package chap2;public class SelectionSort {public void selectionSort(int[] A){for(int i = 0; i < A.length-1; i++){//select the smallest in A[i..end]int minIndex = i;for(int j = i; j < A.length; j++){if(A[j] < A[minIndex]){minIndex = j;}}int tmp = A[i];A[i] = A[minIndex];A[minIndex] = tmp;}}public static void main(String[] args){SelectionSort ss = new SelectionSort();int[] A = {2, 5, 1, 2, 6, 3, 9, 12};ss.selectionSort(A);for(int i = 0; i < A.length; i++){System.out.println(A[i]);}}}


答案:Ref: Solutions for Introduction to algorithms, second edition

Assume that FIND-MIN(A; r; s) returns the index of the smallest element in A between indices r and s. This can be implemented in O(s - r) time if r > s.

Algorithm 2 SELECTION-SORT(A)Input: A = ha1; a2; : : : ani Output: sorted A.for i   1 to n - 1 doj   FIND-MIN(A; i; n)A[j] $ A[i]end for

Loop Invariant: A[1..i-1] are sorted, and all other elements are larger than them. 

Time = theta(n+(n-1)+...+1) = theta(n^2)


2.2-3 Linear search

On average, we need to search half of the elements:

Since the element being searched for has probability of 1/n to be each element, We have (if the element is the 1st, we search once, 2nd-twice, etc)

(1+2+ ... + n)/n = (n+1)/2

Both the average and worst case has Theta(n)


2.2-4 Ref: Solutions for Introduction to algorithms, second edition

One can modify an algorithm to have a best-case running time by specializing it to handle a best case input efefficiently.


2.3 Designing algorithms

Sorting algorithms

  • Incremental: Insertion sort, Selection sort
  • Divide-and-conquer: Merge sort, 

0 0
原创粉丝点击