Data Structures and Algorithms, Winter term 2016 Practice Assignment 2

Similar documents
Analysis of Binary Search algorithm and Selection Sort algorithm

Section IV.1: Recursive Algorithms and Recursion Trees

Data Structures and Algorithms Written Examination

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Lecture Notes on Linear Search

CSIS 202: Introduction to Computer Science Spring term Midterm Exam

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

For example, we have seen that a list may be searched more efficiently if it is sorted.

Pseudo code Tutorial and Exercises Teacher s Version

APP INVENTOR. Test Review

Algorithms. Margaret M. Fleck. 18 October 2010

Chapter 8: Bags and Sets

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

6. Standard Algorithms

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Chapter 7: Sequential Data Structures

Ordered Lists and Binary Trees

Symbol Tables. Introduction

Query Processing C H A P T E R12. Practice Exercises

Computational Geometry. Lecture 1: Introduction and Convex Hulls

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Data Structures Fibonacci Heaps, Amortized Analysis

Binary Heap Algorithms

Arithmetic Progression

Sequential Data Structures

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Loop Invariants and Binary Search

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

AP Computer Science Java Mr. Clausen Program 9A, 9B

Data Structures. Topic #12

HW4: Merge Sort. 1 Assignment Goal. 2 Description of the Merging Problem. Course: ENEE759K/CMSC751 Title:

Binary Search Trees CMPSC 122

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Computer Science 210: Data Structures. Searching

Sample Induction Proofs

Data Structures and Algorithms

Efficient Data Structures for Decision Diagrams

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

CS473 - Algorithms I

Recursion vs. Iteration Eliminating Recursion

Guessing Game: NP-Complete?

15. Functions and Lists. Example 2: Sorting a List of Numbers. Example 1: Computing the Diameter of a Cloud of Points

Regular Expressions and Automata using Haskell

Lecture 3: Finding integer solutions to systems of linear equations

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Data Structures. Level 6 C Module Descriptor

CompSci-61B, Data Structures Final Exam

CS 378 Big Data Programming. Lecture 24 RDDs

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Data Structures. Algorithm Performance and Big O Analysis

Parameter Passing in Pascal

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS

Sample Questions Csci 1112 A. Bellaachia

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

14.1 Rent-or-buy problem

8 Divisibility and prime numbers

4.2 Sorting and Searching

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Node-Based Structures Linked Lists: Implementation

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Lecture 5: Gate Logic Logic Optimization

Efficient representation of integer sets

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

The ADT Binary Search Tree

Lecture 13 - Basic Number Theory.

Intelligent Learning and Analysis Systems: Data Mining and Knowledge Discovery Prof. Dr. Stefan Wrobel; Dr. Tamas Horvath

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS

AP Computer Science AB Syllabus 1

2) Write in detail the issues in the design of code generator.

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Sorting Using the Xilinx Virtex-4 Field Programmable Gate Arrays on the Cray XD1

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Biostatistics 615/815

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Introduction to Data Structures

Converting a Number from Decimal to Binary

2x + y = 3. Since the second equation is precisely the same as the first equation, it is enough to find x and y satisfying the system

11 Multivariate Polynomials

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Kenken For Teachers. Tom Davis June 27, Abstract

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

2) Based on the information in the table which choice BEST shows the answer to 1 906?

Data Structures and Algorithms!

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Session 6 Number Theory

TREE BASIC TERMINOLOGIES

Data Structures and Algorithms!

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Data Structures and Data Manipulation

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Transcription:

German University in Cairo Media Engineering and Technology Prof. Dr. Slim Abdennadher Data Structures and Algorithms, Winter term 2016 Practice Assignment 2 Exercise 2-1 Sorting Algorithms Given the following array of integers: 11 5 14 10 2 Apply each of the following methods to sort this array: Bubble Sort Selection Sort Insertion Sort In each step, you are required to: clearly describe the current step being performed show the resulting contents of the array Please note that you are not required to write a program. Exercise 2-2 Recursive Bubble Sort Re-implement bubble sort algorithm such that you compensate for the outer loop with recursion. Exercise 2-3 Modified Selection Sort In the lecture you were introduced to the Selection Sort algorithm. For this exercise, you need to implement a modified version of the Selection Sort algorithm, where: instead of finding the smallest element in the array and changing its position with an element at the beginning of the array, you need to find both the smallest and the largest elements and change their positions with elements at the beginning and end of the array respectively. You are required to select the smallest and largest numbers in one pass; in other words, your implementation should only include a single loop for finding both of the smallest and largest numbers. Remark: There are some special cases that you need to figure out and handle in your implementation. Make sure that your solution works for the arrays: What is the invariant of this sorting algorithm? {6, 4, 5} and {2, 6, 8, 1, 7, 9, 4, 3, 5} 1

Exercise 2-4 Index Sort a) Write a method indexsort that uses the following algorithm to sort an array x of integers. Loop through the integers in x. For each integer, calculate what index it will be stored at in the output array y of the same length as x. For example, for the following array: 4 3 8 2 by comparing 4 with the rest of the array, we can deduce that 4 should be stored at index 2. Thus, after the first pass the resulting array should be of the following form: Make sure that your algorithm handles arrays with duplicate keys. b) What is the time complexity of your algorithm? c) What is the invariant of your algorithm? 4 Exercise 2-5 Swap Sort Swap Sort is similar to Bubble Sort, although during the first pass the first and second elements are compared (and swapped if necessary), then the third and fourth elements are compared, and so on until the end of the list is reached. During the second pass the first element is skipped and the second and third elements are compared, then the fourth and fifth, and so on. Repeating these steps for X times will result in sorting the elements. You are required to: a) write a method sort that implements the Swap Sort algorithm for sorting an array of integers, b) find the minimum value of X in terms of the length n of the array to be sorted. Exercise 2-6 Shaker Sort Shaker Sort is a simple optimisation of the Bubble Sort, that performs passes in both directions (simultaneously), allowing out-of-place items to move fast across the whole array. Shaker Sort performs the following steps: 1. Examines pairs of items, starting from the beginning of the array and performs swapping if necessary. 2. Examines pairs of items, starting from the end of the array and performs swapping if necessary. 3. Repeats the previous two steps until the array is sorted. Consider the following array: Example: a) From the beginning: Examine a[0] and a[1]. 7 5 11 10 8 2

Compare the contents of this pair (7 and 5): swap contents. Resulting array: 5 7 11 10 8 Examine a[1] and a[2], compare the contents of this pair (7 and 11): do not swap contents. Examine a[2] and a[3], compare the contents of this pair (11 and 10): swap contents. Resulting array: 5 7 10 11 8 Examine a[3] and a[4], compare the contents of this pair (11 and 8): swap contents. Resulting array: 5 7 10 8 11 b) From the end: Examine a[4] and a[3], compare the contents of this pair (11 and 8): do not swap contents. Examine a[3] and a[2], compare the contents of this pair (8 and 10): swap contents. Repeat until array is sorted. Write a method shakersort to implement this algorithm. Exercise 2-7 The odd-even Sort The odd-even sorting algorithm is based on repeatedly making two passes on an array: 1. The first pass examines pairs of items a[j], a[j+1] where j is odd. 2. The second pass examines pairs of items a[i], a[i+1] where i is even. 3. In each pass, the following occurs: if the contents of the pair being examined are un-ordered, then the contents are swapped. 4. Repeat passes until array is sorted. Consider the following array: 1 7 5 2 6 First Pass a) Start with Odd positions: Examine a[1] and a[2], compare contents of this pair (7 and 5): swap contents. Resulting Array: 1 5 7 2 6 Examine a[3] and a[4], compare contents of this pair (2 and 6): do not swap contents. b) Then with Even positions: Examine a[0] and a[1], compare contents of this pair (1 and 5): do not swap contents. Examine a[2] and a[3], compare contents of this pair (7 and 2): swap contents. Resulting Array: 1 5 2 7 6 Repeat until array is sorted. Write a method oddevensort that implements the odd-even sorting algorithm. 3

Exercise 2-8 Counting Sort a) Write a method booleansort that uses the following algorithm to sort an array x of positive integers. It should use another boolean flag array of as many elements as the maximum value in array x plus 1. This boolean array will be used to denote the presence of the elements of array x if found. Thus the algorithm, should loop through the integers in x. For each integer i of the array, the flag of index i of the boolean array should be set to true. Then the algorithm should reconstruct the sorted array from the true values of flag depending on their locations. b) How can you modify your algorithm to handle arrays with duplicate values? (Hint: Call it countingsort!) c) What is the time complexity of your algorithm? d) What are the drawbacks of the algorithm described above? e) What is the invariant of your algorithm? Exercise 2-9 Student Records You are required to implement a sorting algorithm and use it to sort some student records. Each record is composed of: Name Student ID GPA Your program should sort a list of students, according to their names. Exercise 2-10 Midterm Exam 2013 Suppose that while your computer is sorting an array of objects, its memory is struck by a cosmic ray that changes exactly one of the keys to something completely different. For each of the sorting algorithms discussed in the lectures, what is the worst-case possibility in terms of the ordering of the final array? For each sorting algorithm the final array could x) not even close to sorted, y) have just one or two keys out of place, or z) consist of two separate sorted subsets, one following the other, plus perhaps one or two additional keys out of place. Choose x), y), or z) for the following algorithms. Justify your answers by examples. a) Insertion sort b) Selection sort c) Bubble sort 4

Exercise 2-11 Midterm Exam 2015 Suppose we have an array a containing n integers. Initially the integers are not sorted. We would like to rearrange the array in ascending order using the following Bogosort algorithm. Bogosort simply shuffles elements of the array randomly until it is sorted. The Bogosort is only a theoretical concept, which has no use in practical applications. The Bogosort is also know as the Slow Sort or Stupid Sort. Algorithm in Pseudocode: bogosort(array) while!issorted(array) shuffle(array) a) Write a Java method void bogosort(int[] a) that takes an array a and sorts the array based on the above described algorithm. Please note that the shuffling operation should be done in every pass for all numbers of the array randomly. You are requested to implement the shuffle and the issorted methods. 5

b) Given an intially unordered array 1. What is the best case complexity of the algorithm. Justify your answer. 4cm 2. What is the worst case complexity of the algorithm. Justify your answer. 4cm 3. What is the invariant of the algorithm? Justify your answer. 6