Sorting: Thinking about Algorithms - Purdue University

13
Sorting: Thinking about Algorithms HORT 590 Lab 12 Instructor: Kranthi Varala

Transcript of Sorting: Thinking about Algorithms - Purdue University

Page 1: Sorting: Thinking about Algorithms - Purdue University

Sorting: Thinking about Algorithms

HORT 590Lab 12

Instructor: Kranthi Varala

Page 2: Sorting: Thinking about Algorithms - Purdue University

Sorting a list of values

• Sorting is the process of ordering items in an increasing (or decreasing) order based on their value.• Lists in Python can be sorted in two ways:• list.sort() function• sorted() function

• sorted is a general function that will accept any iterable item, such as dictionaries, tuples etc.• Both versions use a sorting algorithm called

‘Timsort’ which is a hybrid of merge and insert sort methods.

Page 3: Sorting: Thinking about Algorithms - Purdue University

Sorting a list of values

• Remember to capture the output of sorted in a new object

Page 4: Sorting: Thinking about Algorithms - Purdue University

Sorting tuples

• Sorting tuples by default will sort them by the first element in the tuple. In this example, that is the name of the employee.• The sorted() function can be used to sort based on a

different element by using the ‘key’ argument.• In this example, the lambda construct is used to

generate an inline functions that simply returns the element at index 2.

Page 5: Sorting: Thinking about Algorithms - Purdue University

Sorting Dictionaries

• Sort by keys :

• Sort by values :

• Sort keys by values :

Page 6: Sorting: Thinking about Algorithms - Purdue University

Sorting as an algorithm exercise

• Sorting is a classic example for discussing algorithm complexity.• Visualize the different sorting algorithms here:

https://visualgo.net/en/sorting

Page 7: Sorting: Thinking about Algorithms - Purdue University

Making system calls from python

• First, let’s load a newer version of python:• module load intel/14.0.2.144• module load python/2.7.6

• Now, we can use python to make calls to the system i.e., calls commands and scripts available on the system command line.• The ‘os’ and ‘subprocess’ module are the two

main ways to interact with the system command line.

Page 8: Sorting: Thinking about Algorithms - Purdue University

Making system calls from python

• os.system will send the argument to the command line and execute it.• The output is shown in the current stdout but not

returned to the caller program.• Use of os.system is ‘deprecated’

Page 9: Sorting: Thinking about Algorithms - Purdue University

Making system calls from python

• subprocess.call is the current preferred way for system calls.• The argument shell=True sends the command

without trying to first interpret it.

Page 10: Sorting: Thinking about Algorithms - Purdue University

Capturing output from system calls

• The os.environ command allows you to capture environment variables set in the shell.• Popen along with communicate() functions lets

you make a system call and capture the outputin your program

Page 11: Sorting: Thinking about Algorithms - Purdue University

Splitting a grep search• Let’s create a python script to make a grep search

faster.• Original command :

grep –f Sites.txt SRR444602.fastq• This command will search for all patterns listed in

Sites.txt in the file SRR444602.fastq.• Each of the patterns in Sites.txt is evaluated in

each line of the file SRR444602.fastq• This command took 27 seconds. • Let’s simplify the search by breaking down the

input patterns into smaller groups.

Page 12: Sorting: Thinking about Algorithms - Purdue University

Splitting a grep search• Let’s create a python script to make a grep search

faster.• Original command :

grep –f Sites.txt SRR444602.fastq• This command will search for all patterns listed in

Sites.txt in the file SRR444602.fastq.• Each of the patterns in Sites.txt is evaluated in

each line of the file SRR444602.fastq• This command took 27 seconds. • Let’s simplify the search by breaking down the

input patterns into smaller groups.

Page 13: Sorting: Thinking about Algorithms - Purdue University

Splitting a grep search

• Finish the script splitGrep.py in /scratch/scholar/k/kvarala/Week13• Instructions for the next steps are in the file