Upload
its4krishna3776
View
242
Download
0
Embed Size (px)
Citation preview
8/10/2019 07 Sorting Algorithms
1/15
Introduction to Python Programming
(7) Sorting Algorithms
S. Thater and A. Friedrich
Saarland University
Winter Semester 2011/2012
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 1 / 15
http://find/8/10/2019 07 Sorting Algorithms
2/15
Sorting Algorithms
Agenda:
Insertion Sort
Selection Sort
Bubblesort
MergesortQuicksort
Goals:
Understand the above sorting algorithms.
Get an idea that there are differences in efficiency.Get used to recursion.
We wont talk about complexity theory in this lecture.
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 2 / 15
http://find/8/10/2019 07 Sorting Algorithms
3/15
Swapping two items of a list
How can we swap two items of a list?
1 a = [5, 3]
2
3
Today, we always assume that our list is called a.
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 3 / 15
http://find/8/10/2019 07 Sorting Algorithms
4/15
Selection Sort
Selection Sort
Until list is empty:
Find the smallest element of the remaining list.
Append it to a new list.
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 4 / 15
http://find/8/10/2019 07 Sorting Algorithms
5/15
Selection Sort: Code
1 def selection_sort(a):
2 """ sorting algorithm, creates a NEW list """
3 b = []
4 # Why is the list a copied here?
5 a = a[:]
6 while len(a) > 0:
7 # Find minimum of the list8 minimum = 0
9 for i in range(1, len(a)):
10 if a[i] < a[minimum]:
11 minimum = i
12 # Remove the minimum from the list13 # and append it to the new list
14 b.append(a.pop(minimum))
15 return b
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 5 / 15
http://find/8/10/2019 07 Sorting Algorithms
6/15
Selection Sort - in place
Selection Sort
For each ifrom 0 to N2:
Find the smallest element of the remaining list.
If this element is smaller than a[i], exchange it with a[i].
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 6 / 15
http://find/8/10/2019 07 Sorting Algorithms
7/15
Selection Sort (in place): Code
1 def selection_sort_in_place(a):2 """ sorting algorithm, works in-place"""
3 # point to element at position i
4 for i in range(0, len(a)):
5 # Find the smallest element in the
6 # rest of the list
7 minimum = i
8 for j in range(i+1, len(a)):
9 if a[j] < a[minimum]:
10 minimum = j
11 # exchange elements
12 temp = a[i]13 a[i] = a[minimum]
14 a[minimum] = temp
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 7 / 15
http://find/8/10/2019 07 Sorting Algorithms
8/15
Insertion Sort
Insertion SortLook at one element after the other and insert it into the list at
the correct position.
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 8 / 15
http://find/8/10/2019 07 Sorting Algorithms
9/15
Insertion Sort - in-place
Insertion Sort
Look at one element after the other and insert it into the part of
the list already dealt with at the correct position.
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 9 / 15
http://find/8/10/2019 07 Sorting Algorithms
10/15
Insertion Sort: Code
1 def insertion_sort_in_place(a):2 """ sorting algorithm, works in-place"""
3 # go through list
4 for i in range(1, len(a)):
5 # Remember value of a[i]
6 v = a[i]
7 j = i
8 # Go backwards in list and shift to
9 # the right if element > value to be
10 # inserted
11 while a[j-1] > v and j>=1:
12 a[j] = a[j-1]
13 j -= 1
14 # insert the value at its correct position
15 a[j] = v
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 10 / 15
http://find/8/10/2019 07 Sorting Algorithms
11/15
Bubble Sort
Bubble SortGo through the list again and again
and exchange neighboring
elements (if necessary).
Stop if no changes have been
made while going through the listonce.
Implementation: Your task. Try notto just copy it from the web. :)
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 11 / 15
http://find/8/10/2019 07 Sorting Algorithms
12/15
Recursion: Base Case
1 def factorial(x):2 # base case
3 if (x
8/10/2019 07 Sorting Algorithms
13/15
Recursion: Base Case
Base cases for sorting algorithms:
empty lists are sorted.
lists with only one element are sorted.
All other lists may still need sorting.
1 if len(a)
8/10/2019 07 Sorting Algorithms
14/15
Mergesort: Divide & Conquer
Dividelists & sort them
recursively.
Mergesorted lists.
on board
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 14 / 15
http://find/http://goback/8/10/2019 07 Sorting Algorithms
15/15
Quicksort: Divide & Conquer
Dividelists using a pivotelement from the list:
(a) items < pivot
(b) items > pivot
Sort (a) and (b)
recursively.
Mergesorted lists:
(a) +[pivot]+ (b)
S. Thater and A. Friedrich ( Saarland University) Introduction to Python Programming Winter Semester 2011/2012 15 / 15
http://find/