Docsity

Sorting Algorithms: A Comparison of Bubble Sort, Selection Sort, and Insertion Sort, Essays (high school) of Computer science

Toggle sidebar
bg1
1
Week4:
Sorting
Algorithms
Bubble sort
Compare each element (except the last one) with its
neighbor to the right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last two) with its
neighbor to the right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final
places
Compare each element (except the last three) with its
neighbor to the right
Continue as above until you have no unsorted elements on
the left
2
pf3
pf4
pf5
pf8

Partial preview of the text

Download Sorting Algorithms: A Comparison of Bubble Sort, Selection Sort, and Insertion Sort and more Essays (high school) Computer science in PDF only on Docsity!

Week4:

Sorting

Algorithms

Bubble sort

 Compare each element (except the last one) with its

neighbor to the right

 If they are out of order, swap them  This puts the largest element at the very end  The last element is now in the correct and final place

 Compare each element (except the last two ) with its

neighbor to the right

 If they are out of order, swap them  This puts the second largest element next to last  The last two elements are now in their correct and final places

 Compare each element (except the last three ) with its

neighbor to the right

 Continue as above until you have no unsorted elements on the left

2

Example of bubble sort 3

7 2 8 5 4

2 7 8 5 4

2 7 8 5 4

2 7 5 8 4

2 7 5 4 8

2 7 5 4 8

2 5 7 4 8

2 5 4 7 8

2 7 5 4 8

2 5 4 7 8

2 4 5 7 8

2 5 4 7 8

2 4 5 7 8

2 4 5 7 8

(done)

Code for bubble sort

 public static void bubbleSort(int[] a) {

int outer, inner;

for (outer = a.length - 1; outer > 0; outer--) { // counting down

for (inner = 0; inner < outer; inner++) { // bubbling up

if (a[inner] > a[inner + 1]) { // if out of order...

int temp = a[inner]; // ...then swap

a[inner] = a[inner + 1];

a[inner + 1] = temp;

4

Selection sort

 Given an array of length n,

 Search elements 0 through n-1 and select the smallest

 Swap it with the element in location 0

 Search elements 1 through n-1 and select the smallest

 Swap it with the element in location 1

 Search elements 2 through n-1 and select the smallest

 Swap it with the element in location 2

 Search elements 3 through n-1 and select the smallest

 Swap it with the element in location 3

 Continue in this fashion until there’s nothing left to search

7

Example and analysis of

selection sort

 The selection sort might swap an array

element with itself--this is harmless, and not

worth checking for

 Analysis:

 The outer loop executes n-1 times

 The inner loop executes about n/2 times on average (from n to 2 times)

 Work done in the inner loop is constant (swap two array elements)

 Time required is roughly (n-1)*(n/2)

 You should recognize this as O(n 2 )

8

7 2 8 5 4

2 7 8 5 4

2 4 8 5 7

2 4 5 8 7

2 4 5 7 8

Code for selection sort

public static void selectionSort(int[] a) {

int outer, inner, min;

for (outer = 0; outer < a.length - 1; outer++) {

min = outer;

for (inner = outer + 1; inner < a.length; inner++) {

if (a[inner] < a[min]) {

min = inner;

// Invariant: for all i, if outer <= i <= inner, then a[min] <= a[i]

// a[min] is least among a[outer]..a[a.length - 1]

int temp = a[outer];

a[outer] = a[min];

a[min] = temp;

// Invariant: for all i <= outer, if i < j then a[i] <= a[j]

9

Invariants for selection sort

 For the inner loop:

 This loop searches through the array, incrementing inner

from its initial value of outer+1 up to a.length-

 As the loop proceeds, min is set to the index of the smallest

number found so far

 Our invariant is:

for all i such that outer <= i <= inner, a[min] <= a[i]

 For the outer (enclosing) loop:

 The loop counts up from outer = 0

 Each time through the loop, the minimum remaining value

is put in a[outer]

 Our invariant is:

for all i <= outer, if i < j then a[i] <= a[j]

10

Analysis of insertion sort

 We run once through the outer loop, inserting

each of n elements; this is a factor of n

 On average, there are n/2 elements already

sorted

 The inner loop looks at (and moves) half of these

 This gives a second factor of n/

 Hence, the time required for an insertion sort of an

array of n elements is proportional to n^2 /

 Discarding constants, we find that insertion sort is

O(n^2 )

13

Summary

 Most of the sorting techniques we have discussed are O(n^2 )

 As we will see later, we can do much better than this with

somewhat more complicated sorting algorithms

 Within O(n^2 ),

 Bubble sort is very slow, and should probably never be used for anything

 Selection sort is intermediate in speed

 Insertion sort is usually faster than selection sort—in fact, for small arrays (say, 10 or 20 elements), insertion sort is faster than more complicated sorting algorithms

 Selection sort and insertion sort are “good enough” for small

arrays

14

References 15

Wirth, N. (1986), Algorithms & Data Structures , Upper

Saddle River, NJ: Prentice-Hall

Goodrich, M.T.; Tamassia, R. (2002). " Bucket-Sort and Radix-

Sort ". Algorithm Design: Foundations, Analysis, and Internet

Examples. John Wiley & Sons.