Data Structures CSCI 262, Spring 2002 Lecture 2 Classes and

Data Structures
CSCI 132, Spring 2014
Lecture 27
Insertion Sort
1
More Examples
Compare the following functions:
5) f(n) = lg(lg(n))
g(n) = lg(n)
6) f(n) = lg(n)
g(n) = n0.5
7) f(n) = lg3(n) = (lg(n))3
g(n) = n0.5
2
Sortable Lists
template <class Record>
class Sortable_list: public List<Record> {
public:
// Add prototypes for sorting methods here.
private:
// Add prototypes for auxiliary functions here.
};
3
Insertion Sort
1. Start at beginning of list and continue with each successive
item.
2. For each item, insert it into appropriate position in sorted list.
sorted
7 3 8 5 2
unsorted
3 7 8 5 2
3 7 8 5 2
3 5 7 8 2
2 3 5 7 8
4
Implementation for contiguous
list
template <class Record>
void Sortable_list<Record> :: insertion_sort( )
{
int first_unsorted;
// position of first unsorted entry
int position;
// searches sorted part of list
Record current;
// holds the entry temporarily removed from list
//we will finish this in class
}
5
Implementation for contiguous
list
template <class Record>
void Sortable_list<Record> :: insertion_sort( )
{
int first_unsorted;
// position of first unsorted entry
int position;
// searches sorted part of list
Record current;
// holds the entry temporarily removed from list
for (first_unsorted = 1; first_unsorted < count; first_unsorted++) {
position = first_unsorted;
current = entry[first_unsorted];
// Pull unsorted entry out of the list.
while (position > 0 && entry[position - 1] > current) {
// Shift all entries until the proper position is found.
entry[position] = entry[position - 1];
position--;
}
entry[position] = current;
}
}
6
Analysis of running time
1. Must insert n-1 entries in proper place.
2. How many comparisons must we do to insert each entry?
•Assume we start with a random list so there is an equal
probability of each order of entries.
•Suppose we have sorted i-1 entries and want to insert the
ith entry.
•Probability of not moving it = 1/i
•Probability of moving it = (i - 1) /i
•If we move it, the average number of moves = i/2
7
Analysis continued
Average number of assignments = i/2 + 2
Average number of comparisons = i/2 + 1
Total for moving and non-moving cases:
Comparisons: (1/i) * 1 + (i - 1)/i * (i/2 + 1) = (i + 1)/2
Assignments: 1/i * 2 + (i - 1)/i * (i/2 + 2) = (i + 3)/2
= (1/2) i + O (1)
We repeat this for i = 2 up to n.
S(i = 2 to n) ((1/2) i + O (1)) = (1/4) n2 + O(n)
8
Implementation of insertion
sort with a linked list
last_sorted
first_unsorted
head
5
8
trailing
10
9
13
current
last_sorted->next = first_unsorted->next
trailing->next = first_unsorted
first_unsorted->next = current
9
Implementation for linked list
template <class Record>
void Sortable_list<Record> :: insertion_sort( )
{
Node <Record> *first_unsorted, *last_sorted, *current, *trailing;
if (head != NULL) {
// Otherwise, the empty list is already sorted.
last_sorted = head; // The first node alone makes a sorted sublist.
while (last_sorted->next != NULL) {
first_unsorted = last_sorted->next;
if (first_unsorted->entry < head->entry) {
// Insert *first_unsorted at the head of the sorted list:
last_sorted->next = first_unsorted->next;
first_unsorted->next = head;
head = first_unsorted;
}
10
Linked list implementation
continued
else {
// Search the sorted sublist to insert *first_unsorted:
trailing = head;
current = trailing->next;
while (first_unsorted->entry > current->entry) {
trailing = current;
current = trailing->next;
}
// *first_unsorted now belongs between *trailing and *current.
if (first_unsorted == current) {
last_sorted = first_unsorted; // already in right position
} else {
last_sorted->next = first_unsorted->next;
first_unsorted->next = current;
trailing->next = first_unsorted;
}
}
}
}
}