Searching and Sorting

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

avg num of passes in selection sort

n-1

num of passes to sort in bubble sort

n-1

Avg. number of comparisons per pass in insertion sort (linear worst case)

n/2

avg comps of linear search

n/2

avg num of comparisons per pass in selection sort

n/2

avg num of comps per pass in bubble sort

n/2

Avg. number of comparisons per pass in insertion sort (linear avg case)

n/4

Merge Sort: Total comparisons

(n-1)(log_2(n))

total num of comparisons in insertion sort (binary)

(n-1)log_2(n/2)

total comparisons in bubble sort

(n-1)n/2

total num of comparisons in insertion sort (linear worst case)

(n-1)n/2

total num of comparisons in selection sort

(n-1)n/2

total num of comparisons in insertion sort (linear avg case)

(n-1)n/4

Insertion Sort Algorithm (unsorted)

- look at the second item in a list - compare it to all items before it and insert the item into the right place - repeat the second step for the rest of the items until the last item in the list has been inserted into the correct place

how to find order

1) ignore lower degree terms 2) ignore constants

Merge Sort Algorithm unsorted (is list size 8)

1. Merge list1 and list2, list3 and list4, etc. using the algorithm above to get 4 sorted lists of length 2 2. Repeat: Merge the first 2 lists, then the next two, to get 2 sorted lists of length 4 3. Repeat until you have one sorted list

Bubble Sort Algorithm

1. Take the first element and second element from the list and compare them, IF element 1 > element 2 THEN swap them 2. Repeat: Moving along the list to the next pair until the end (which would make it one pass) 3. do this until you have moved through the entire list and not made any changes

Merge Sort: #comp per pass for list size 32:

16 24 28 30 31

Merge Sort: #comp per pass for list size 8:

4 6 7

Merge Sort: #comp per pass for list size 16:

8 12 14 15

Merge Sort: Avg. number of comparisons per pass

< n-1

quick sort total comps

<= (n-1)log_2(n)

Quick Sort num of comps

<= n-1

(Binary & Sequential Search)What are the advantages/disadvantages to using a recursive binary search over a sequential search algorithm?

A disadvantage to a recursive binary search algorithm is that it must already be sorted in ascending order prior to searching for a value. An advantage to a recursive binary search is that it is much more efficient and requires less comparisons than the sequential search algorithm. Binary search requires log2 n comparisons where in the worst case scenario all elements in an array must be compared in a sequential search algorithm.

selection sort algorithm

A sorting algorithm that keeps moving smaller items toward the front of the list

Is quick sort any faster or slower if the original list is in order at the start?

Faster because no data movements but still O(NlogN)

Is merge sort any faster/slower if the original list is sorted at the start?

Faster because there are less comparisons: N/2 vs. N-1, but the same number of data movements. Order is the same: O(nlog(n))

Algorithm to merge two sorted lists:

Make a new list to hold sorted list Loop Compare first of each list Move smaller one to new list Until (one of lists is empty) Add other list to end of sorted list

How can you speed up the insertion sort if the list is initially sorted?

If a backward linear search is used instead of a forward linear search, then for each pass only one comparison is needed, resulting in a total number of comparisons equal to (N-1). The order of the search becomes O(N).

order of binary search

O(log_2(N))

oder in insertion sort (binary)

O(n log_2(n)

Order of linear Search

O(n)

oder in insertion sort (linear avg case)

O(n^2)

oder in insertion sort (linear worst case)

O(n^2)

order of bubble sort

O(n^2)

order of selection sort

O(n^2)

Merge Sort: Order of sort

O(nlog_2(n))

quick sort order

O(nlog_2(n))

Quick Sort Algorithm

Pick a pivot element. Rearrange the array so that all elements smaller than the pivot come before it, and all elements larger come after. Recursively go through each half (the half before the pivot, and the half after), and repeat.

Explain the factors that should determine which type of sorting algorithm to use.

Size of the array or ArrayList, run-time efficiency of the sorting algorithm, and space efficiency of the sorting algorithm

avg num of passes in insertion sort (linear avg case)

n-1

avg num of passes in insertion sort (linear worst case)

n-1

List the advantages and disadvantages of using the merge sort

advantages: No matter the ordering of the data, it is efficient disadvantages: It creates a temporary array the size of the original which can use a great amount of memory

List the advantages and disadvantages of using the quick sort

advantages: When the array is divided evenly, it sorts large set of data quickly disadvantages: If the array is partitioned unevenly, it becomes an inefficient sort and if the pivot is the smallest or largest value in the array, it becomes a slow recursive sort.

List the advantages and disadvantages of using the insertion

advantages: good for small sets of data and creates a sorted and unsorted portion of the array disadvantages: it takes many passes to sort

List the advantages and disadvantages of using the selection sort.

advantages: useful for small sets of data disadvantages: It makes many passes to sort

what is order

ballpark of how fast it is

is bubble sort any faster/slower if the original list is sorted at the start?

faster because less data movements

Merge Sort: num of passes to sort whole list

log_2 (n)

Merge Sort: the number of passes:

log_2 (n)

Quick Sort num of passes

log_2(n)

avg comps of binary search

log_2(n)

Avg. number of comparisons per pass in insertion sort (binary)

log_2(n/2)

how to linear search

look at each unsorted or sorted element in order until reach one you are looking for

Merge Sort: Avg. number of data movements per pass

n

Merge Sort: the average number of comparisons per pass is approximately (in terms of N):

n

avg num of passes in insertion sort (binary)

n-1

is selection sort or bubble sort faster?

selection sort is faster because of less data movements

does binary search need to be in order

yes


संबंधित स्टडी सेट्स

Square Roots and Irrational Numbers

View Set

Chapter 10 - Organizational Design and Control

View Set