• No results found

Algorithms and Data Structures for Data Science. Sorting

N/A
N/A
Protected

Academic year: 2021

Share "Algorithms and Data Structures for Data Science. Sorting"

Copied!
33
0
0

Loading.... (view fulltext now)

Full text

(1)

Department of Computer Science

Algorithms and Data Structures for Data Science

CS 277

Brad Solomon

September 27, 2021

Sorting

(2)

mp_train due Wednesday!

Optional extra credit will be worth extra credit, currently 0 points Can request a 24-hour extension policy

https://forms.gle/1KreTgcfJb1fXQeQ6

Request one-on-one office hours by emailing with a list of times

(3)

Reminder: Sign up for exam 1!

Sign up at: https://cbtf.engr.illinois.edu/sched

Email DRES accomodations: http://[email protected]/

(4)

Exam 1: Python Fundamentals

Conditionals: How to write, how to read, relation to logic

Lists: How to look up elements, how to write 1D and 2D lists

Loops: How to write and evaluate loops

(5)

Exam 1: Logic

Logic Operators: Understand truth tables and underlying meaning Logic Equivalencies: Negation, converse, contrapositive

Compound propositions and predicates How to write, read, evaluate and code:

Quantifiers and compound quantifiers

(6)

Exam 1: Lists

List ADT: How to write and use common list functions

Linked List: How to write and use, Big O efficiency of methods Array List: How to write and use, Big O efficiency of methods

(7)

Exam 1: Stacks and Queues

How to write and use, Big O efficiency, how order is maintained

(8)

Exam 1: Coding Questions

You will have a choice of coding questions on exam

1. Code up a compound predicate with quantifiers

2. Create a 2D matrix with a specific set of values

(9)

Learning Objectives

Motivate the need for sorting

Sample the space of available sorting algorithms Introduce recursion

(10)

The Sorting Problem

Input:

Output:

8 4 3 1 2 5 6 9 0 7 0 1 2 3 4 5 6 7 8 9

Given a collection of objects, , with comparable values, order the objects such that

∀x ∈ C, xi C≤ xi+1

(11)

Sorting leads to efficient searching

0 1 2 3 4 5 6 7 8 9 8 4 3 1 2 5 6 9 0 7

Search(7)

(12)

Sorting leads to better visualization

(13)

Sorting is a fundamental problem in CS

Many algorithms begin with or include a sorting step

Fundamental sorting algorithms are great for mastering concepts

Sorting algorithms are a classic introduction to algorithms

(14)

Optimizing sort is an ongoing challenge

GraySort: Sort rate (TBs / minute) achieved while sorting a very large amount of data (currently 100 TB minimum).

CloudSort: Minimum cost (Dollars) for sorting a very large amount of data on a public cloud. (currently 100 TB).

MinuteSort: Amount of data that can be sorted in 60 seconds or less.

TeraByeSort: Elapsed time to sort 1 TB of data

Competition details: http://sortbenchmark.org/

(15)

Sorting Algorithms we will discuss:

SelectionSort

InsertionSort

MergeSort

QuickSort

(16)

Sorting by hand (humanSort)

Input:

8

4

3 1

2

5 6

9

0 7

Output:

(17)

Optimal Sorting

Claim: Any deterministic comparison-based sorting algorithm must perform O(n log n) comparisons to sort objects.n

0 1 2 0 2 1

1 0 2 1 2 0

2 0 1 2 1 0

(18)

SelectionSort

4 3 6 7 1 1. Find the -th smallest valuei

2. Place it at position via swapi

3. Repeat for 0 ≤ i ≤ n − 1

(19)

SelectionSort Efficiency

0 1 2 3

1 2 3

2 3

(n=4)

(20)

SelectionSort Efficiency (large n)

(21)

SelectionSort

def selectionSort(inList):

n = len(inList) for i in range(n):

mindex = i

for j in range(i+1, n):

if inList[j] < inList[mindex]:

mindex = j

inList[i], inList[mindex] = inList[mindex], inList[i]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

(22)

InsertionSort

4 3 6 7 1 1. Divide array into two parts

2. Insert the first unsorted item into the sorted position

3. Repeat until all items are sorted

(23)

InsertionSort “Insert”

1 2 4 5 7 3 8 1 2 4 5 3 7 8 1 2 4 3 5 7 8 1 2 3 4 5 7 8

(24)

InsertionSort

def insertionSort(inList):

n = len(inList)

for i in range(_____, _____):

val = inList[i]

j = i - 1

while __________ and _______________:

inList[j+1]=inList[j]

j -= 1

inList[j+1]=val 1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

(25)

Selection vs InsertionSort

def selectionSort(inList):

n = len(inList) for i in range(n):

mindex = i

for j in range(i+1, n):

if inList[j] < inList[mindex]:

mindex = j

inList[i], inList[mindex] = inList[mindex], inList[i]

1 2 3 4 5 6 7 8 9 10 11 12

def insertionSort(inList):

n = len(inList)

for i in range(1, n):

val = inList[i]

j = i - 1

while j >= 0 and val < inList[j]:

inList[j+1]=inList[j]

j -= 1

inList[j+1]=val 1

2 3 4 5 6 7 8 9 10 11 12 13

(26)

Sorting Algorithms we will discuss:

SelectionSort

InsertionSort

MergeSort

QuickSort

(27)

Divide and Conquer Algorithms

Recursively break a problem into sub-problems until the the problems become simple enough to solve directly

4 3 6 7 1

4 3 6 7 1

4 3 6 7 1

4 3

(28)

Recursion

The process by which a function calls itself directly or indirectly is called recursion.

(29)

Recursive For Loop

def recursiveFor(n):

if n == 0:

print(n) return

recursiveFor(n-1) print(n)

1 2 3 4 5 6 7 8 9 10 11 12 13

for i in range(10):

print(i) 1

2

(30)

Recursion

Base Case: What is the smallest sub-problem? What is the trivial solution?

Recursive Step: How can I systematically reduce my problem to an easier one?

Combining: How can I build my solution from recursive pieces?

(31)

def findMax(inList):

Recursive findMax

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

(32)

def fib(n):

Recursive Fibonacci

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

(33)

Recursive Array Sorting

0 3 7 5 8 9 2 1 4 6

Base Case:

Recursive Step:

Combining:

References

Related documents

Integrating concepts from information theory and the knowledge-based view of the firm, I introduce a generalized model and quantitative methods for estimating the inherent

This will &#34;hold polluters accountable, including those who disproportionately harm communities of colour and low-income communities; reduce greenhouse gas emissions; bolster

Her work appeared in the Journal of Business Research, Marketing Theory, Consumption, Markets &amp; Culture, Journal of Public Policy and Marketing, Journal

Both forms of fi nancing pay their investors from segregated asset pools; but whereas securitization effectively fi xes the segregated asset pool, thereby allocating risk to

Moreover, the current study revealed significantly lower morning basal cortisol, higher ACTH and lower cortisol response after ACTH stimulation as the autistic severity increased1.

SECTION 2a - GENERAL INFORMATION BREED SPECIALISTS: How long have you been interested in the Irish Setter as: A BREEDER: AN EXHIBITOR: Years Years Do

Hence this study was carried out to investigate the volatiles of this herb and to assess the whole plant as a heavy metal remover in artificial water.The unsaponifiable fraction

 The total quantity of PAHs detected in the dry season are higher than those of the rainy season in marshy soil samples collected in Warri city in this study period. 