Lab 05 PDF
Lab 05 PDF
Selection Sort
Bubble Sort
Insertion Sort
Mergesort
Quicksort
To sort a list of items the selection sort selects the largest item
and puts it in its place, then selects the next largest item and
puts it in its place, and so on.
Lecture 5.Sorting Algorithms
Selection Sort
To sort a list of items the selection sort selects the largest item
and puts it in its place, then selects the next largest item and
puts it in its place, and so on.
For example, to sort an array into ascending order, you first
search it for the largest item, and swap it with the the last item,
even if these items happen to be identical.
Lecture 5.Sorting Algorithms
Selection Sort
To sort a list of items the selection sort selects the largest item
and puts it in its place, then selects the next largest item and
puts it in its place, and so on.
For example, to sort an array into ascending order, you first
search it for the largest item, and swap it with the the last item,
even if these items happen to be identical.
Now, ignoring the last - and largest - item of the array, you
search the rest of the array for its largest item and swap it with
next-to-last item in the original array.
Lecture 5.Sorting Algorithms
Selection Sort
To sort a list of items the selection sort selects the largest item
and puts it in its place, then selects the next largest item and
puts it in its place, and so on.
For example, to sort an array into ascending order, you first
search it for the largest item, and swap it with the the last item,
even if these items happen to be identical.
Now, ignoring the last - and largest - item of the array, you
search the rest of the array for its largest item and swap it with
next-to-last item in the original array.
You continue until you have selected and swapped n 1 of the
n items in the array.
Lecture 5.Sorting Algorithms
Selection Sort
To sort a list of items the selection sort selects the largest item
and puts it in its place, then selects the next largest item and
puts it in its place, and so on.
For example, to sort an array into ascending order, you first
search it for the largest item, and swap it with the the last item,
even if these items happen to be identical.
Now, ignoring the last - and largest - item of the array, you
search the rest of the array for its largest item and swap it with
next-to-last item in the original array.
You continue until you have selected and swapped n 1 of the
n items in the array.
The remaining item, which is now in the first position in the
array, is in its proper order.
Lecture 5.Sorting Algorithms
Selection Sort
5 2 7 3 4 1 6
Lecture 5.Sorting Algorithms
Selection Sort
5 2 6 3 4 1 7
Lecture 5.Sorting Algorithms
Selection Sort
5 2 1 3 4 6 7
Lecture 5.Sorting Algorithms
Selection Sort
4 2 1 3 5 6 7
Lecture 5.Sorting Algorithms
Selection Sort
3 2 1 4 5 6 7
Lecture 5.Sorting Algorithms
Selection Sort
1 2 3 4 5 6 7
Lecture 5.Sorting Algorithms
Selection Sort
1 2 3 4 5 6 7
Lecture 5.Sorting Algorithms
Selection Sort
n (n 1) n2 5n
+ 3(n 1) = + 3
2 2 2
major operations. Thus, selection sort is O(n2 ).
Lecture 5.Sorting Algorithms
Selection Sort
n (n 1) n2 5n
+ 3(n 1) = + 3
2 2 2
major operations. Thus, selection sort is O(n2 ).
I A selection sort is appropriate only for small n because
O(n2 ) grows rapidly.
Lecture 5.Sorting Algorithms
Selection Sort
n (n 1) n2 5n
+ 3(n 1) = + 3
2 2 2
major operations. Thus, selection sort is O(n2 ).
I A selection sort is appropriate only for small n because
O(n2 ) grows rapidly.
I While the algorithm requires O(n2 ) comparisons, it
requires only O(n) data moves. Thus, a selection sort
could be a good choice over other methods when data
moves are costly but comparisons are not.
Lecture 5.Sorting Algorithms
Bubble Sort
5 2 7 3 6 1 4
Lecture 5.Sorting Algorithms
Bubble Sort
2 5 7 3 6 1 4
Lecture 5.Sorting Algorithms
Bubble Sort
2 5 7 3 6 1 4
Lecture 5.Sorting Algorithms
Bubble Sort
2 5 3 7 6 1 4
Lecture 5.Sorting Algorithms
Bubble Sort
2 5 3 6 7 1 4
Lecture 5.Sorting Algorithms
Bubble Sort
2 5 3 6 1 7 4
Lecture 5.Sorting Algorithms
Bubble Sort
2 5 3 6 1 4 7
Lecture 5.Sorting Algorithms
Bubble Sort
2 5 3 6 1 4 7
Although the array is not sorted after the first pass, the largest
item has "bubbled" to its proper position in the end of the array.
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
2 5 3 6 1 4 7
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
2 5 3 6 1 4 7
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
2 3 5 6 1 4 7
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
2 3 5 6 1 4 7
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
2 3 5 1 6 4 7
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
2 3 5 1 4 6 7
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
2 3 5 1 4 6 7
After the second pass, the second largest item in the array will
be in its proper place.
Lecture 5.Sorting Algorithms
Bubble Sort
During the second pass of the bubble sort, you return to the
beginning of the array and consider pairs of items in exactly the
same manner as the first pass.
You do not, however, include the last - and largest - item of the
array. That is, the second pass considers the first n 1 items of
the array.
2 3 5 1 4 6 7
After the second pass, the second largest item in the array will
be in its proper place.
Now, ignoring the last two items, which are in order, you
continue with subsequent passes until the array is sorted.
Lecture 5.Sorting Algorithms
Bubble Sort
n (n 1)
(n 1) + (n 2) + ... + 1 =
2
comparisons and the same number of exchanges.
Lecture 5.Sorting Algorithms
Bubble Sort
n (n 1)
(n 1) + (n 2) + ... + 1 =
2
comparisons and the same number of exchanges.
Therefore, the bubble sort algorithm is O(n2 ) in the worst case.
Lecture 5.Sorting Algorithms
Bubble Sort
n (n 1)
(n 1) + (n 2) + ... + 1 =
2
comparisons and the same number of exchanges.
Therefore, the bubble sort algorithm is O(n2 ) in the worst case.
The best case occures when the original data is already sorted:
bubbleSort uses one pass, during which no comparisons and
no exchanges occur.Thus, the bubble sort is O(n) in the best
case.
Lecture 5.Sorting Algorithms
Insertion Sort
5 2 7 3 6 1 4
Lecture 5.Sorting Algorithms
Insertion Sort
5 2 7 3 6 1 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 5 7 3 6 1 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 5 7 3 6 1 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 5 3 7 6 1 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 3 5 7 6 1 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 3 5 6 7 1 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 3 5 6 1 7 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 3 5 1 6 7 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 3 1 5 6 7 4
Lecture 5.Sorting Algorithms
Insertion Sort
2 1 3 5 6 7 4
Lecture 5.Sorting Algorithms
Insertion Sort
1 2 3 5 6 7 4
Lecture 5.Sorting Algorithms
Insertion Sort
1 2 3 5 6 4 7
Lecture 5.Sorting Algorithms
Insertion Sort
1 2 3 5 4 6 7
Lecture 5.Sorting Algorithms
Insertion Sort
1 2 3 4 5 6 7
Lecture 5.Sorting Algorithms
Insertion Sort
n (n 1)
1 + 2 + ... + (n 1) =
2
comparisons in the worst case. Which occurs if the initial data
is sorted in the descending order.
Lecture 5.Sorting Algorithms
Insertion Sort
n (n 1)
1 + 2 + ... + (n 1) =
2
comparisons in the worst case. Which occurs if the initial data
is sorted in the descending order.
1 4 8
2 3
Lecture 5.Sorting Algorithms
Mergesort
1 4 8
2 3
Lecture 5.Sorting Algorithms
Mergesort
1 4 8
2 3
1
Lecture 5.Sorting Algorithms
Mergesort
1 4 8
2 3
1 2
Lecture 5.Sorting Algorithms
Mergesort
1 4 8
2 3
1 2 3
Lecture 5.Sorting Algorithms
Mergesort
1 4 8
2 3
1 2 3 4 8
Lecture 5.Sorting Algorithms
Mergesort
The first task is to find a pivot. We will use the first element of
the collection.
Lecture 5.Sorting Algorithms
Quicksort
The first task is to find a pivot. We will use the first element of
the collection.
The hard work of Quicksort involves the second step - ie
placing the pivot in the correct position.
Lecture 5.Sorting Algorithms
Quicksort
The first task is to find a pivot. We will use the first element of
the collection.
The hard work of Quicksort involves the second step - ie
placing the pivot in the correct position.
One way to do this is through the use of two indicators. One
indicator is placed at the lower end of the collection, the other is
placed at the upper end.
Lecture 5.Sorting Algorithms
Quicksort
The first task is to find a pivot. We will use the first element of
the collection.
The hard work of Quicksort involves the second step - ie
placing the pivot in the correct position.
One way to do this is through the use of two indicators. One
indicator is placed at the lower end of the collection, the other is
placed at the upper end.
The left indicator moves up the collection until it either finds an
element that is greater than the pivot or it crosses the other
indicator.
Lecture 5.Sorting Algorithms
Quicksort
The first task is to find a pivot. We will use the first element of
the collection.
The hard work of Quicksort involves the second step - ie
placing the pivot in the correct position.
One way to do this is through the use of two indicators. One
indicator is placed at the lower end of the collection, the other is
placed at the upper end.
The left indicator moves up the collection until it either finds an
element that is greater than the pivot or it crosses the other
indicator.
If the former then the right indicator moves down the collection
until it finds an element less than or equal to the pivot. The two
elements thus indicated are swapped.
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lets look at how it works. Assume we wish to sort the following
collection (we will choose 12 to be the pivot, being the first
element of the collection)
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lets look at how it works. Assume we wish to sort the following
collection (we will choose 12 to be the pivot, being the first
element of the collection)
12 3 20 19 5 25 7
L R
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lets look at how it works. Assume we wish to sort the following
collection (we will choose 12 to be the pivot, being the first
element of the collection)
7 3 20 19 5 25 12
L R
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lets look at how it works. Assume we wish to sort the following
collection (we will choose 12 to be the pivot, being the first
element of the collection)
7 3 20 19 5 25 12
L R
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lets look at how it works. Assume we wish to sort the following
collection (we will choose 12 to be the pivot, being the first
element of the collection)
7 3 12 19 5 25 20
L R
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lets look at how it works. Assume we wish to sort the following
collection (we will choose 12 to be the pivot, being the first
element of the collection)
7 3 12 19 5 25 20
L R
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lets look at how it works. Assume we wish to sort the following
collection (we will choose 12 to be the pivot, being the first
element of the collection)
7 3 5 19 12 25 20
L R
Lecture 5.Sorting Algorithms
Quicksort
The process continues until the two indicator cross or when the
pivot is swapped with the element marked by the right indicator
moving down the list.
Lets look at how it works. Assume we wish to sort the following
collection (we will choose 12 to be the pivot, being the first
element of the collection)
7 3 5 12 19 25 20
L/R
Lecture 5.Sorting Algorithms
Quicksort