Download presentation
Presentation is loading. Please wait.
Published byKryštof Pokorný Modified over 5 years ago
1
Data Structures CSCI 132, Fall 2018 Lecture 28 Insertion Sort for Linked List Selection Sort
2
Implementation of insertion sort with a linked list
last_sorted first_unsorted head 5 8 10 9 13 trailing current 1) Set up current and trailing pointers by moving them along the list until current points to the first node whose entry is greater than the entry at the first unsorted node. 2) Insert the first unsorted node between the trailing and current nodes: last_sorted->next = first_unsorted->next trailing->next = first_unsorted first_unsorted->next = current
3
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; }
4
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; } // *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;
5
Selection sort Selection sort decreases the number of moves made when sorting a contiguous list. Algorithm: Find the maximum entry in unsorted sublist Swap it to the end of the sublist. sorted unsorted
6
Auxiliary Functions template <class Record>
int Sortable_list<Record> :: max_key(int low, int high) { int largest, current; // We will work this out in class } void Sortable_list<Record> :: swap(int low, int high) { //we will work this out in class
7
Implementing Selection Sort
template <class Record> void Sortable_list<Record> :: selection_sort( ) { //We will work this out in class }
8
Analysis of Selection Sort
Comparisons: For a list of length k, max_key() does k -1 comparisons. max_key() is called by selection sort, n-1 times, for lists of length n, n-1, n-2, etc. (each time the list length decreases by 1). Total comparisons = (n -1) + (n-2) = Sk=1to(n-1) k = (n-1)(n)/2 = (1/2) n2 + O(n) Assignments: swap() is called (n-1) times from selection sort. Each time, swap performs 3 assignments. Total assignments = 3(n-1) = 3n + O(1)
9
Insertion Sort vs. Selection sort
Selection Insertion Assignments 3n + O(1) 0.25n2 + O(n) Comparisons 0.5n2 + O(n) 0.25n2 + O(n) If items are big and moving them is slow, then selection sort is faster. (3n vs. 0.25n2) If items are small, so moving them is fast, then insertion sort is faster. (0.25n2 vs. 0.5n2)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.