0% found this document useful (0 votes)
20 views11 pages

Ch6-Sorting (1 Slide Per Page)

Uploaded by

chenziyi1202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

Ch6-Sorting (1 Slide Per Page)

Uploaded by

chenziyi1202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

6.

Sorting: Iterative Algorithms

6.1 Selection Sort


6.2 Bubble Sort (covered in Tutorial 2)
6.3 Insertion Sort
6.1 Selection Sort

89 45 68 90 34 17
17 | 45 68 90 34 89
17 34 | 68 90 45 89
17 34 45 | 90 68 89
17 34 45 68 | 90 89
17 34 45 68 89 | 90

- Search the array for the smallest element and swap that element with the
first one.
- Search for the second smallest and swap that element with the second one.
- Repeat the described steps until the whole array is sorted.
6.1 Selection Sort
ALGORITHM selectionSort(A[0..n-1])
for i  0 to n-2 do

find the smallest element A[mini] among


A[i],…,A[n-1]

swap A[i] and A[mini]

- Search the array for the smallest element and swap that element with the
first one.
- Search for the second smallest and swap that element with the second one.
- Repeat the described steps until the whole array is sorted.
6.1 Selection Sort
ALGORITHM selectionSort(A[0..n-1])
for i  0 to n-2 do main operation
mini  i (find the smallest
for j  i+1 to n-1 do element A[mini]
if A[j] < A[mini] mini  j among
A[i],…,A[n-1])
tmp  A[i]
(swap A[i] and
A[i]  A[mini] A[mini])
A[mini]  tmp

- Search the array for the smallest element and swap that element with the
first one.
- Search for the second smallest and swap that element with the second one.
- Repeat the described steps until the whole array is sorted.
i j A[0] A[1] A[2] A[3] A[4] A[5] mini
0 89 45 68 90 34 17 0
0 1 89 45 68 90 34 17 1
0 2 89 45 68 90 34 17 1 find the smallest element i=0:
0 3 89 45 68 90 34 17 1 among A[0], A[1], …, A[5] n-1 comparisons
0 4 89 45 68 90 34 17 4
0 5 89 45 68 90 34 17 5
0 5 17 45 68 90 34 89 5
1 17 45 68 90 34 89 1
1 2 17 45 68 90 34 89 1 find the smallest element
1 3 17 45 68 90 34 89 4 i=1:
among A[1],…, A[5] n-2 comparisons
1 4 17 45 68 90 34 89 4
1 5 17 45 68 90 34 89 4
1 5 17 34 68 90 45 89 4
2 17 34 68 90 45 89 2
2 3 17 34 68 90 45 89 2 find the smallest i=2:
2 4 17 34 68 90 45 89 4 among A[2],…, A[5] n-3 comparisons
2 5 17 34 68 90 45 89 4
2 5 17 34 45 90 68 89 4
3 17 34 45 90 68 89 3
3 4 17 34 45 90 68 89 4  
3 5 17 34 45 90 68 89 4
3 5 17 34 45 68 90 89 4
4 17 34 45 68 90 89 4 find the smallest i=n-2:
4 5 17 34 45 68 90 89 5 among A[4], A[5] 1 comparison
4 5 17 34 45 68 89 90 5
Total: (n−1) + (n−2) + … + 2 + 1 = ½n(n-1) comparisons. The running time is O(n2).
6.1 Selection Sort: Proof of Correctness
ALGORITHM selectionSort(A[0..n-1])
for i  0 to n-2 do
mini  i
for j  i+1 to n-1 do
if A[j] < A[mini] mini  j
swap A[i] and A[mini]

▪ The array at any moment is divided into two parts: sorted and unsorted, and
any element of the sorted sub-array is less than or equal to any element of
the unsorted one.

▪ In each pass of the algorithm, the minimum element of the unsorted sub-
array is found and swapped with the first element of the unsorted sub-array.
The sorted sub-array is thus extended by one element in every pass.
6.1 Selection Sort: Proof of Correctness
ALGORITHM selectionSort(A[0..n-1])
for i  0 to n-2 do
mini  i
for j  i+1 to n-1 do
if A[j] < A[mini] mini  j
swap A[i] and A[mini]
Loop invariant

At the beginning of iteration i, the sub-array A[0..i-1] is sorted and


any element in A[0..i-1] is less than or equal to any element in
A[i..n-1].

1) The invariant is true initially: the sub-array A[0..-1] is empty.


2) If the invariant is true for i=k, it is true for i=k+1.
3) When the loop terminates, the invariant is true for the entire array.
4) The loop does terminate.
6.2 Bubble Sort
ALGORITHM bubbleSort(A[0..n-1])
for i  0 to n-2 do main operation
for j  0 to n-2-i do
if A[j]>A[j+1]
tmp  A[j]
A[j]  A[j+1]
A[j+1]  tmp

Tracing and analysis of Bubble sort was done in Tutorial 2.


O(n2)
6.3 Insertion Sort

i A[0] A[1] A[2] A[3] A[4] A[5]

1 23 78 45 8 32 56
2 23 78 45 8 32 56
3 23 45 78 8 32 56
4 8 23 45 78 32 56
5 8 23 32 45 78 56
6 8 23 32 45 56 78

The array at any moment is divided into two parts: sorted and unsorted.
In each pass of the algorithm, the first element of the unsorted sub-list
is inserted into the sorted sub-list at the appropriate place.
6.3 Insertion Sort

ALGORITHM insertionSort(A[0..n-1])
//The algorithm sorts a given array A
//Input: an array A[0..n-1] of n numbers
//Output: array A[0..n-1] in ascending order
for i  0 to n-1 do main operation
tmp  A[i]
j i-1
while (j≥0 and tmp < A[j]) do
A[j+1]  A[j]
j  j-1
A[j+1]  tmp

Analysis: CW1
Testing the Sorting Algorithms

n Selection Sort Insertion Sort Bubble Sort Merge-Sort Quick Sort

10,000 5.17 sec. 3.18 sec. 10.88 s. 0.05 sec. 0.05 sec.

20,000 22.03 sec 13.51 sec. 45.09 s. 0.11 sec 0.05 sec.

40,000 1 min. 31.01 sec. 53.66 sec 2 min. 59.40 s. 0.22 sec. 0.11 sec.

80,000 6 min. 06.30 sec. 3 min. 46.62 sec. 12 min. 27.15 s. 0.49 sec. 0.28 sec.

iterative recursive

from A. Drozdek “Data Structures and Algorithms in C++”


(Pentium 133 MHz PC)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy