Upload
rachel-andrews
View
220
Download
0
Tags:
Embed Size (px)
Citation preview
Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”?
Two algorithms considered here Bubble sort Selection sort
9-2
1. Compare 1st two elements and exchange them if they are out of order.
2. Move down one element and compare 2nd and 3rd elements. Exchange if necessary. Continue until end of array.
3. Pass through array again, repeating process and exchanging as necessary.
4. Repeat until a pass is made with no exchanges.
9-3
9-4
0 1 2 3 4
5 3 1 4 2
3 5 1 5 4 5 2 5 3 1 4 2 5 1 3 3 4 2 4 1 3 2 4 5
Pass 1
Pass 2
9-5
1 3 2 4 5
1 3 2 3 1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Pass 2
Pass 3
Pass 4
0 1 2 3 4
5 3 1 4 2
void bubbleSort(int a[], int count)
pass count - 1
last pass - 1
Loop (for i from 1 to pass)
Loop (for j from 0 to last)
If (a[j] > a[j + 1]) Then
swap a[j] and a[j = 1]
End If
End Loop
last last - 1
End Loop
Swap Algorithm
temp a[j]
a[j] a[i + 1]
a[j + 1] temp
Benefit Easy to understand and implement
Disadvantage Inefficiency makes it slow for large arrays
9-7
1. Locate smallest element in array and exchange it with element in position 0.
2. Locate next smallest element in array and exchange it with element in position 1.
3. Continue until all elements are in order.
9-8
The picture shows an array of six integers that we want to sort from smallest to largest
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Start by finding the smallest entry.
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Start by finding the smallest entry.
Swap the smallest entry with the first entry.
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Start by finding the smallest entry.
Swap the smallest entry with the first entry.
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Part of the array is now sorted.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Find the smallest element in the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Find the smallest element in the unsorted side.
Swap with the front of the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
We have increased the size of the sorted side by one element.
Sorted side Unsorted side
[0] [1] [2 ] [3] [4] [5]
The process continues...
Sorted side Unsorted side
Smallest
from
unsorted
Smallest
from
unsorted
[0] [1] [2] [3] [4] [5]
The process continues...
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Swap
with
front
Swap
with
front
The process continues...
Sorted side Unsorted sideSorted side
is bigger
Sorted side
is bigger
[0] [1] [2] [3] [4] [5]
The process keeps adding one more number to the sorted side.
The sorted side has the smallest numbers, arranged from small to large.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
We can stop when the unsorted side has just one number, since that number must be the largest number.
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
The array is now sorted.
We repeatedly selected the smallest element, and moved this element to the front of the unsorted side.
[0] [1] [2] [3] [4] [5]
9-23
void selectionSort(int array[], int size){ int startScan, minIndex, minValue; for (startScan = 0; startScan < (size - 1); startScan++){ minIndex = startScan; minValue = array[startScan]; for (int index = startScan + 1; index < size; index++){ if (array[index] < minValue){ minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; }}
void insertionSort(elemType a[], int count)
Loop (for next = 1 to count – 1)
insert a[next]
move next
Loop (while move > 0 && a[move – 1] > insert)
// Shift to right
a[move] a[move – 1]
move move - 1
End Loop
// Insert item in correct position
a[move] insert
End Loop
Benefit › More efficient than Bubble Sort, due to
fewer exchanges
Disadvantage › Considered harder than Bubble Sort to
understand
9-25