CS253

Ace your homework & exams now with Quizwiz!

logN (logarithmic execution time)

solves the original problem by transforming it into a smaller problem. cuts size of input by some constant fraction, program gets slightly slower as N grows.

Explain the notion of a position.

a position of an item does not change if a rank of this item changes as a result of adding or removing a previous item in the sequence. Also, a position will not change if an item currently in that position is removed from the sequence. Since position is defined in relation to data before and after, we must throw InvalidPositionException at each method.

algorithm

a precise set of instructions for solving a particular task

data structure

any data type (or representation) with its associated operations. (int double char)

Similarity between merge and quicksort

Both sorting algorithms have an average Big O of "n log n" where n is the number of items to be sorted. They both also have sorting algorithms that takes an approach similar to a "divide and conquer" setup.

Levels of abstraction

1)Physical: =The lowest level of abstraction describes how the data are actually stored. The physical level describes complex low-level data structures in detail. 2)Logical:The next-higher level of abstraction describes what data are stored in the database, and what relationships exist among those data. The logical level thus describes the entire database in terms of a small number of relatively simple structures. 3)View level:The highest level of abstraction describes only part of the entire database. The variety of information stored in a large database. Many users of the database system do not need all this information; instead, they need to access only a part of the database. The view level of abstraction exists to simplify their interaction with the system.

Describe the Ranked Sequence ADT (give a definition, set of operations)

A ranked sequence is a collection of items arranged in a linear order, where each item has a rank defining the relative ordering of that item in the sequence. returnItem (rank) Returns the item at the specified rank replaceItem (rank, newItem) Replaces the item at the specified rank with newItem insertItem (rank, newItem) Inserts newItem at the specified rank deleteItem (rank) Deletes the item at the specified rank

Explain how the efficiency of recursive algorithms is defined. Consider two examples: the recursive binary search, and the tower of Hanoi problem and compare their efficiencies.

A recursive algorithm is an algorithm which calls itself within the program. Therefore, the efficiency of a recursive algorithm is defined by how many times the algorithm calls itself. Recursive algorithms solve smaller problems to reach the result. Both binary search and tower of Hanoi are divide and conquer algorithms, so they use this method. The time complexity of Tower of Hanoi is always O(2^n) because only one disk can be moved at a time, so three steps are involved at each level of recursion. The time complexity of binary search is log(n) because the list is broken in half during every iteration, making it more efficient than the tower of Hanoi. The best case for binary search is O(1), if the target is already centered in the list.

abstract data type

An abstract data type, sometimes abbreviated ADT, is a logical description of how we view the data and the operations that are allowed without regard to how they will be implemented. This means that we are concerned only with what the data is representing and not with how it will eventually be constructed. By providing this level of abstraction, we are creating an encapsulation around the data. The idea is that by encapsulating the details of the implementation, we are hiding them from the user's view. This is called information hiding.

Define the big-O notation and explain how well it allows us to classify algorithms from a real- world perspective

Big-O notation is used to describe the running time and space complexity of a given algorithm, it tells us the upper bound of the function (the limiting behavior of the function). It can be represented via best-case, worst-case or average-case.

Explain Bubble Sort

Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items. Best-case is O(n) given ordered dataset.

Describe the Positional Sequence ADT (give a definition, set of operations).

Definition A positional sequence is a collection of items arranged in a linear order, where each item has a position defined in terms of the neighboring nodes. Positions (contrary to ranks) are defined relatively to each other, and are not tied to items. Operations (methods) on positional sequences: replace (position, item) Replaces data at position with item swap (position1, position2) Swaps items in positions 1 and 2 first () Returns the position of the first item in the sequence last () Returns the position of the last item in the sequence before (position) Returns the position of the item preceding the one in position after (position) Returns the position of the item following the one in position

some ways algorithms can be compared:

EXECUTION (runtime), space (memory needed), CORRECTNESS, clarity, etc. (caps most important).

Assume that we are searching a large data base with 4 million items represented as an ordered array. How many probes into the list will the binary search require before finding the target or concluding that it cannot be found? Assume that it takes 1 microsecond to access an item, estimate the execution time of the binary search.

Given an ordered array, binary search will have a complexity of log(n), with a base 2. Therefore, the number of comparisons is 22 given log2(4,000,000) = 22. To find the execution time, we must multiply log2(4,000,000) by the time needed to access an item (1x10^-6 seconds), therefore it will take 22 microseconds.

Describe merge sort

Merge sort is based on Divide and conquer method. It takes the list to be sorted and divide it in half to create two unsorted lists. The two unsorted lists are then sorted and merged to get a sorted list. The two unsorted lists are sorted by continually calling the merge-sort algorithm; we eventually get a list of size 1 which is already sorted. The two lists of size 1 are then merged.

Describe the divide-and-conquer search algorithm and explain its efficiency

In divide and conquer algorithms first we divide the problem into the smaller problems and then when we reach a particular state or the base case we start manipulations and combine the results from the smaller problem to get the final result which is called conquer. Efficiency depends on the data or the problem that means if the problem is something that can not be solved by divide and conquer then there is no meaning in using this algorithm technique. but if we can use this is more efficient and easy to understand which is why binary search is so efficient given large data.

Compare the array-based and the doubly linked list implementation of the Ranked Sequence ADT

Nodes in a doubly linked list have pointers to both, the next node and the previous node. For simplicity, we can add two dummy nodes to the list: the header node, which comes before the first node, and the trailer node, which comes after the last node.

rank Big-O efficiencies

O(1) < O(logn) < O(n) < O(nlogn) < O(n^x) < O(x^n) < O(n!)

Describe quick sort

Quick sort is a divide and conquer recursive algorithm that picks one element in the array to be the pivot, partitions each time so that the pivot is in the proper place, and entries to the left are smaller and entries to the right are larger than the pivot.

Explain Selection Sort

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right. This algorithm is not suitable for large data sets as its average, best and worst case complexities are of Ο(n2), where n is the number of items. It is not stable, but it's the only one of the three simple to guarantee linear exchanges.

Compare the 3 simple sorting methods assuming that your application requires a stable sorting method, i.e. a method that does not change the relative ordering of elements that are equal. Which of these sorting methods would you choose, and why? (Hint: consider sorting based on the following integer keys 19, 14(1), 7, 12, 14(2), 10)

Selection sort is automatically out of the running given that it is not a stable sorting method because the order of elements does not affect the sorting time. If the array is partially sorted, still each element is compared and there is no breaking out early. Even though both the bubble sort and insertion sort algorithms have average case time complexities of O(n2), bubble sort is almost all the time outperformed by the insertion sort. This is due to the number of swaps needed by the two algorithms (bubble sorts needs more swaps). But due to the simplicity of bubble sort, its code size is very small. Also there is a variant of insertion sort called the shell sort, which has a time complexity of O(n3/2), which would allow it to be used practically. Furthermore, insertion sort is very efficient for sorting "nearly sorted" lists, when compared with the bubble sort.

Describe Shell sort (example is always helpful). What advantage do the relatively prime values of the increments have over other values?

Shell short is effectively an improved version of insertion sort. The difference is that shell short allows us to insert the element to a position that is farther away that one position ahead (as in insertion sort). We decide a value h and create an array that is h-sorted. Then we reduce h and repeat the process until it is equal to 1. Shell short is efficient for medium sized lists, and has an average efficiency of O(n^2).

How important is the choice of the pivot in Quick sort?

The choice of pivot in quick sort is essential because it determines if the program will run efficiently, or in quadratic time.

Provide examples of best case and worst case data sets for Shell sort, and compare its efficiency in these cases to the best case and worst case efficiency of Quick sort.

Shell sort best case efficiency would be O(n) for an already sorted list. However, when the list is nearly sorted, it is not as efficient. The worst case in O((logn)n^2) or O(n^3/2) depending on the gap sequence you use. This would be when it is h/2, making repeated pointless comparisons, making it very inefficient. The worst case of quick sort occurs when the pivot is always either the greatest or largest element in the unsorted list (sorted in ascending or descending). Here, we have a complexity of O(n^2). The best case occurs when in the special case where the pivot perfectly divides the list into two equal halves every time, complexity of O(nlogn).

Describe the Stack ADT (give a definition, set of operations).

Stack is a linear data structure in which the insertion and deletion operations are performed at only one end. In a stack, adding and removing of elements are performed at single position which is known as "top". That means, new element is added at top of the stack and an element is removed from the top of the stack. In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) principle. Push(n) inserts item at top of stack pop(n) removes item from top of stack

differences in quick and merge

The Merge Sort is sometimes known as an external sort. This sorting algorithm is usually required when sorting a set that is too large to hold or handle in internal memory. Basically the merge sort will divide the set into a number of subsets of one element and then repeatedly merge the subsets into increasingly larger subsets with the elements sorted correctly until one set is left. Usually this method means that the actually sorting ultimately deals with only portions of the complete set. The Quick Sort operates quite differently from the Merge Sort. In many cases, implementing the Quick Sort often yields a faster sort than other Big O "n log n" sorting algorithms. The Quick Sort operates by selecting a single element from the set and labeling it the pivot. The set is then reorder to ensure that all elements of lesser value than the pivot come before it and all elements of greater value come after it. This operation is recursively applied to the subsets on both sides of pivot until the entire set has been deemed, sorted.

Data Structure

The actual implementation of ADT. The implementation of an abstract data type, often referred to as a Data structure, will require that we provide a physical view of the data using some collection of programming constructs and primitive data types.

Define the Double-Ended Queue ADT.

The double-ended queue (or dequeu) supports insertions and deletions at the front and at the rear of the queue. Therefore, in addition to enqueue, dequeue (which we call now insertLast and deleteFirst, respectively), and first methods, we must also provide insertFirst, deleteLast and last methods.

Explain Insertion Sort

This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2), where n is the number of items. Best case is O(n) given ordered dataset.

how does knowledge fo run time efficiency helps us decide which algorithm is the best for a given application

This knowledge is helpful because given the time efficiency, we can decide how to better implement our algorithm or which algorithm to choose for the program. We can use the worst-case running time and space complexity to decide which algorithm this may be.

Explain why it is important to know the run time efficiency of algorithms

is important to know the run-time efficiency of algorithms because it tells us the behavior of algorithms and how they will handle given inputs and helps us to create the best algorithm to solve specific problems. It tells us the time it takes for the algorithm to execute in main memory.

Queue ADT

queue is one of the linear data structure in which elements are inserted from one location known as rear and deleted from another location known as front. queue is also known as FIFO (FIRST IN FIRST OUT) operations which are performed on queue public void enqueue (int item); insert item from rear public int dequeue(); delete item from front public int size(); return size public boolean empty(); return whether list is empty public int front(); return front

Describe Radix Sort

radix sort is a non-comparative sorting algorithm. It avoids comparison by creating and distributing elements into buckets according to their radix. For elements with more than one significant digit, this bucketing process is repeated for each digit, while preserving the ordering of the prior step, until all digits have been considered

Explain and compare array and linked list implementations of the Stack ADT.

the major problem with the stack implemented using array is, it works only for fixed number of data values. That means the amount of data must be specified at the beginning of the implementation itself. Stack implemented using array is not suitable, when we don't know the size of data which we are going to use. A stack data structure can be implemented by using linked list data structure. The stack implemented using linked list can work for unlimited number of values

leading term

the term with the highest degree, this term defines the run time

Explain and compare array and linked list implementations of the Queue ADT

using array: when queue is defined using array the size of the queue is to be defined at the begining using #define statement. in this case the size of the queue can't be changed during execution, so there may be a case that elements may get wasted or it may fall short. using linked list: when queue is defined using linked list then it is defined by using pointers


Related study sets

Троцкий Дм. "Я - не Я"

View Set

Systems and Application Security

View Set

Chapter 27 Anger, Aggression, and Violence

View Set