Download presentation
Presentation is loading. Please wait.
Published byRachel Andrews Modified over 9 years ago
2
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
3
1. Compare 1 st two elements and exchange them if they are out of order. 2. Move down one element and compare 2 nd and 3 rd 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
4
9-4 01234 53142 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
5
9-5 1 3 2 4 5 1 3 2 3 1 2 3 4 5 Pass 2 Pass 3 Pass 4 01234 53142
6
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
7
Benefit Easy to understand and implement Disadvantage Inefficiency makes it slow for large arrays 9-7
8
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
9
The picture shows an array of six integers that we want to sort from smallest to largest [0] [1] [2] [3] [4] [5]
10
Start by finding the smallest entry. [0] [1] [2] [3] [4] [5]
11
Start by finding the smallest entry. Swap the smallest entry with the first entry. [0] [1] [2] [3] [4] [5]
12
Start by finding the smallest entry. Swap the smallest entry with the first entry. [0] [1] [2] [3] [4] [5]
13
Part of the array is now sorted. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
14
Find the smallest element in the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
15
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]
16
We have increased the size of the sorted side by one element. Sorted side Unsorted side [0] [1] [2 ] [3] [4] [5]
17
The process continues... Sorted side Unsorted side Smallest from unsorted Smallest from unsorted [0] [1] [2] [3] [4] [5]
18
The process continues... Sorted side Unsorted side [0] [1] [2] [3] [4] [5] Swap with front Swap with front
19
The process continues... Sorted side Unsorted side Sorted side is bigger Sorted side is bigger [0] [1] [2] [3] [4] [5]
20
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]
21
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
22
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]
23
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; }
24
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
25
Benefit › More efficient than Bubble Sort, due to fewer exchanges Disadvantage › Considered harder than Bubble Sort to understand 9-25
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.