Analysis of Algorithm

Submitted by: Submitted by

Views: 69

Words: 281

Pages: 2

Category: Science and Technology

Date Submitted: 07/22/2014 12:41 AM

Report This Essay

Insert

 Procedure

 

For

 (i

 =

 k

 to

 1)

 

 If

 A[i]

 <

 A[i-­‐1]

 

 

 

 

 

 

 swap

 A[i]

 

 A[i-­‐1]

 

  Is

 this

 enough

 for

 moveMin?

 

  There

 was

 confusion

 between

 this

 and

 sorFng

 algorithm.

 This

 is

 just

 a

  building

 block

 for

 the

 sorFng

 algorithm.

 

•  k

 

•  K-­‐1

 

•  K-­‐2

 

•  …

 

•  4

 

•  3

 

•  2

 

•  1

 

Algorithm

 Insert

 

 

  Input:

 An

 Array

 A

 of

 k

 numbers.

 This

 can

 also

 be

 expressed

 as

  A[1..k].

 The

 first

 k-­‐1

 numbers

 are

 sorted

 in

 ascending

 order,

  the

 last

 number

 is

 out

 of

 place.

 

 

 

 Example:

 1,

 3,

 7,

 9,

 13,

 17,

 23,

 4.

 Note

 that

 the

 first

 7

  numbers

 are

 sorted

 while

 the

 8th

 number

 is

 not

 in

 its

 correct

  sorted

 place.

 

 

 

 

 Output:

 An

 array

 A[1..k],

 which

 is

 sorted

 in

 ascending

 order.

 

  Example:

 1,

 3,

 4,

 7,

 9,

 13,

 17,

 23

 

1 2 3 … K

 The

 above

 input

 and

 output

 describes

 the

 algorithm

 Insert

 in

  simple

 language.

 We

 have

 not

 yet

 discussed

 or

 designed

 the

  algorithm

 Insert

 but

 now

 we

 at

 least

 understand

 what

 this

  algorithm

 is

 supposed

 to

 do,

 how

 to

 do

 it

 comes

 next.

 

 

 Write

 Pseudo-­‐code

 for

 this

 algorithm,

 run

 it

 on

 the

 given

  example

 and

 be

 confident

 that

 it

 performs

 the

 required

  funcFon.

 

 

 

 Calculate

 the...