Activity 1

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Given array 8, 5, 3, 7, 1, 6, 4, 2. List array to show the array changes in memory. You may list out array after each merge call is terminated.

5 8 3 7 1 6 4 2 5 8 3 7 1 6 4 2 3 5 7 8 1 6 4 2 3 5 7 8 1 6 2 4 3 5 7 8 1 2 4 6 1 2 3 4 5 6 7 8

What is the running time of merger sort?

O(n log n)

What is the running time of quick sort?

O(n log n)

What is the running time of merger?

O(n)

What is the running time of partition?

O(n)

Modify the insertion sort algorithm with an extra argument, named isAscend, of Boolean type. If isAscend is true, then the array will be sorted in ascend order; otherwise, the array will be sorted in descend order.

Procedure insertionSort(arr, size, isAscend) For(position = 1; position < size; position++) Key = arr[position] Previous = position - 1 While(previous >= 0 and (isAscend && arr[previous] > key || !isAscend && arr[previous] < key)) Arr[previous+1] = arr[previous] Previous-- Arr[previous+1] = key End Procedure

Write the following algorithms. The algorithms are all named isSorted. We assume an array object know its size, The first algorithm returns true if the array is sorted either in ascend order or in descend order; returns false otherwise. The algorithm takes an int array as argument.

Procedure isSorted(arr, size) if (size <= 1) return true i = 0; while(i < size-1 && arr[i] == arr[i+1] ) i++ // ignore equal elements at begin if(i == size-1) return true if(arr[i] < arr[i+1]) isAscend = true else ifAscend = false for(i++; i < size-1; i++) if(isAscend && arr[i] > arr[i+1]) return false if(!isAscend && arr[i] < arr[i+1]) return false return true

Problem Four (10 points, 5 points each) Write the following algorithms. The algorithms are all named isSorted. We assume an array object know its size, The second algorithm takes an int array and a Boolean value as arguments. If the Boolean value is true, then the algorithm returns true if the array is sorted in ascend order; If the Boolean value is false, then the algorithm returns true if the array is sorted in descend order; The algorithm returns false otherwise

Procedure isSorted(arr, size, isAscend) for(I = 0; i < size-1; i++) if(isAscend && arr[i] > arr[i+1]) return false if(!isAscend && arr[i] < arr[i+1]) return false return true End Procedure

Modify the select sort algorithm with an extra argument, named isAscend, of Boolean type. If isAscend is true, then the array will be sorted in ascend order; otherwise, the array will be sorted in descend order.

Procedure selectionSort(arr, size, isAscend) For(position = 0; position < size - 1; position++) extremaIndex = position For(scan = position+1; scan < size; scan++) If(arr[scan] < arr[extremaIndex] && isAscend Or arr[scan] > arr[extremaIndex] && !isAscend) extremaIndex = scan Swap arr[position] and arr[extremaIndex] End Procedure

You are not required to write code. In fact, you should not write code here. You should just write the algorithm. Also, you may need another algorithm so called the driver for BinarySearchDriver which simply call BinarySearchRecursive appropriately to search the given array for given key.

Solution: Procedure BinarySearchRecursive(arr, begin, end, key) if begin > end return -1 mid = (begin+end)/2 if(arr[mid] == key) return mid if(arr[mid] < key) return BinarySearchRecursive(arr, mid+1, end) else return BinarySearchRecursive(arr, begin, mid-1) End Procedure Procedure BinarySearch(arr, size) Return BinarySearchRecursive(arr, 0, size-1)

a. Order the following big O notation, from the fastest running time to slowest running time. 𝑂 (1000), 𝑂(2^n), 𝑂 (𝑛 ln 𝑛), (2𝑛^2), 𝑂 (𝑛)

Solution: 𝑂 (1000) < 𝑂 (𝑛) < 𝑂 (𝑛 ln 𝑛) < 𝑂 (2𝑛^2) = 𝑂 (𝑛^2) < 𝑂(2^n)

Problem Two (8 points, 4 points each) Consider the following array: 12, 13, 15, 15, 16, 16, 19, 23, 25, 26, 30, 31 ,32 If use binary search algorithm from your textbook with search key 16, what is the return value?

Solution: 4.

Consider the following array: 12, 13, 15, 15, 16, 16, 19, 23, 25, 26, 30, 31 ,32 If use linear search algorithm from your textbook with search key 16, what is the return value?

Solution: 4. Assume array index starts 0.

According to your book, "...To partition the input, quicksort chooses a pivot to divide the data into low and high parts. The pivot can be any value within the array being sorted, commonly the value of the middle array element." Now rewrite the partition algorithm by using the last element of the subarray as the pivot.

Solution: Obvious. Simply change one statement: pivot = numbers[midpoint] to pivot = numbers[k]. Also, delete midpoint = i + (k-i)/2.

How many comparisons are needed to sort a list of 2048 elements

n log2 n= 2048 log2 2048 = 2048 ∗ 11 = 22528

How many times the partition algorithm will be executed in a quick sort? Assume the array size is n. Justify your answer.

n partition since each partition actually put one element in its final position. We need to place n element in their final positions, so we need n partitions.

How many times the merger algorithm will be executed in a merger sort?

n-1

Determine big O notation for function f(n) = 1000n + 0.1n^2 + n ln x Solution:

𝑂 (𝑛^2)

How many comparisons are needed to sort a list of 2048 elements?

𝑛 log2 𝑛 = 22528


Ensembles d'études connexes

HPS 490 Unit 1: Neuromuscular Physiology

View Set

Unit 14: Fiscal Policy and Trade

View Set

CHAPTER; FORTY-FOUR; NETWORKING AND THE LIAISON FUNCTION

View Set

Respiratory/Endocrine - Morcombe Dynamic Quiz

View Set

Computer Science mid-term (multiple choice)

View Set

Voice Disorders- Laryngeal Cancer

View Set