Sorting: Thinking about Algorithms - Purdue University
Transcript of Sorting: Thinking about Algorithms - Purdue University
![Page 1: Sorting: Thinking about Algorithms - Purdue University](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/1.jpg)
Sorting: Thinking about Algorithms
HORT 590Lab 12
Instructor: Kranthi Varala
![Page 2: Sorting: Thinking about Algorithms - Purdue University](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/3.jpg)
Sorting a list of values
• Remember to capture the output of sorted in a new object
![Page 4: Sorting: Thinking about Algorithms - Purdue University](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/5.jpg)
Sorting Dictionaries
• Sort by keys :
• Sort by values :
• Sort keys by values :
![Page 6: Sorting: Thinking about Algorithms - Purdue University](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022081503/62a271a35043cc3e485f36d4/html5/thumbnails/13.jpg)
Splitting a grep search
• Finish the script splitGrep.py in /scratch/scholar/k/kvarala/Week13• Instructions for the next steps are in the file