COSC 102: Exam 1 Review - Sorting

Ace your homework & exams now with Quizwiz!

How it Works: Binary Search

Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array

How it Works: Insertion Sort

Builds the final sorted array, one item at a time. Not very effective on longer lists, but efficient for smaller data sets. It iterates, consuming one input element each repetition, and growing a sorted output list. At 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 element remains.

Best and Worst Case Scenario: Insertion Sort

Compared to selection sort, it runs much faster if a list is already close to being sorted. Insertion sort's advantage is that it only scans as many elements as it needs in order to place the k + 1st element, while selection sort must scan all remaining elements to find the k + 1st element. Insertion sort's run time varies greatly, however.

How it Works: Merge Sort

Efficient, comparison (longer over time due to linear memory space) based sorting algorithm. Uses a divide and conquer strategy. Sort works as follows; divides the unsorted list into n sublists, each containing 1 element (1 element list = sorted.) Repeatedly merge sublists to produce new sorted sublist until there is only one sublist remaining. This will be the sorted list.

How it Works: Selection Sort

Finds smallest (or largest, depending on sort order) element in unsorted sublist, and swaps it with the left most unsorted element. Sublist boundaries move right and repeat. Goes through list, finds min, swaps with leftmost element.

Run Time: Binary Search

Lower Bound: Omega(1) Upper Bound: O(n log(n))

Run Time: Bubble Sort

Lower Bound: Omega(n) Upper Bound: O(n^2)

Run Time: Insertion Sort

Lower Bound: Omega(n) Upper Bound: O(n^2)

Run Time: Selection Sort

Lower Bound: Omega(n^2) Upper Bound: O(n^2)

Run Time: Merge Sort

Lower Bound: omega(n log(n)) Upper Bound O(n log(n))

Run Time: Quick Sort

Lower Bound: omega(n log(n)) Upper Bound: O(n^2)

Space Complexity: Binary Search

O(1)

Space Complexity: Bubble Sort

O(1)

Space Complexity: Insertion Sort

O(1)

Space Complexity: Selection Sort

O(1)

Space Complexity: Quick Sort

O(logn(n))

Space Complexity: Merge Sort

O(n)

Best and Worst Case Scenario: Quick Sort

Problem: if the pivot was 18 or 3, you don't get nice pivot breakdown. You would end up with 18 and then everything else. N - 1 becomes your pivot. To pick a median you basically do a variation of quicksort. To be short, you just have to hope it works. It doesn't take much space and in the worst case it has n(Squared) performance. Worst case is pretty bad, but on average if works fine.

Sorting

Provide good context for applying our analysis in terms of time complexity because we have different approaches for certain problems and what the time consequences are. Order does matter.

How it Works: Quick Sort

Quicksort is a divide and conquer algorithm. First, it picks an element, called a pivot, from the array. Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm's performance.

Best and Worst Case Scenario: Binary Search

Sorted arrays with binary search are a very inefficient solution when insertion and deletion operations are interleaved with retrieval, taking O(n) time for each such operation, and complicating memory use.[18] Other data structures support much more efficient insertion and deletion, and also fast exact matching. However, binary search applies to a wide range of search problems, usually solving them in O(log n) time regardless of the type or structure of the values themselves.

How it Works: Bubble Sort

Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one is smaller than the former one.) Move on the next item in list and repeat.

Recursion

Take a problem, and you think about it as a smaller version of that problem. Solve this problem by solving a smaller version of the same problem

Mod

The % modulus operator is an additional arithmetic operation: basically the remainder left over after division. For example, what is 73 % 10? The simplest way to think about it is, keep subtracting 10's from 73 until there's less than 10 left (3 in this case).

Best and Worst Case Scenario: Bubble Sort

The only significant advantage that bubble sort has over most other implementations, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted efficiently is built into the algorithm. When the list is already sorted (best-case), the complexity of bubble sort is only O(n). By contrast, most other algorithms, even those with better average-case complexity, perform their entire sorting process on the and thus are more complex. Bubble sort should be avoided in the case of large collections.

Run Time

The time during which a program is running.

Best and Worst Case Scenario: Merge Sort

This sorting algorithm uses extra memory. If you get improvement in time, often it means you are using extra memory. Rarely you can cut down on both. Merge sort uses linear space. Run time is linear, not worse than linear but it is linear. You need to put n things in place and for each of them how much work is it to put them in n place. The fact both lists are sorted makes merging much faster.

Best and Worst Case Scenario: Selection Sort

Typically, along with insertion sort, faster on short arrays (fewer than 10-20 elements) With long arrays, it is outperformed by merge sort. Almost always better than bubble sort.


Related study sets

Unit 6 (Ryan) African Americans in the British New World

View Set

Chapter 24 recall questions (BIRD)

View Set

COMPLETE RESEARCH METHODS STUDY PACK: ALL QUESTIONS FROM THE TEXT

View Set

Organizational Behavior Final Exam

View Set

matilda the musical lines: miss honey

View Set

Lesson 7 - Winning the War in Europe (100%)

View Set

Chapter 7 Arrays and Vectors Review Questions.

View Set