Data Structures and Algorithms (WS 13/14) Exercise01: Submitted by
Data Structures and Algorithms (WS 13/14) Exercise01: Submitted by
Submitted By
Problem 2:
Sequential search is the simplest approach of searching. It works as: you try every element in the collection until you have found the wanted element or until you reach the end of the collection. Binary search is a way that is more complicate than sequential search but would be more efficient in most cases. It checks the element in the middle of the collection. If the searched element is smaller or greater than the found element, then a sub-array is defined which is then searched again until the searched element is found or the collection is empty. Basing on these rules, lets see how they works on the following arrays: a) Searching for 17 by sequential search, we just try the elements one by one until we find 17. The process is as following: 2 3 5 8 11 12 14 17 However, binary search cannot be applied to this array, because this array is not sorted. After the array is sorted then the binary search can be applied. b) Searching for 41 by sequential search, we just try the elements one by one until we find 41. The process is as following: 5 >9 > 36 > 40 > 41 Binary search can also applied to this array. It works like: First, we find the middle element 69, as 69>41, we choose the first part as following: 5 9 36 40 41 43 second, we find the middle element of the new sub-array, 36, as 36<41, we got : 40 41 43 third, the middle element is 41, which is exactly the searched element.
Problem 3:
a) SelectionSort works as: search for the largest element and move it to the end of the list. Continue this process with the rest of the array. And it can be done as: search for the smallest element and move it to the beginning of the array. Continue this process with the rest of the array. InsertionSort works as: first, take the first element of the collection then a new set is created by the remaining elements. It consums one input element each repetition, and grows a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within
the sorted list, and inserts it there. It repeats until no input elements remain. Two main properties that can differentiate these two algorithms are: 1) SelectionSort compares the whole remaining elements in each round, while InsertionSort only takes a new element as input each time and make comparison among the first set of elements. 2) SelectionSort has identical number of comparisons for the best, average and worst cases i.e. the complexity of these three case is O(n2) while the number of comparisons of InsertionSort would vary for these three cases i.e. the complexity of the worse case and the average case are also of O(n2), but the complexity of best case is of O(n). And this feature leads to higher efficiency in some cases. In practice, InsertionSort is simple to implement and efficient for small data sets. And it is stable since it does not change the relative order of elements with equal keys. It can sort a list as it receives it, i.e. no need to know the whole list at the beginning. It also save space since it only requires a constant amount O(1) of additional memory space. So in general, InsertionSort is preferred since it is more efficient than SelectionSort in most cases. b) The interface Iterator<E> only provides the abstract methods to go through all the elements in a collection. Programmers can implement an Iterator to do stuff that they wish. And there are methods must be implemented: hasNext(), next() and remove(). So the iterator guarantees that you must provide some way to go through the whole element. But it does not guarantee that what the way should be.
Problem 4:
a) The name of the author is: C.A.R. Hoare b) Characteristic 1: all the data is to be sorted is stored in situ, with which you would interrupt the normal state of a system. And therefore the store would be filled with data to be sorted. This is no need to sort the input and output simultaneously. Characteristic 2: inside the store, the number of cycles of innermost comparison loop is closed to theoretical minimum and the loop can be made very fast. c) The author think that the QuickSort is likely to be a standard sorting method on most computers with a large enough random-access store to make internal sorting worthwhile.