Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

INHA UNIVERSITY IN TASHKENT

Fall semester 2019


Computer Algorithms (CIE3090)
Practice #1

Name: Set Ashish ID: U228335 Gp: ICSE-00-00

Submission Date: 27 /09/ 2022


NOTE
 Solve the following questions and upload/submit the same on eclass, submit only on eclass,
NO hardcopy submission is required.
 Your Solution can be either handwritten or typed.

Q1. For the sequence (12 13 5 6 67 34 23 78), apply the Median-of-3-partition-


QUICKSORT method to sort the sequence. Assume any values of i, j and k and show all the
steps.
QUICKSORT(A, 1, 8)
i = 1 (A[i] = 13); j = 4 (A[j] = 67); k = 5 (A[k] = 34)

Since A[i] <= A[k] and A[k] <= A[j] => I = k; Then exchange A[I] <=> A[r]

12 13 5 6 67 34 23 78 => 12 13 5 6 67 78 23 34
x = A[r] = 34; j = 4 (A[j] = 67); j = 4 (A[j] = 67);

12 13 5 6 23 78 67 34 (67 <=> 23);


12 13 5 6 23 34 67 78 (78 <=> 34);

Partition is done and q = 6; QUICKSORT(A, 1, 5); QUICKSORT(A, 7, 8) are to be executed;

QUICKSORT(A, 1, 4)
12 13 5 6 23 => i = 0 (A[i] = 12); j = 1 (A[j] = 13); k = 2 (A[k] = 5)

Since A[i] <= A[j] and A[k] <= A[i] => I = i; Then exchange A[I] <=> A[r]
23 13 5 6 12 (12 <=> 23);
5 13 23 6 12 (23 <=> 5);
5 6 23 13 12 (13 <=> 6);
5 6 12 13 23 (23 <=> 12);

Partition is done and q = 3; QUICKSORT(A, 1, 2); QUICKSORT(A, 4, 5) are to be executed;

QUICKSORT(A, 6, 8) as well as QUICKSORT(A, 1, 2) as well as QUICKSORT(A, 4, 5) will not


affect the array since the p will be equal r because they have 2 elements;

Sorted array: 5 6 12 13 23 34 67 78
Q2 For the same above sequence, apply INSERTION sort to arrange the sequence . Show all
the steps clearly

12 13 5 6 67 34 23 78

key = 13; i = 1 => 12 13 5 6 67 34 23 78;

key = 5; i = 2 => 12 13 13 6 67 34 23 78;


key = 5; i = 1 => 12 12 13 6 67 34 23 78;
key = 5; i = 0 => 5 12 13 6 67 34 23 78;

key = 6; i = 3 => 5 12 13 13 67 34 23 78;


key = 6; i = 2 => 5 12 12 13 67 34 23 78;
key = 6; i = 1 => 5 12 12 13 67 34 23 78;
key = 6; i = 2 => 5 6 12 13 67 34 23 78;

key = 67 => 5 6 12 13 67 34 23 78;

key = 34; i = 5 => 5 6 12 13 67 67 23 78;


key = 34; i = 4 => 5 6 12 13 67 67 23 78;
key = 34; i = 3 => 5 6 12 13 34 67 23 78;

key = 23; i = 6 => 5 6 12 13 34 67 67 78;


key = 23; i = 5 => 5 6 12 13 34 34 67 78;
key = 23; i = 4 => 5 6 12 13 23 34 67 78;

key = 67 => 5 6 12 13 34 67 23 78;

key = 78 => 5 6 12 13 34 67 23 78;

Sorted array: 5 6 12 13 23 34 67 78

You might also like