Docsity

Algorithm Efficiency & Sorting: Selection, Bubble, Insertion, Merge, Quick, & Radix Sorts , Study notes of Computer Science

Toggle sidebar
bg1
CHAPTER 9
ALGORITHM EFFICIENCY AND SORTING
Defn: An internal sort is a sort in which the array is
sufficiently small so as to fit into internal memory.
Defn: An in situ sort is a sort in which the unsorted and sorted
arrays occupy the same space, possibly with a small
amount of auxiliary working storage.
For simplicity, we will consider ascending sorts of arrays in all
of our algorithms.
Example: Typical array for sorting
5 4 8 2 1 7 3 6
1 2 3 4 5 6 7 8
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Algorithm Efficiency & Sorting: Selection, Bubble, Insertion, Merge, Quick, & Radix Sorts and more Study notes Computer Science in PDF only on Docsity!

CHAPTER 9

ALGORITHM EFFICIENCY AND SORTING

Defn: An internal sort is a sort in which the array is

sufficiently small so as to fit into internal memory.

Defn: An in situ sort is a sort in which the unsorted and sorted

arrays occupy the same space, possibly with a small

amount of auxiliary working storage.

For simplicity, we will consider ascending sorts of arrays in all

of our algorithms.

Example: Typical array for sorting

A. Selection Sort

Principle:

st

pass , find the largest value and place it the last slot.

nd

pass , find the 2

nd

largest value and place it the 2

nd

to last

slot.

 n

th

pass , find the n

th

largest value and place it the n

th

to last

slot.

Notes:

 For an array of size n, make n passes.

 The last pass is really not necessary.

B. Bubble Sort

Principle: Make n passes through the array. For each pass:

 if two adjacent elements are in the wrong order,

swap them

The smaller elements will bubble up to the top, while the

larger elements slowly sink to the bottom.

Note: The last pass is not necessary.

C++ Code for Bubble Sort (Carrano, Helman and Veroff, p. 451)

void bubbleSort(DataType theArray[], int n)

// Sorts the items in an array into ascending order

bool sorted = false; // false when swaps occur

for (int pass = 1; (pass < n) && !sorted; ++pass) {

sorted = true; // assume sorted

for (int index = 0; index < n - pass; ++index) {

int nextIndex = index + 1;

if (theArray[index] > theArray[nextIndex]) {

// exchange items

swap(theArray[index], theArray[nextIndex]);

sorted = false;

Note: You can actually stop this algorithm early if no elements change position for a complete pass. Performance: Worst case = O(n

) Best case = O(n), with early stopping

C++ Code for Insertion Sort (Carrano, Helman and Veroff, pp. 453-454)

void insertionSort(DataType theArray[], int n)

// Sorts the items in an array into ascending order

// unsorted = first index of the unsorted region

// loc = index of insertion in the sorted region

// nextItem = next item in the unsorted region

// Initially, sorted region is theArray[0],

// unsorted region is theArray[1..n - 1]

// In general, sorted region is theArray[0..unsorted - 1],

// unsorted region is theArray[unsorted..n - 1]

for (int unsorted = 1; unsorted < n; ++unsorted) {

// Find the right position (loc) in

// theArray[0..unsorted] for theArray[unsorted}, which is the

// first item in the unsorted region.

// Shift, if necessary, to make room

DataType nextItem = theArray[unsorted];

int loc = unsorted;

for ( ; (loc > 0) && (theArray[loc - 1] > nextItem); --loc)

// Shift theArray[loc - 1] to the right

theArray[loc] = theArray[loc - 1];

// Insert nextItem into Sorted region

theArray[loc] = nextItem;

Performance: O(n

)

D. Merge Sort

Principle: It is easy (O(n)) to merge two arrays that are already

sorted into one larger sorted array.

To merge two sorted arrays A and B ,

 examine the 1

st

element of A

 copy everything from the front of B that is smaller than

A’s front into a new array

 copy the 1

st

element of A into the new array

 repeat until both arrays are empty

A

B

Merged

Array

E. Quick Sort

Principle:

 Select an item from the array and call it Middle

 Put Middle into the array so that:

 items < Middle are to the left of it

 items > Middle are to the right of it

 Recursively repeat these steps on the arrays “< Middle” and

“> Middle”.

Items < Middle Items >

Middle Middle

Performance:

Average Case: O(n * log 2 n)

Worst Case: O(n

F. Radix Sort

Principle: Instead of comparing the value of the item to be

sorted, compare digit by digit.

Algorithm:

 Sort by the last digit

 Sort by the next to last digit

 Sort by the 1

st

digit

Performance: O(n)

 # of passes = # of bits in the key

 For a fixed size key, # of passes is constant.

 In each pass, we examine each record once (and there are n

records).

(constant * n) = O(n)

But, much extra space is required!

G. Summary

Sort Performance Memory Other

Selection

Bubble

Insertion

Merge

Quick

Radix