Sei sulla pagina 1di 3

CS

234 Lecture 13: Sorting


Sorting

Idea:
Make linear search more efficient
Operations:
Comparison of key
Move entries with a list
Goal:
Total ordering of item (sorted order)
Example:

[8,7,6,4,5,3,2,1] [1,2,3,4,5,6,7,8]

Man algorithms for sorting


Computation complexity: O(n ) O(nlogn) optimal


Memory requirements: O(n) O(1)
Stability maintains ordering of items that are equal
o Ex [1,2,5(1st),5(2nd),7,5(3rd)] [1,2,5(1st),5(2nd),5(3rd),7]
Heap sort

1. Build a heap out of the data (list) O(nlogn)


2. Repeatedly remove the largest item from the head and insert into the output
list O(nlogn)
Example:
[6,5,3,1,8,7,2,4] Build Heap
8
Heap : [8,6,7,4,5,3,2,1] (breadth first from diagram)
[1,6,7,4,5,3,2,8]
[7,6,3,4,5,1,2,8]
[2,6,3,4,5,1,7,8]
6
[6,5,3,4,2,1,7,8]
[5,4,3,1,2,6,7,8]



[1,2,3,4,5,6,7,8] Sorted order, heap is gone




4
5
1

7
2

Selection sort

1. Repeatedly pick the largest item in the list and place it in the output

Example: [4,9,3,7,6]


Input
Output
[4,9,3,7,6]
[]
[4,3,7,6]
[9]
[4,3,6]
[7,9]
[4,3]
[6,7,9]
[3]
[4,6,7,9]
[]
[3,6,7,9]

Python Code for selection sort:







n - 1







Time complexity: O(n)

Insertion Sort

1. For all items in the list, insert the ith item into the i-1 sorted ones
Example: [4,9,3,7,6]

i
List
0
[4,9,3,7,6] 4 is the first element, it is not less than anything so far
1
[4,9,3,7,6] 9 is compared to [4]. It is not less than 4 so it is the second item
2
[3,4,9,7,6] 3 is compared to [4,9]. It is less than 9 and 4 so it is inserted in
3
[3,4,7,9,6] 7 is compared to [3,4,9]. It is less than 9 but greater than 7
4
[3,4,6,7,9] 6 is compared to [3,4,7,9]. It is less than 7 but greater than 4

n n - 1




Time Complexity:

Worst Case - O(n )
Best Case O(n) Already sorted

Bubble Sort

1. Iterate repeatedly through the list and swap the adjacent item, if they are not
in order
Example: [4,9,3,7,6]

Iteration List
1
[4,9,3,7,6] No swap. 9>4
1
[4,9,3,7,6] Swap 3 and 9. 3<9
1
[4,3,9,7,6] Swap 7 and 9. 7<9
1
[4,3,7,9,6] Swap 6 and 9. 6<9
2
[4,3,7,6,9] Swap 3 and 4. 3<4
2
[3,4,7,6,9] No Swap. 7>4
2
[3,4,7,6,9] Swap 6 and 7. 6<7

2
[3,4,6,7,9] No swap. 9>7
2
[3,4,6,7,9] List is sorted

Time Complexity:
Every pass: # of comparisons = n -1
How many passes? Up to n times. Therefore time complexity = O(n )
Larger item towards the beginning are called rabbits
Smaller items at the end of the list are called turtles

Potrebbero piacerti anche