Using Python to Implement Algorithms
Tyler Moore
CSE 3353, SMU, Dallas, TX
Lecture 2
Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos
We already know Java and C++. Why learn Python?
Python has far less overhead than Java/C++ for the programmer. Python is closer to psuedo-code on the English-pseudocode-code spectrum than Java or C/C++, but it actually executes! Python is handy for data manipulation and transformation, and anything ”quick and dirty.” Python is very powerful, thanks to all those extension modules.
2 / 33
Python Resources
Download from python.org for all major platforms I’ve written an online tutorial: http://lyle.smu.edu/~tylerm/courses/cse3353/python.html Think Python by Allen Downey: http://www.greenteapress.com/thinkpython/ Videos from Khan academy: https://www.khanacademy.org/ science/computer-science-subject/computer-science
3 / 33
Selection Sort (C)
1
s e l e c t i o n s o r t ( int s [ ] , int n)
2
{
3
int i , j ;
4
int min ;
5
for ( i =0; i <n ; i++) {
6
min=i ;
7
for ( j=i +1; j<n ; j++)
8
i f ( s [ j ] < s [ min ] ) min=j ;
9
swap(&s [ i ] ,& s [ min ] ) ;
10
}
4 / 33