Solutions of Theory of Algorithms assignment 7-1

Solutions of Theory of Algorithms assignment 7-1 - array)....

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Solutions of Theory of Algorithms Assignment 4 Exercise 7-1 a. x i J 13 19 9 5 12 8 7 4 11 2 6 21 13 0 11 6 19 9 5 12 8 7 4 11 2 13 21 2 0 10 2 19 9 5 12 8 7 4 11 6 13 21 6 1 9 2 6 9 5 12 8 7 4 11 19 13 21 6 3 9 2 5 9 6 12 8 7 4 11 19 13 21 5 1 3 2 4 9 6 12 8 7 5 11 19 13 21 9 2 7 2 4 5 6 12 8 7 9 11 19 13 21 13 2 7 2 4 5 6 12 8 7 9 11 13 19 21 11 9 10 2 4 5 6 12 8 7 9 11 13 19 21 12 4 8 2 4 5 6 11 8 7 9 12 13 19 21 11 4 7 2 4 5 6 9 8 7 11 12 13 19 21 9 4 6 2 4 5 6 7 8 9 11 12 13 19 21 7 4 5 2 4 5 6 7 8 9 11 12 13 19 21 19 10 11 b. Although in the initialization steps in lines 2 and 3 for i and j are outside the range of the array, but they are adjusted inside the While loop; so that i access the first element in the array and j access the last element. c. As j begins the loop by being the last element in the array, and then it decrements by one in every iteration. Therefore, its maximum value is r (the last element ) and its minimum value is p (the first element in the
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: array). d. In HOARE-PARTITION, it searches the array for an element smaller than x, and an element greater than x. but it swaps these two elements only if the index of the smaller element is greater than the index of the greater element. And that leads us to divide the array into two partitions where every element in the first partition is smaller than or equal to all elements in the second partition. e. QUICKSORT HOARE(A, p, r) 1 if p < r 2 then q HOARE-PARTITION (A, p, r) 3 QUICKSORT HOARE (A, p, q - 1) 4 QUICKSORT HOARE (A, q + 1, r)...
View Full Document

This note was uploaded on 11/09/2010 for the course CS 11841 taught by Professor Dr.ayman during the Spring '09 term at Alexandria University.

Page1 / 2

Solutions of Theory of Algorithms assignment 7-1 - array)....

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online