10.6 Shell Sort: A Better Insertion

Slides:



Advertisements
Similar presentations
ITEC200 Week10 Sorting. pdp 2 Learning Objectives – Week10 Sorting (Chapter10) By working through this chapter, students should: Learn.
Advertisements

CPS120: Introduction to Computer Science Searching and Sorting.
HST 952 Computing for Biomedical Scientists Lecture 9.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
An Introduction to Sorting Chapter 9. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Spring 2010CS 2251 Sorting Chapter 8. Spring 2010CS 2252 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn.
An Introduction to Sorting Chapter 8. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Sorting Chapter 10.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Section 8.4 Insertion Sort CS Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.
Data Structures Using C++ 2E
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Sorting 2 Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
1 Sorting اعداد: ابوزيد ابراهيم حامد سعد صبرة حميده الشاذلي عبدالاه السيد محمد احمد.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 10: Sorting1 Sorting. Chapter 10: Sorting2 Chapter Outline How to use standard sorting functions in How to implement these sorting algorithms:
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
Sorting.
16 Searching and Sorting.
Sorting Mr. Jacobs.
Recitation 13 Searching and Sorting.
Section 10.3a Merge Sort.
An Introduction to Sorting
Sorting Chapter 10.
Sorting Chapter 8.
10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.
10.3 Bubble Sort Chapter 10 - Sorting.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Wednesday, April 11, 2018 Announcements… For Today…
Sorting Algorithms IT12112 Lecture 07.
Advanced Sorting Methods: Shellsort
Selection Sort Sorted Unsorted Swap
Quicksort analysis Bubble sort
MSIS 655 Advanced Business Applications Programming
8/04/2009 Many thanks to David Sun for some of the included slides!
C++ Plus Data Structures
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Yan Shi CS/SE 2630 Lecture Notes
Sorting Chapter 8 CS 225.
Divide and Conquer Algorithms Part I
Chapter 4.
Sorting Chapter 8.
Analysis of Algorithms
CSE 373 Data Structures and Algorithms
Sorting Chapter 10.
Chapter 10 Sorting Algorithms
Searching/Sorting/Searching
Sorting Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
10.3 Bubble Sort Chapter 10 - Sorting.
Presentation transcript:

10.6 Shell Sort: A Better Insertion 10.6, pgs. 588-590

Attendance Quiz #35 Sorting

Tip #37: Equal vs Equivalence Sorting STL is packed in comparisons of objects to see if they have the same value. Find – locate the first object of a particular value in a specified range. Insert – add a new element to a set if not already in the set. When are values "the same"? Find's definition of "the same" is equality, which is based on operator==. Set insert's definition of "the same" is equivalence, which is based on operator<. Equal (operator=) returns true if "x == y", else false. Equivalence (operator<) is based on the relative ordering of object values and returns true if neither object precedes the other, else false (ie. !(w1 < w2) && !(w2 < w1)).

10.6 Shell Sort: A Better Insertion Sort Analysis of Shell Sort Code for Shell Sort 10.6, pgs. 588-590

Shell Sort: A Better Insertion Sort Sorting A Shell sort is a type of insertion sort, but with O(n3/2) or better performance than the O(n2) sorts. It is named after its discoverer, Donald Shell. A Shell sort can be thought of as a divide-and-conquer approach to insertion sort. Instead of sorting the entire array, Shell sort sorts many smaller subarrays using insertion sort before sorting the entire array. The initial subarrays will contain two or three elements, so the insertion sorts will go very quickly. After each collection of subarrays is sorted, a new collection of subarrays with approximately twice as many elements as before will be sorted. The last step is to perform an insertion sort on the entire array, which has been presorted by the earlier sorts.

Shell Sort Algorithm Sorting 1. Set the initial value of gap to n / 2 2. while gap > 0 3. for each array element from position gap to the last element 4. Insert this element where it belongs in its subarray. 4.1 next_pos is an iterator to the element to insert 4.2 Save the value of the element to insert in next_val 4.3 while next_pos > first + gap and the element at next_pos – gap > next_val 4.4 Shift the element at next_pos – gap to position next_pos 4.5 Decrement next_pos by gap 4.6 Insert next_val at next_pos 5. if gap is 2, set it to 1 6. else gap = gap / 2.2 // chosen by experimentation

How many subarrays with gap of 7? Shell Sort Sorting gap value 7 Let’s sort the following array using initial subarrays with only two and three elements. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 How many subarrays with gap of 7?

We will use an initial gap of 7. Shell Sort Sorting gap value 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 1 We determine the elements in each subarray by setting a gap value between the subscripts in each subarray. We will use an initial gap of 7.

Shell Sort Sorting gap value 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 2

Shell Sort Sorting gap value 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 3

Shell Sort Sorting gap value 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 4

Shell Sort Sorting gap value 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 5

Shell Sort Sorting gap value 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 6

Shell Sort Sorting gap value 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 7

Shell Sort gap value 7 Sort subarray 1 40 35 80 75 60 90 70 55 85 34 Sorting gap value 7 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 1

Shell Sort gap value 7 Sort subarray 2 40 35 80 75 60 90 70 55 85 34 Sorting gap value 7 Sort subarray 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 2

Shell Sort gap value 7 Sort subarray 3 40 35 80 75 60 90 70 55 85 34 Sorting gap value 7 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 3

Shell Sort gap value 7 Sort subarray 4 40 35 80 75 60 90 70 55 85 34 Sorting gap value 7 Sort subarray 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 4

Shell Sort gap value 7 Sort subarray 5 40 35 80 75 60 90 70 55 85 34 Sorting gap value 7 Sort subarray 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65 subarray 5

Shell Sort gap value 7 Sort subarray 5 40 35 80 75 34 90 70 55 85 60 Sorting gap value 7 Sort subarray 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 90 70 55 85 60 45 62 57 65 subarray 5

Shell Sort gap value 7 Sort subarray 6 40 35 80 75 34 90 70 55 85 60 Sorting gap value 7 Sort subarray 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 90 70 55 85 60 45 62 57 65 subarray 6

Shell Sort gap value 7 Sort subarray 6 40 35 80 75 34 45 70 55 90 85 Sorting gap value 7 Sort subarray 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 70 55 90 85 60 62 57 65 subarray 6

Shell Sort gap value 7 Sort subarray 7 40 35 80 75 34 45 70 55 90 85 Sorting gap value 7 Sort subarray 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 70 55 90 85 60 62 57 65 subarray 7

Shell Sort gap value 7 Sort subarray 7 40 35 80 75 34 45 62 55 90 85 Sorting gap value 7 Sort subarray 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 62 55 90 85 60 70 57 65 subarray 7

Shell Sort gap value 7 Sort subarray 1 40 35 80 75 34 45 62 55 90 85 Sorting gap value 7 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 62 55 90 85 60 70 57 65 subarray 1

Shell Sort gap value 7 Sort subarray 1 40 35 80 75 34 45 62 57 55 90 Sorting gap value 7 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 62 57 55 90 85 60 70 65 subarray 1

Shell Sort gap value 7 Sort subarray 2 40 35 80 75 34 45 62 57 55 90 Sorting gap value 7 Sort subarray 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 62 57 55 90 85 60 70 65 subarray 2

Sort on smaller gap value Shell Sort Sorting gap value 3 Sort on smaller gap value 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 62 57 55 90 85 60 70 65

Shell Sort gap value 3 Sort subarray 1 40 35 80 75 34 45 62 57 55 90 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 62 57 55 90 85 60 70 65 subarray 1

Shell Sort gap value 3 Sort subarray 2 40 35 80 75 34 45 62 57 55 90 Sorting gap value 3 Sort subarray 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 34 45 62 57 55 90 85 60 70 65 subarray 2

Shell Sort gap value 3 Sort subarray 2 40 34 80 75 35 45 62 57 55 90 Sorting gap value 3 Sort subarray 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 80 75 35 45 62 57 55 90 85 60 70 65 subarray 2

Shell Sort gap value 3 Sort subarray 3 40 34 80 75 35 45 62 57 55 90 Sorting gap value 3 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 80 75 35 45 62 57 55 90 85 60 70 65 subarray 3

Shell Sort gap value 3 Sort subarray 3 40 34 45 75 35 80 62 57 55 90 Sorting gap value 3 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 75 35 80 62 57 55 90 85 60 70 65 subarray 3

Shell Sort gap value 3 Sort subarray 1 40 34 45 75 35 80 62 57 55 90 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 75 35 80 62 57 55 90 85 60 70 65 subarray 1

Shell Sort gap value 3 Sort subarray 1 40 34 45 62 35 80 75 57 55 90 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 80 75 57 55 90 85 60 70 65 subarray 1

Shell Sort gap value 3 Sort subarray 2 40 34 45 62 35 80 75 57 55 90 Sorting gap value 3 Sort subarray 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 80 75 57 55 90 85 60 70 65 subarray 2

Shell Sort gap value 3 Sort subarray 3 40 34 45 62 35 80 75 57 55 90 Sorting gap value 3 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 80 75 57 55 90 85 60 70 65 subarray 3

Shell Sort gap value 3 Sort subarray 3 40 34 45 62 35 55 75 57 80 90 Sorting gap value 3 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 80 90 85 60 70 65 subarray 3

Shell Sort gap value 3 Sort subarray 1 40 34 45 62 35 55 75 57 80 90 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 80 90 85 60 70 65 subarray 1

Shell Sort gap value 3 Sort subarray 2 40 34 45 62 35 55 75 57 80 90 Sorting gap value 3 Sort subarray 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 80 90 85 60 70 65 subarray 2

Shell Sort gap value 3 Sort subarray 3 40 34 45 62 35 55 75 57 80 90 Sorting gap value 3 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 80 90 85 60 70 65 subarray 3

Shell Sort gap value 3 Sort subarray 3 40 34 45 62 35 55 75 57 60 90 Sorting gap value 3 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 90 85 80 70 65 subarray 3

Shell Sort gap value 3 Sort subarray 1 40 34 45 62 35 55 75 57 60 90 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 90 85 80 70 65 subarray 1

Shell Sort gap value 3 Sort subarray 2 40 34 45 62 35 55 75 57 60 90 Sorting gap value 3 Sort subarray 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 90 85 80 70 65 subarray 2

Shell Sort gap value 3 Sort subarray 2 40 34 45 62 35 55 75 57 60 90 Sorting gap value 3 Sort subarray 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 90 70 80 85 65 subarray 2

Shell Sort gap value 3 Sort subarray 3 40 34 45 62 35 55 75 57 60 90 Sorting gap value 3 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 90 70 80 85 65 subarray 3

Shell Sort gap value 3 Sort subarray 3 40 34 45 62 35 55 75 57 60 90 Sorting gap value 3 Sort subarray 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 90 70 85 80 65 subarray 3

Shell Sort gap value 3 Sort subarray 1 40 34 45 62 35 55 75 57 60 90 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 90 70 85 80 65 subarray 1

Shell Sort gap value 3 Sort subarray 1 40 34 45 62 35 55 75 57 60 90 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 90 70 65 85 80 subarray 1

Shell Sort gap value 3 Sort subarray 1 40 34 45 62 35 55 75 57 60 65 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 75 57 60 65 70 90 85 80 subarray 1

Shell Sort gap value 3 Sort subarray 1 40 34 45 62 35 55 65 57 60 75 Sorting gap value 3 Sort subarray 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 65 57 60 75 70 90 85 80 subarray 1

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 34 45 62 35 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 40 45 62 35 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 40 45 62 35 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 40 45 62 35 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 40 45 62 35 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 40 45 62 35 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 40 45 62 35 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 40 45 35 62 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 40 35 45 62 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 62 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 62 55 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 62 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 62 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 62 65 57 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 62 57 65 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 62 65 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 62 65 60 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 62 60 65 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 75 70 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 90 85 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 85 90 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 85 90 80

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 85 80 90

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 80 85 90

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 80 85 90

Sort on gap value of 1 (a regular insertion sort) Shell Sort Sorting gap value 1 Sort on gap value of 1 (a regular insertion sort) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 34 35 40 45 55 57 60 62 65 70 75 80 85 90

Sort the following integer array in ascending order using shell sort Sort the following integer array in ascending order using shell sort. Show each step. 26 1 54 2 93 3 17 4 77 5 31 6 44 7 55 8 20 How many insertions?

Sort the following integer array in ascending order using shell sort Sort the following integer array in ascending order using shell sort. Show each step. 26 1 54 2 93 3 17 4 77 5 31 6 44 7 55 8 20 26 54 93 17 77 31 44 55 20 26 31 93 17 77 54 44 55 20 26 31 44 17 77 54 93 55 20 26 31 44 17 77 54 93 55 20 20 31 44 17 26 54 93 55 77 17 20 31 44 26 54 93 55 77 17 20 26 31 44 54 93 55 77 17 20 26 31 44 54 55 93 77 17 20 26 31 44 54 55 77 93 Gap = 8 / 2 = 4 Gap = 4 / 2.2 = 1 How many insertions?

Analysis of Shell Sort Sorting Because the behavior of insertion sort is closer to O(n) than O(n2) when an array is nearly sorted, presorting speeds up later sorting. This is critical when sorting large arrays where the O(n2) performance becomes significant. A general analysis of Shell sort is an open research problem in computer science. Performance depends upon the decreasing sequence of values for gap: If successive powers of 2 are used for gap, performance is O(n2) If successive values for gap are based on Hibbard's sequence, 2k – 1 (i.e. 31, 15, 7, 3, 1) it can be proven that the performance is O(n3/2) Other sequences give similar or better performance. Our algorithm selects the initial value of gap as n/2, divides by 2.2, and truncates the result. Empirical studies show that the performance is O(n5/4) or even O(n7/6), but there is no theoretical basis for this result.

Code for Shell Sort Sorting #ifndef SHELLSORT_H_ #define SHELLSORT_H_ /** Insert the element at position next_pos in the sorted subarray. */ template<typename RI> void insert2(RI first, RI next_pos, int gap) { typename std::iterator_traits<RI>::value_type next_val = *next_pos; while ((next_pos > first + gap - 1) && (next_val < *(next_pos - gap))) *next_pos = *(next_pos - gap); // Shift down. next_pos -= gap; // Check next position in subarray. } *next_pos = next_val; return; /** Sort data in the specified range using Shell sort. */ void shell_sort(RI first, RI last) int gap = (last - first - 1) / 2; // Set initial gap between adjacent elements. while (gap > 0) for (RI next_pos = first + gap; next_pos != last; ++next_pos) insert2(first, next_pos, gap); // Insert element at next_pos in its subarray. gap = (gap == 2) ? 1 : int(gap / 2.2); // Reset gap for next pass. #endif // SHELLSORT_H_

10.7, pgs. 592-597 10.7 Merge Sort Analysis of Merge Code for Merge Algorithm for Merge Sort Trace of Merge Sort Algorithm Analysis of Merge Sort Code for Merge Sort 10.7, pgs. 592-597

Merge Sort Sorting A merge is a common data processing operation performed on two sequences of data with the following characteristics: Both sequences are ordered by the same comparison operator (that is, both sequences are sorted). The result of the merge operation is a third sequence containing all the data from the first two sequences. Analysis of Merge Sort: For two input sequences each containing n elements, each element needs to move from its input sequence to the output sequence. Merge time is O(n). Space requirements: The array cannot be merged in place. Additional space usage is O(n).

Merge Algorithm Merge Algorithm Sorting Merge Algorithm 1. Access the first item from both sequences. 2. while not finished with either sequence 3. Compare the current items from the two sequences, copy the smaller current item to the output sequence, and access the next item from the input sequence whose item was copied. 4. Copy any remaining items from the first sequence to the output sequence. 5. Copy any remaining items from the second sequence to the output sequence.

Code for Merge Sorting /** Merge data in two sorted input sequences into a sorted output sequence. pre: Both input sequences are sorted. post: The output sequence is sorted and contains all elements in both sequences. @param left/end_left: left sequence @param right/end_right: right sequence @param out: first element in the output sequence */ template<typename RI> void merge(RI left, RI end_left, RI right, RI end_right, RI out) { // While there is data in both input sequences while ((left != end_left) && (right != end_right)) // Find the smaller and insert it into the output sequence. if (*left < *right) *out++ = *left++; else *out++ = *right++; } // Assert: one of the sequences has more items to copy. // Copy remaining input from left/right sequence into the output. while (left != end_left) *out++ = *left++; while (right != end_right) *out++ = *right++;

Recursive Merge Sort Sorting We can modify merging to sort a single, unsorted array Split the array into two halves Sort the left half Sort the right half Merge the two This algorithm can be written with a recursive step If the table has more than one element Store the first half of the table in left_table Store the second half of the table in right_table Recursively apply the merge sort algorithm to left_table Recursively apply the merge sort algorithm to right_table Call the merge function with left_table and right_table as the input sequences and the original table as the output sequence.

Merge Sort Sorting 50 50 60 45 30 90 20 80 15 60 45 30 90 20 80 15 50 60 45 30 90 20 80 15

Merge Sort Sorting 50 60 45 30 90 20 80 15 50 50 60 45 30 90 20 80 15 60 45 30 50 60 45 30

Merge Sort Sorting 50 60 45 30 90 20 80 15 50 60 45 30 90 20 80 15 50 50 60 60 45 30 50 60

Merge Sort Sorting 50 60 45 30 90 20 80 15 50 60 45 30 90 20 80 15 50 50 60 60 45 30 60

Merge Sort Sorting 50 60 45 30 90 20 80 15 50 60 45 30 90 20 80 15 50 60 45 45 30 30 45 30

Merge Sort Sorting 50 60 45 30 90 20 80 15 50 60 45 30 90 20 80 15 50 60 45 30 45 30 30 45 45

Merge Sort Sorting 50 60 45 30 90 20 80 15 30 45 50 60 50 60 45 30 90 20 80 15 50 60 45 30

Merge Sort Sorting 50 60 45 30 90 20 80 15 30 45 50 60 50 60 45 30 90 20 80 15 90 20 80 15

Merge Sort Sorting 50 60 45 30 90 20 80 15 30 45 50 60 50 60 45 30 90 20 80 15 90 20 80 15 90 20

Merge Sort Sorting 50 60 45 30 90 20 80 15 30 45 50 60 50 60 45 30 90 20 80 15 20 90 90 20 80 15 90 20

Merge Sort Sorting 50 60 45 30 90 20 80 15 30 45 50 60 50 60 45 30 90 20 80 15 20 90 80 15 80 15

Merge Sort Sorting 50 60 45 30 90 20 80 15 30 45 50 60 50 60 45 30 90 20 80 15 20 90 15 80 80 15 80 15

Merge Sort Sorting 50 60 45 30 90 20 80 15 30 45 50 60 50 60 45 30 15 20 80 90 90 20 80 15 20 90 15 80

Merge Sort Sorting 50 60 45 30 90 20 80 15 15 20 30 45 50 60 80 90 30 45 50 60 50 60 45 30 90 20 85 15 15 20 80 90

Analysis of Merge Sort Sorting Each backward step requires a movement of n elements from smaller-size arrays to larger arrays; the effort is O(n). The number of steps which require merging is log n because each recursive call splits the array in half. The total effort to reconstruct the sorted array through merging is O(n log n). Going down through the recursion chain, sorting the left tables, a sequence of right table is allocation of size: Since a total of n additional storage locations are required.

Code for Merge Sort Sorting #ifndef MERGE_SORT_H_ #define MERGE_SORT_H_ /** Sort data in the specified range using merge sort. @param RI An iterator that meets the random-access iterator requirements @param first A random access iterator that references the first element in the sequence to be sorted @param last A random-access iterator that references 1 past the end of sequence */ template<typename RI> void merge_sort(RI first, RI last) { if (last - first > 1) // Split table into two new half tables. typedef typename std::iterator_traits<RI>::value_type value_type; RI middle = first + (last - first) / 2; std::vector<value_type> left_table(first, middle); std::vector<value_type> right_table(middle, last); // Sort the halves. merge_sort(left_table.begin(), left_table.end()); merge_sort(right_table.begin(), right_table.end()); // Merge the halves back into the original table. merge(left_table.begin(), left_table.end(), right_table.begin(), right_table.end(), first); } #endif //

Sort the following integer array in descending order using merge sort Sort the following integer array in descending order using merge sort. Show each step. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 35 80 75 60 90 70 55 85 34 45 62 57 65