Download presentation
Presentation is loading. Please wait.
1
Algorithms for Sorting Things
2
Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds more than 4,000 movies –how can I find “The Incredibles”
3
Sorting Integers How to sort the integers in this array? 28 18 7 4 10 4 7 10 18 28
4
Elementary Sorting Algorithms Selection Sort Insertion Sort Bubble Sort
5
Selection Sort Main idea: –find the smallest element –put it in the first position –find the next smallest element –put it in the second position –…
6
The algorithm splits the array into two parts: already sorted, and not yet sorted. On each pass, the algorithm locates the smallest of the unsorted elements, and moves into the correct position Straight Selection Sort 28 18 7 4 10
7
Selection Sort: Pass One UNSORTEDUNSORTED 28 18 7 4 10
8
Selection Sort: End Pass One UNSORTEDUNSORTED 4 18 7 28 10 SORTEDSORTED
9
Selection Sort: End Pass Two UNSORTEDUNSORTED 4 7 18 28 10 SORTEDSORTED
10
Selection Sort: Pass Three UNSORTEDUNSORTED 4 7 10 28 18 SORTEDSORTED
11
Selection Sort: End Pass Three 4 7 10 18 28 SORTEDSORTED
12
Insertion Sort Main Idea: Starts by considering the first two elements of the array data, if out of order, swap them Consider the third element, insert it into the proper position among the first three elements. Consider the forth element, insert it into the proper position among the first four elements. …
13
One by one, each as yet unsorted array element is inserted into its proper place with respect to the already sorted elements. On each pass, this causes the number of already sorted elements to increase by one. Insertion Sort 28 18 7 4 10
14
Insertion Sort: Pass One UNSORTEDUNSORTED 28 18 7 4 10 SORTEDSORTED
15
Insertion Sort: Pass Two 18 28 7 4 10 SORTEDSORTED UNSORTEDUNSORTED
16
Insertion Sort: Pass Three 7 18 28 4 10 SORTEDSORTED UNSORTEDUNSORTED
17
Insertion Sort: Pass Four 4 7 18 28 10 SORTEDSORTED UNSORTEDUNSORTED
18
Insertion Sort: Pass Five 4 7 10 18 28 SORTEDSORTED
19
Bubble Sort The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order). This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list.
20
Compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order. On each pass, this causes the smallest element to “bubble up” to its correct place in the array. Bubble Sort 28 18 7 4 10
21
Bubble Sort: Pass One UNSORTEDUNSORTED 28 18 7 4 10
22
Bubble Sort: Pass One UNSORTEDUNSORTED 28 18 7 4 10
23
Bubble Sort: Pass One UNSORTEDUNSORTED 28 18 4 7 10
24
Bubble Sort: Pass One UNSORTEDUNSORTED 28 4 18 7 10
25
Bubble Sort: End Pass One UNSORTEDUNSORTED 4 28 18 7 10 SORTEDSORTED
26
Bubble Sort: Pass Two UNSORTEDUNSORTED 4 28 18 7 10 SORTEDSORTED
27
Bubble Sort: Pass Two UNSORTEDUNSORTED 4 28 18 7 10 SORTEDSORTED
28
Bubble Sort: Pass Two UNSORTEDUNSORTED 4 28 7 18 10 SORTEDSORTED
29
Bubble Sort: End Pass Two UNSORTEDUNSORTED 4 7 28 18 10 SORTEDSORTED
30
Bubble Sort: Pass Three UNSORTEDUNSORTED 4 7 28 18 10 SORTEDSORTED
31
Bubble Sort: Pass Three 4 7 28 10 18 SORTEDSORTED UNSORTEDUNSORTED
32
Bubble Sort: End Pass Three 4 7 10 28 18 SORTEDSORTED UNSORTEDUNSORTED
33
Bubble Sort: Pass Four 4 7 10 28 18 SORTEDSORTED UNSORTEDUNSORTED
34
Bubble Sort: End Pass Four 4 7 10 18 28 SORTEDSORTED
36
A Pascal Program Incorporating a Bubble Sort
39
Research Topic The bubble sort is rather slow because it operates only locally each item moves only one position at a time The literature on algorithms discusses a number of alternatives that are more efficient but more complex When you fully understand the logic of the bubble sort take a look at these other algorithms –Merge sorts and Quick Sorts
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.