550 likes | 571 Views
Learn about the Insertion Sort and Shellsort algorithms, their implementations, time analysis, and comparisons. Dive into the principles of sorting and the efficiency of these interchangeable algorithms.
E N D
Chapter 7: Sorting(Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Text: Read Weiss, § 7.1 – 7.4 Izmir University of Economics
Preliminaries • Main memory sorting algorithms • All algorithms are Interchangeable; an array containing the N elements will be passed. • “<“, “>” (comparison) and “=“ (assignment) are the only operations allowed on the input data : comparison-based sorting Izmir University of Economics
Contents Selection Sort Insertion Sort Shell Sort
CE 221 Selection Sort
Time Analysis of Selection Sort Comparisons: Exchanges: N
CE 221 Insertion Sort
Time Analysis Number of comparisions: Number of excahnges:
Effective Algorithms Shell Sort
Shell Sort Move entries more than one position at a time by h–sorting the array Start at L and look at every 4th element and sort it Start at E and look at every 4th ekement and sort it Start at E and look at every 4th ekement and sort it Start at A and look at every 4th ekement and sort it
Example: S O R T E X A M P L E Result: A E E L M O P R S T X
Shell Sort vs Insertion Sort • Insertion sort compares every single item with all the rest elements of the list in order to find its place, • Shell sort compares items that lie far apart. This makes light elements to move faster to the front of the list.
The End Yes, the End !!!!!
Insertion Sort Analysis • One of the simplest sorting algorithms • Consists of N-1 passes. • for pass p = 1 to N-1 (0 thru p-1 already known to be sorted) • elements in position 0 trough p(p+1 elements)are sorted by moving the element left until a smaller element is encountered. Izmir University of Economics
Insertion Sort - Algorithm typedef int ElementType; voidInsertionSort( ElementType A[ ], int N ) { int j, P; ElementType Tmp; for( P = 1; P < N; P++ ) { Tmp = A[ P ]; for( j = P; j > 0 && A[ j - 1 ] > Tmp; j-- ) A[ j ] = A[ j - 1 ]; A[ j ] = Tmp; } } N*N iterations, hence, time complexity = O(N2) in the worst case.This bound is tight (input in the reverse order). Number of element comparisons in the inner loop is p, summing up over all p = 1+2+...+N-1 = Θ(N2). If the input is sorted O(N). Izmir University of Economics
A Lower Bound for Simple Sorting Algorithms • An inversion in an array of numbers is any ordered pair (i, j) such that a[i] > a[j]. • In the example set we have 9 inversions, (34,8),(34,32),(34,21),(64,51),(64,32),(64,21),(51,32),(51,21), and (32,21). • Swapping two adjacent elements that are out of place removes exactly one inversion and a sorted array has no inversions. • The running time of insertion sort O(I+N). Izmir University of Economics
Average Running Time for Simple Sorting - I • Theorem: The average number of inversions for N distinct elements is the average number of inversions in a permutation, i.e., N(N-1)/4. • Proof: It is the sum of the number of inversions in N! different permutations divided by N!. Each permutation L has a corresponding permutation LR which is reversed in sequence. If L has x inversions, then LR has N(N-1)/2 – x inversions. As a result ((N(N-1)/2) * (N!/2)) / N! = N(N-1)/4 is the number of inversions for an average list. Izmir University of Economics
Average Running Time for Simple Sorting - II • Theorem: Any algorithm that sorts by exchanging adjacent elements requires Ω(N2) time on average. • Proof: Initially there exists N(N-1)/4 inversions on the average and each swap removes only one inversion, so Ω(N2) swaps are required. • This is valid for all types of sorting algorithms (including those undiscovered) that perform only adjacent exchanges. • Result: For a sorting algorithm to run subquadratic (o(N2)), it must exchange elements that are far apart (eliminating more than just one inversion per exchange). Izmir University of Economics
Shellsort - I • Shellsort, invented by Donald Shell, works by comparing elements that are distant; the distance decreases as the algorithm runs until the last phase (diminishing increment sort) • Sequence h1, h2, ..., ht is called the increment sequence. h1= 1 always. After a phase, using hk, for every i, a[i] ≤ a[i+hk]. The file is then said to be hk-sorted. Izmir University of Economics
Shellsort - II • An hk-sorted file that is then hk-1-sorted remains hk-sorted. • To hk-sort, for each i in hk,hk+1,...,N-1, place the element in the correct spot among i, i-hk, i-2hk. This is equivalent to performing an insertion sort on hk independent subarrays. Izmir University of Economics