You are on page 1of 41

Sorting

ERIN KEITH

14_SORTING 1
Topics
1. Bubble
2. Selection
3. Insertion
4. Merge
5. Quick
6. Radix

14_SORTING 2
Sorting
Sorting allows us to put data into an order (ascending or
descending).
Internal Sort
◦ Collection of data fits in memory
◦ “in-place” sorting
External Sort
◦ Collection of data does not all fit in memory
◦ Must rely on secondary storage

14_SORTING 3
Sorting
For example, we start out with an array containing unordered
values:
10 3 7 2 9 8 1 4 6 5

When we’re finished sorting, we want the values to be in


ascending order:
1 2 3 4 5 6 7 8 9 10

14_SORTING 4
Bubble Sort
Bubble sort is sometimes considered the simplest of the sorting
algorithms.
We continuously iterate over a collection of data (an array) and
swap elements which don’t follow our ordering rules.

14_SORTING 5
Bubble Sort
We’re going compare neighboring elements in the array and
swap them if the first (left hand) element isn’t less than the
second (right hand) element.

10 3 7 2 9 8 1 4 6 5

Is 10 less than 3?
◦ Then swap!
3 10 7 2 9 8 1 4 6 5

14_SORTING 6
Bubble Sort
3 10 7 2 9 8 1 4 6 5
Is 10 less than 7?
◦ Then swap And continue!
3 7 10 2 9 8 1 4 6 5
3 7 2 10 9 8 1 4 6 5
3 7 2 9 10 8 1 4 6 5
3 7 2 9 8 10 1 4 6 5
3 7 2 9 8 1 10 4 6 5
3 7 2 9 8 1 4 10 6 5
3 7 2 9 8 1 4 6 10 5
3 7 2 9 8 1 4 6 5 10

14_SORTING 7
Bubble Sort
3 7 2 9 8 1 4 6 5 10
Is 3 less than 7?
◦ No swap! And continue!
3 7 2 9 8 1 4 6 5 10
3 2 7 9 8 1 4 6 5 10
3 2 7 9 8 1 4 6 5 10
3 2 7 8 9 1 4 6 5 10
3 2 7 8 1 9 4 6 5 10
3 2 7 8 1 4 9 6 5 10
3 2 7 8 1 4 6 9 5 10
3 2 7 8 1 4 6 5 9 10

14_SORTING 8
Bubble Sort
3 2 7 8 1 4 6 5 9 10
Is 3 less than 1?
◦ Then swap! And continue!
2 3 7 8 1 4 6 5 9 10
2 3 7 8 1 4 6 5 9 10
2 3 7 8 1 4 6 5 9 10
2 3 7 1 8 4 6 5 9 10
2 3 7 1 4 8 6 5 9 10
2 3 7 1 4 6 8 5 9 10
2 3 7 1 4 6 5 8 9 10

14_SORTING 9
Bubble Sort
For I = 0 to N - 2
For J = 0 to N - 2
If (A(J) > A(J + 1)
Temp = A(J)
A(J) = A(J + 1)
A(J + 1) = Temp
End-If
End-For
End-For

14_SORTING 10
Selection Sort
Selection sort is also a simple sorting algorithms.
We divide our data into a “sorted” section and an “unsorted”
section
We select the smallest element in the “unsorted” section and
swap it with the leftmost element in the “unsorted” section
It then becomes the rightmost element in the “sorted” section

14_SORTING 11
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
10 3 7 2 9 8 1 4 6 5
0 6

The swapped min value becomes the edge of the “sorted


section
1 3 7 2 9 8 10 4 6 5
0 1

14_SORTING 12
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
1 3 7 2 9 8 10 4 6 5
1 3

The swapped min value becomes the edge of the “sorted


section
1 2 7 3 9 8 10 4 6 5
1 2

14_SORTING 13
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
1 2 7 3 9 8 10 4 6 5
2 3

The swapped min value becomes the edge of the “sorted


section
1 2 3 7 9 8 10 4 6 5
2 3

14_SORTING 14
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
1 2 3 7 9 8 10 4 6 5
3 7

The swapped min value becomes the edge of the “sorted


section
1 2 3 4 9 8 10 7 6 5
3 4

14_SORTING 15
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
1 2 3 4 9 8 10 7 6 5
4 9

The swapped min value becomes the edge of the “sorted


section
1 2 3 4 5 8 10 7 6 9
4 5

14_SORTING 16
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
1 2 3 4 5 8 10 7 6 9
5 8

The swapped min value becomes the edge of the “sorted


section
1 2 3 4 5 6 10 7 8 9
5 6

14_SORTING 17
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
1 2 3 4 5 6 10 7 8 9
6 7

The swapped min value becomes the edge of the “sorted


section
1 2 3 4 5 6 7 10 8 9
6 7

14_SORTING 18
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
1 2 3 4 5 6 7 10 8 9
7 8

The swapped min value becomes the edge of the “sorted


section
1 2 3 4 5 6 7 8 10 9
7 8

14_SORTING 19
Selection Sort
We’re going to iterate over the “unsorted” section to find
the position of the smallest value
Then swap that with the first position in the “unsorted”
section
1 2 3 4 5 6 7 8 10 9
8 9

The swapped min value becomes the edge of the “sorted


section
1 2 3 4 5 6 7 8 9 10

14_SORTING 20
Selection Sort
For I = 0 to N-1 do:
Smallsub = I
For J = I + 1 to N-1 do:
If A(J) < A(Smallsub)
Smallsub = J
End-If
End-For
Temp = A(I)
A(I) = A(Smallsub)
A(Smallsub) = Temp
End-For 14_SORTING 21
Insertion Sort
Insertion sort is also a simple sorting algorithms.
Again, we divide our data into a “sorted” section and an
“unsorted” section
We select the first element in the “unsorted” section and insert
in its correct position on the left-hand side
This requires shuffling until we find the correct position

14_SORTING 22
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

10 3 7 2 9 8 1 4 6 5

3 10 7 2 9 8 1 4 6 5

14_SORTING 23
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

3 10 7 2 9 8 1 4 6 5

3 7 10 2 9 8 1 4 6 5

14_SORTING 24
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

3 7 10 2 9 8 1 4 6 5

3 7 2 10 9 8 1 4 6 5

3 2 7 10 9 8 1 4 6 5

2 3 7 10 9 8 1 4 6 5

14_SORTING 25
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

2 3 7 10 9 8 1 4 6 5

2 3 7 9 10 8 1 4 6 5

14_SORTING 26
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

2 3 7 9 10 8 1 4 6 5

3 7 2 9 8 10 1 4 6 5

2 3 7 8 9 10 1 4 6 5

14_SORTING 27
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

2 3 7 8 9 10 1 4 6 5

1 2 3 7 8 9 10 4 6 5

14_SORTING 28
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

1 2 3 7 8 9 10 4 6 5

1 2 3 4 7 8 9 10 6 5

14_SORTING 29
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

1 2 3 4 7 8 9 10 6 5

1 2 3 4 6 7 8 9 10 5

14_SORTING 30
Insertion Sort
We’re going to use the first value in the “unsorted” section
Then iterate backwards over the “sorted” section to find the
correct spot to insert the item

1 2 3 4 6 7 8 9 10 5

1 2 3 4 5 6 7 8 9 10

14_SORTING 31
Insertion Sort
For I = 1 to N-1
J=I
Do while (J > 0) and (A(J) < A(J - 1)
Temp = A(J)
A(J) = A(J - 1)
A(J - 1) = Temp
J=J-1
End-Do
End-For

14_SORTING 32
Moar Sorting
Sorting allows us to put data into an order (ascending or
descending).
Some faster sorting algorithms
A divide-and-conquer algorithm works by recursively breaking
down a problem into two or more sub-problems of the same or
related type, until these become simple enough to be solved
directly

14_SORTING 33
Merge Sort
Merge sort is a “divide and conquer” algorithm
We continuously divide the unsorted list into n sublists, each
containing one element (a list of one is considered sorted)
Repeatedly merge sublists to produce new sorted sublists until
there is only one sublist remaining. This will be the sorted list.

14_SORTING 34
Merge Sort

14_SORTING 35
Merge Sort
mergeSort(arr[], left, right, final[]) merge(a[], begin, mid, end, b[])
If right > left i = begin, j = mid
mid = (left + right)/2 For k = begin to end
Call mergeSort(arr, left, mid) If a[i] <= a[j]
Call mergeSort(arr, mid+1, right) b[k] = a[i]
Call merge(arr, left, mid, right, final) i=i+1
else
b[k] = a[j]
j=j+1

14_SORTING 36
Quick Sort
Quick sort is also a “divide and conquer” algorithm
We choose a pivot and move elements into the sub-arrays
according to whether they are less than or greater than the pivot

14_SORTING 37
Quick Sort
quickSort(arr[], low, high) partition(arr[], low, high)
If low < high pivot = arr[hi]
pi = partition(arr, low, high) i = low
Call quickSort(arr, low, pi - 1) For j = low to high
Call quickSort(arr, pi + 1, high) If arr[j] < pivot
swap arr[i] with arr[j]
i=i+1
swap arr[i] with arr[hi]
return i

14_SORTING 38
Radix Sort
Non-comparative sort! Aka bucket sort
Assume each “string” has the same length
Place into buckets according to values BUT start with the
LEAST significant digit
As each position gets bucketed, keep the order from the
previous bucketing

14_SORTING 39
Radix Sort
https://www.youtube.com/watch?v=ibtN8rY7V5k

14_SORTING 40
Next Class
No Instruction
:’(

14_SORTING 41

You might also like