Docsity

Sorting Problem - Analysis of Algorithms - Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Sorting Problem - Analysis of Algorithms - Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Introduction

The sorting problem

  • Input: A sequence of n numbers 〈 a 1 , a 2 , …, an 〉.
  • Output: A permutation (reordering) 〈 a’ 1 , a’ 2 , …, a’n 〉 of the input sequence such that a’ 1 ≤ a’ 2 ≤ …≤ a’n.
  • The sequences are typically stored in arrays.
  • We also refer to the numbers as keys. Along with each key may be additional information, known as satellite data.

Expressing algorithms

  • We express algorithms in whatever way is the clearest and most concise.
  • English is sometimes the best way.
  • When issues of control need to be made perfectly clear, we often use pseudocode.
  • Pseudocode is similar to C, C++, Pascal, and Java. If you know any of these languages, you should be able to understand pseudocode.
  • Pseudocode is designed for expressing algorithms to humans. Software engineering issues of data abstraction, modularity, and error handling are often ignored.
  • We sometimes embed English statements into pseudocode. Therefore, unlike for “real” programming languages, we cannot create a compiler that translates pseudocode to machine code.

Insertion sort

Pseudocode

  • We use a procedure INSERTION-SORT.
    • Takes as parameters an array A [1 .. n ] and the length n of the array.
    • As in Pascal, we use “..” to denote a range within an array
    • The array A is sorted in place: the numbers are rearranged within the array , with at most a constant number outside the array at any time

Pseudocode

Correctness

  • We often use a loop invariant to help us understand why an algorithm gives the correct answer - Here’s the loop invariant for INSERTION-SORT - Loop invariant: At the start of each iteration of the “outer” for loop—the loop indexed by j —the subarray A [1 .. j -1] consists of the elements originally in A [1.. j – 1] but in sorted order.

Loop invariants

  • We must show three things
    • Initialization: It is true prior to the first iteration of the loop.
    • Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration.
    • Termination: When the loop terminates, the invariant—usually along with the reason that the loop terminated—gives us a useful property that helps show that the algorithm is correct.

Loop invariants

  • Initialization : Just before the first iteration, j = 2. The subarray A [ .. j – 1] is the single element A [1], which is the element originally in A [1], and it is trivially sorted.
  • Maintenance: To be precise, we would need to state and prove a loop invariant for the “inner” while loop. Rather than getting bogged down in another loop invariant, we instead note that the body of the inner while loop works by moving A [j – 1], A [j – 2], A [j – 3], and so on, by one position to the right until the proper position for key (which has the value that started out in A [ j ]) is found_._ At that point, the value of key is placed into this position_._
  • Termination: The outer for loop ends when j > n , which occurs when j = n +1. Therefore, j = 1 - n. Plugging n in for j - 1 in the loop invariant, the subarray A [1 .. n ] consists of the elements originally in A [1 .. n ] but in sorted order. In other words, the entire array is sorted.

Pseudocode conventions

  • Indentation indicates block structure. Saves space and writing time.
  • Looping constructs are like in C, C++, Pascal, and Java. We assume that the loop variable in a for loop is still defined when the loop exits (unlike in Pascal).
  • // indicates that the remainder of the line is a comment.
  • Variables are local, unless otherwise specified.

Pseudocode conventions

  • The boolean operators “and” and “or” are short-circuiting : if after evaluating the left- hand operand, we know the result of the expression, then we don’t evaluate the right- hand operand.

Analyzing algorithms

  • In order to predict resource requirements, we need a computational model.
  • Random-access machine (RAM) model
    • Instructions are executed one after another. No concurrent operations.
    • Each instruction takes a constant amount of time.
    • The RAM model uses integer and floating-point types.
    • There is a limit on the word size. Integers require c lg n bits for an input size n

Input size

  • Depends on the problem being studied.
    • Usually, the number of items in the input. Like the size n of the array being sorted.
    • But could be something else. If multiplying two integers, could be the total number of bits in the two integers.
    • Could be described by more than one number. For example, graph algorithm running times are usually expressed in terms of the number of vertices and the number of edges in the input graph.

Running time

  • On a particular input, it is the number of primitive operations (steps) executed. - Want to define steps to be machine-independent. - Figure that each line of pseudocode requires a constant amount of time. - One line may take a different amount of time than another, but each execution of line i takes the same amount of time c (^) i. - This is assuming that the line consists only of primitive operations. - If the line is a subroutine call, then the actual call takes constant time,but the execution of the subroutine being called might not. - !take more than constant time. Example: “sort the points by If the line specifies operations other than primitive ones, then it might x- coordinate .”