Day 4 Sorting - Department of Computer Science

advertisement
Day 4
Sorting
Yesterday

We looked at sorting

Saw that algorithms can be improved

Today we look at another classical
problem in computer science
Sorting!
Sorting another classical problem in
computer science
 Many, many different algorithms
developed
 http://en.wikipedia.org/wiki/Sorting_algor
ithm Lists a lot of examples.
 Today we’re going to look at “Bubble
sorting”

Why Sort?

Already seen that a sorted list can be
searched must faster than an unsorted
list

Sort once. Improves search always.
 Adding an element to a sorted list is possible
○ Insertion short
Bubble sort
Pretty strange name
 So called because the smaller values
“bubble” up to the top of this list.


Slow – only practically used for very
small sets of data
Lets take a look at the algorithm
Sorted = false;
 While sorted = false;

 Sorted = true;
 For all elements n in a list
 If n > n +1
○ Swap
○ Sorted = false;
 N = n +1;
Demonstration

https://cs.senecac.on.ca/~catherine.leun
g/sketches/bubble.html
Why is it slow

Worst case scenario –
 The inverse sorted list
 The algorithm runs n * n times
We say it has a worst case complexity of
𝑛2
 Time taken to sort increases
exponentially

Practicality

How do we do the swap?
 Array[i] = Array[i+1];
 Array[i+1] = Array[i];
That wont work, we’re overwriting
Array[i+1] before assigning its value to
Array[i].
 So what do we do.

Practicality

Simples
 We can use a local variable
 int tmp;
 tmp = Array[i];
 Array[i] = Array[i+1];
 Array[i+1] = tmp;

Now value of Array[i] is stored before
being overwritten.
How can we do better?
Example
 Merge sort
 Example of a recursive algorithm

 An algorithm that calls itself

Let me explain
Algorithm
mergeSort( input:Array)
 If (Array.length > 1)

 arrayFirst = Array[0..n/2]
 arraySecond = Array[n/2+1 ..n]
 mergeSort(arrayFirst);
 mergeSort(arraySecond);

Combine arrayFirst with arraySecond
 With elements in order
Algorithm
mergeSort( input:Array)
 If (Array.length > 1)

 arrayFirst = Array[0..n/2]
 arraySecond = Array[n/2+1 ..n]
 mergeSort(arrayFirst);
 mergeSort(arraySecond);

Recursive Calls
Combine arrayFirst with arraySecond
 With elements in order
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
8
1
6
Now merge each list in the right order
5
9
3
7
2
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
8
1
6
Now merge each list in the right order
5
3
9
3
7
2
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
8
1
6
Now merge each list in the right order
5
9
3
5
3
7
2
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
8
1
6
Now merge each list in the right order
5
9
3
3
5
7
7
2
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
8
1
6
Now merge each list in the right order
5
9
3
7
3
5
7
9
2
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
8
1
6
Now merge each list in the right order
5
9
3
7
2
3
5
7
9
1
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
1
6
Now merge each list in the right order
5
9
3
7
2
8
3
5
7
9
1
2
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
6
Now merge each list in the right order
5
9
3
7
2
8
1
3
5
7
9
1
2
6
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge each list in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge each list in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
1
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge each list in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
1
2
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge each list in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
1
2
3
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge each list in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
1
2
3
5
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge each list in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
1
2
3
5
6
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge adjacent lists in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
1
2
3
5
6
7
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge each list in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
1
2
3
5
6
7
8
Demonstration
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
5
9
3
7
8
2
6
1
Now merge each list in the right order
5
9
3
7
2
8
1
6
3
5
7
9
1
2
6
8
1
2
3
5
6
7
8
9
Sorted!
Faster than Bubble Sort?
Yes
 Sorting each recurrence takes 2*n/2+n
time of the level above.
 Worst case O(n log n)
 Requires more memory than bubble sort

Download