sort: arrange values into an order alphabetical ascending numeric descending numeric does...
TRANSCRIPT
![Page 1: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/1.jpg)
![Page 2: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/2.jpg)
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
![Page 3: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/3.jpg)
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
![Page 4: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/4.jpg)
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
![Page 5: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/5.jpg)
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
![Page 6: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/6.jpg)
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
![Page 7: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/7.jpg)
Benefit Easy to understand and implement
Disadvantage Inefficiency makes it slow for large arrays
9-7
![Page 8: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/8.jpg)
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
![Page 9: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/9.jpg)
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]
![Page 10: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/10.jpg)
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]
![Page 11: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/11.jpg)
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]
![Page 12: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/12.jpg)
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]
![Page 13: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/13.jpg)
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]
![Page 14: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/14.jpg)
Find the smallest element in the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 15: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/15.jpg)
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]
![Page 16: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/16.jpg)
We have increased the size of the sorted side by one element.
Sorted side Unsorted side
[0] [1] [2 ] [3] [4] [5]
![Page 17: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/17.jpg)
The process continues...
Sorted side Unsorted side
Smallest
from
unsorted
Smallest
from
unsorted
[0] [1] [2] [3] [4] [5]
![Page 18: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/18.jpg)
The process continues...
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Swap
with
front
Swap
with
front
![Page 19: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/19.jpg)
The process continues...
Sorted side Unsorted sideSorted side
is bigger
Sorted side
is bigger
[0] [1] [2] [3] [4] [5]
![Page 20: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/20.jpg)
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]
![Page 21: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/21.jpg)
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
![Page 22: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/22.jpg)
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]
![Page 23: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/23.jpg)
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; }}
![Page 24: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/24.jpg)
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
![Page 25: Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Does “@” come before or after “%”? Two algorithms considered](https://reader037.vdocuments.us/reader037/viewer/2022103022/56649ce25503460f949adf5a/html5/thumbnails/25.jpg)
Benefit › More efficient than Bubble Sort, due to
fewer exchanges
Disadvantage › Considered harder than Bubble Sort to
understand
9-25