H2 Solution

.pdf

School

George Washington University *

*We aren’t endorsed by this school

Course

6212

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

21

Uploaded by ChancellorParrotMaster869

1 CS 6212 Fall 2023 Youssef Homework 2 Problem 1 : (10 points) a. Show step by step how the heapsort algorithm sorts the array 27, 13, 56, 35, 48, 8, 18, 67, 5, 62, 7 starting from the min-heap you built in homework 1 (Problem 4a) for the same array. Let us denote B[1..n] as the sorted array. Step 0: A[i] 5 7 13 27 8 56 18 67 35 62 48 B[i] Step 1: A[i] 7 8 13 27 48 56 18 67 35 62 B[i] 5 Step 2: A[i] 8 27 13 35 48 56 18 67 62 B[i] 5 7 Step 3: A[i] 13 27 18 35 48 56 62 67 B[i] 5 7 8 Step 4: A[i] 18 27 56 35 48 67 62 B[i] 5 7 8 13 Step 5: A[i] 27 35 56 62 48 67 B[i] 5 7 8 13 18
2 Step 6: A[i] 35 48 56 62 67 B[i] 5 7 8 13 18 27 Step 7: A[i] 48 62 56 67 B[i] 5 7 8 13 18 27 35 Step 8: A[i] 56 62 67 B[i] 5 7 8 13 18 27 35 48 Step 9: A[i] 62 67 B[i] 5 7 8 13 18 27 35 48 56 Step 10: A[i] 67 B[i] 5 7 8 13 18 27 35 48 56 62 Step 11: A[i] B[i] 5 7 8 13 18 27 35 48 56 62 67 b. Show step by step how the quicksort algorithm sorts the same array 27, 13, 56, 35, 48, 8, 18, 67, 5, 62, 7. Indicate at each step what the partitioning element is. We always take the 1st element as a pivot and omit the steps for sorting A[p:q] when p=q. Color: red signified that location where the pivot ends up. Yellow highlight means that the corresponding number has taken its final position in the sorted array.
3 Step 0: A[i] 27 13 56 35 48 8 18 67 5 62 7 Step 1: Quicksort(1,11): pivot 27 A[i] 8 13 7 5 18 27 48 67 35 62 56 Step 2: Quicksort(1,5): pivot 8 A[i] 7 5 8 13 18 27 48 67 35 62 56 Step 3: Quicksort(1,2): pivot 7 A[i] 5 7 8 13 18 27 48 67 35 62 56 Step 4: Quicksort(4,5): pivot 13 A[i] 5 7 8 13 18 27 48 67 35 62 56 Step 5: Quicksort(7,11): pivot 48 A[i] 5 7 8 13 18 27 35 48 67 62 56 Step 6: Quicksort(9,11): pivot 67 A[i] 5 7 8 13 18 27 35 48 56 62 67 Step 7: Quicksort(9,10): pivot 56 A[i] 5 7 8 13 18 27 35 48 56 62 67 c. Show step by step how the Mergesort algorithm sorts the same array.
4 We highlight the intervals where Mergesort is applied using red and bold font. To simplify the presentation, (1) we simply assume that sorting is in plac e, i.e., the input and output arrays are assumed to be A, and (2) we skip calls to Mergesort when the input size is 1. A[i] 27 13 56 35 48 8 18 67 5 62 7 Mergesort(A[1:11]) Mergesort(A[1:6]) Mergesort(A[1:3]) Mergesort(A[1:2]): A[i] 13 27 56 35 48 8 18 67 5 62 7 Mergesort(A[1:11]) Mergesort(A[1:6]) Mergesort(A[3:3]) : A[i] 13 27 56 35 48 8 18 67 5 62 7 Mergesort(A[1:11]) Mergesort(A[1:6]) Mergesort(A[6:6]) : A[i] 13 27 56 8 35 48 18 67 5 62 7 Mergesort(A[1:11]) Mergesort(A[1:6]) Merge(A[1:3],A[4:6]): A[i] 8 13 27 35 48 56 18 67 5 62 7 Mergesort(A[1:11]) Mergesort(A[7:11]) Mergesort(A[7:9]) Mergesort(A[7:8]): A[i] 8 13 27 35 48 56 18 67 5 62 7 Mergesort(A[1:11]) Mergesort(A[7:11]) Mergesort(A[7:9]) Merge(A[7:8],A[9]): A[i] 8 13 27 35 48 56 5 18 67 62 7 Mergesort(A[1:11]) Mergesort(A[7:11]) Mergesort(A[10:11]): A[i] 8 13 27 35 48 56 5 18 67 7 62 Mergesort(A[1:11]) Mergesort(A[7:11]) Merge(A[7:9], A[10:11]): A[i] 8 13 27 35 48 56 5 7 18 62 67 Mergesort(A[1:11]) Merge(A[1:6], A[7:11]): A[i] 5 7 8 13 18 27 35 48 56 62 67 d. Show step by step how the insertion sort algorithm sorts the same array. (This algorithm sorts by repeated insertion into a (initially empty) sorted array.) We highlight the sorted output subarray using red and bold font.
5 A[i] 27 13 56 35 48 8 18 67 5 62 7 A[i] 13 27 56 35 48 8 18 67 5 62 7 A[i] 13 27 56 35 48 8 18 67 5 62 7 A[i] 13 27 35 56 48 8 18 67 5 62 7 A[i] 13 27 35 48 56 8 18 67 5 62 7 A[i] 8 13 27 35 48 56 18 67 5 62 7 A[i] 8 13 18 27 35 48 56 67 5 62 7 A[i] 8 13 18 27 35 48 56 67 5 62 7 A[i] 5 8 13 18 27 35 48 56 67 62 7 A[i] 5 8 13 18 27 35 48 56 62 67 7 A[i] 5 7 8 13 18 27 35 48 56 62 67 e. Show step by step how selection sort sorts the same array. (This finds the minimum first, then the 2nd min, then the 3rd min, and so on. It has nothing to do with quick-select.) At each step, we highlight the selected minimum element in red and bold, and the swapped element with an underline. The output subarray is shown with yellow highlight. A[i] 27 13 56 35 48 8 18 67 5 62 7 A[i] 5 13 56 35 48 8 18 67 27 62 7 A[i] 5 7 56 35 48 8 18 67 27 62 13
6 A[i] 5 7 8 35 48 56 18 67 27 62 13 A[i] 5 7 8 13 48 56 18 67 27 62 35 A[i] 5 7 8 13 18 56 48 67 27 62 35 A[i] 5 7 8 13 18 27 48 67 56 62 35 A[i] 5 7 8 13 18 27 35 67 56 62 48 A[i] 5 7 8 13 18 27 35 48 56 62 67 A[i] 5 7 8 13 18 27 35 48 56 62 67 A[i] 5 7 8 13 18 27 35 48 56 62 67 Problem 2 : (20 points) Let 𝐴𝐴 [1: 𝑛𝑛 ] be an array of real numbers. The distance between two elements 𝐴𝐴 [ 𝑖𝑖 ] and 𝐴𝐴 [ 𝑗𝑗 ] is the absolute value | 𝐴𝐴 [ 𝑖𝑖 ] − 𝐴𝐴 [ 𝑗𝑗 ]| . Two elements 𝐴𝐴 [ 𝑖𝑖 ] and 𝐴𝐴 [ 𝑗𝑗 ] of 𝐴𝐴 are called neighbors if 𝑖𝑖 = 𝑗𝑗 − 1 or 𝑖𝑖 = 𝑗𝑗 + 1. Two elements of 𝐴𝐴 are called the closest neighbors if they are neighbors and the distance between them is the smallest distance between any two neighbors of 𝐴𝐴 . a. Write a divide-and-conquer algorithm that takes 𝐴𝐴 [1: 𝑛𝑛 ] and returns the following output: The minimum of 𝐴𝐴 , the maximum of 𝐴𝐴 , and the indices of the two closest neighbors in 𝐴𝐴 . Note that there could be multiple closest neighbors; break the tie anyway you want. b. Analyze the time complexity of your algorithm. Note that for full credit, your algorithm should take less than ( 𝑛𝑛 log 𝑛𝑛 ) . Solution:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help