CompSci 105. Sorting Algorithms Part 2

Similar documents
CompSci 105 Lecture 36 Revision Week 9 12 NOTE: All exam information without guarantee and subject to change. Exam Technique. Why MCQ?

CompSci 105 Lecture 36 Revision Week NOTE: All exam information without guarantee and subject to change.

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Sorting Pearson Education, Inc. All rights reserved.

COMPUTE THE GCD OF TWO NUMBERS. Date: Aim: To compute the GCD of two numbers

CS1 Lecture 30 Apr. 2, 2018

UNIT 5C Merge Sort. Course Announcements

Quick Sort. CSE Data Structures May 15, 2002

DATA STRUCTURES AND ALGORITHMS

Advanced Sorting. Merge Sort Quick Sort Radix Sort

Sorting and Searching

Introduction to Computers and Programming. Today

We can use a max-heap to sort data.

Sorting Algorithms. + Analysis of the Sorting Algorithms

Teach A level Compu/ng: Algorithms and Data Structures

DATA STRUCTURE AND ALGORITHM USING PYTHON

Divide-and-Conquer. Dr. Yingwu Zhu

UNIT 5C Merge Sort Principles of Computing, Carnegie Mellon University

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

CS240 Fall Mike Lam, Professor. Quick Sort

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

CS102 Sorting - Part 2

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

Sorting is a problem for which we can prove a non-trivial lower bound.

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Cpt S 122 Data Structures. Sorting

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

COSC242 Lecture 7 Mergesort and Quicksort

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

Sorting Algorithms Day 2 4/5/17

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

Parallel Sorting Algorithms

Visualizing Data Structures. Dan Petrisko

Search and Sorting Algorithms

Divide and Conquer 4-0

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Chapter 17: Sorting Algorithms. 1.1 Description Performance Example: Bubble Sort Bubble Sort Algorithm 5

Sorting Algorithms. Slides used during lecture of 8/11/2013 (D. Roose) Adapted from slides by

Assignment 4: Question 1b omitted. Assignment 5: Question 1b clarification

CS 171: Introduction to Computer Science II. Quicksort

Searching and Sorting

Divide and Conquer. Algorithm Fall Semester

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

CompSci 101 Introduction to Computer Science

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Week - 04 Lecture - 02 Merge Sort, Analysis

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Lecture 6: Divide-and-Conquer

Dhanalakshmi College of Engineering Manimangalam, Tambaram, Chennai

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

SORTING AND SELECTION

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Sorting and Selection

UNIT 7. SEARCH, SORT AND MERGE

CSE 115. Introduction to Computer Science I

Programming II (CS300)

CSC Design and Analysis of Algorithms

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Faster Sorting Methods

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

CP222 Computer Science II. Searching and Sorting

CSC236H Lecture 5. October 17, 2018

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL

Overview of Sorting Algorithms

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Data Structure Lecture#17: Internal Sorting 2 (Chapter 7) U Kang Seoul National University

Lecture Notes on Quicksort

CS2351 Data Structures. Lecture 1: Getting Started

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

CmpSci 187: Programming with Data Structures Spring 2015

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

Quicksort (Weiss chapter 8.6)

Mergesort again. 1. Split the list into two equal parts

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

CMSC201 Computer Science I for Majors

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

Sorting. Task Description. Selection Sort. Should we worry about speed?

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

Information Coding / Computer Graphics, ISY, LiTH

Lecture Notes on Quicksort

Searching in General

7 Sorting Algorithms. 7.1 O n 2 sorting algorithms. 7.2 Shell sort. Reading: MAW 7.1 and 7.2. Insertion sort: Worst-case time: O n 2.

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

100 points total. CSE 3353 Homework 2 Spring 2013

Sorting. Weiss chapter , 8.6

Copyright 2009, Artur Czumaj 1

Transcription:

Shell Sort another n 2 (or better!) sorting algorithm Merge Sort an n log(n) sorting algorithm Textbook: Chapter 5 CompSci 105 Sorting Algorithms Part 2 Note: we do not study quicksort in CompSci 105 Remember: Insertion sort has fewer comparisons than selection sort Selection sort has fewer moves swaps than insertion sort => IDEA: compare/shift non neighbouring elements Shell sort On average shell sort has fewer comparisons than selection sort and bubble sort and fewer moves than insertion sort Shell sort is based on the insertion sort algorithm, BUT: It instead of shifting elements many times by one step, it makes larger moves 2 Divide the list into lots of small lists, e.g., for the following list, say the gap (increment) used is 3 0 1 2 3 4 5 6 [3, 53, 38, 77, 22, 19, 38, 53, 22, 77, 47 ] Insertion Sort 3, 19, 47 Insertion Sort 53, 38, 38 53 Insertion Sort 22, 77, 77 22 ONE ITERATION (with 3 sublists) Then repeat sorting with reduced gap (=> fewer, but larger sublists) until gap is 1. Sublist 1 Sublist 2 Sublist 3 NOTE: The normal insertion sort algorithm uses a gap of 1, but in this algorithm sorting with gap 1 very efficient because list almost sorted due to previous steps. 3 Example from book (page 182). Start with a gap of 3. The results after the first pass are: 2 shift 1 shift 3 shift 4

Now use a gap of 1 (i.e., ordinary insertion sort): 5 Shell Sort algorithm Choose a gap size, do an insertion sort on all the sublists using this chosen gap size (this is a total of one pass of the collection), repeat using smaller gap sizes until finally the gap size is one. In practice, it turns out that only occasionally there are small values on the right hand side. Therefore the final insertion sort needs to move few elements. 6 Note: After previous step list is almost sorted => only four moves required for this final step A default option for gap sizes is 2 k 1, i.e. [, 31, 15, 7, 3,1] Research in the optimal gap sequence is ongoing A often quoted empirical derived gap sequence is [701, 301, 132, 57, 23, 10, 4, 1] Shell Sort Exercise Start with a gap size of half the length of the list, halve the gap size after each pass (text book implementation). Show the elements at the end of each pass. 0 1 2 3 4 5 6 7 8 54 26 93 17 77 31 44 55 20 List to sort PASS 1 PASS 2 PASS 3 7 Shell Sort Code def shell_sort(a_list): NOTE: We use this gap sequence because it is gap = len(a_list) // 2 simple and used in the text book. However, it is a poor choice (see slide 10) while gap > 0: for start_position in range(gap): gap_insertion_sort(a_list, start_position, gap) #print("for gap: ", gap, " - ", a_list) gap = gap // 2 def gap_insertion_sort(a_list, start, gap): for i in range(start + gap, len(a_list), gap): current_value = a_list[i] position = i while position >= gap and a_list[position - gap] > current_value: a_list[position] = a_list[position - gap] position = position - gap a_list[position] = current_value 8

9 10 Shell Sort Code (continued) Shell Sort Big O def main(): a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20] print("before: ", a_list) shell_sort(a_list) print("after: ", a_list) main() This is an improvement on all the previous sorting algorithms. The Big O for Shell Sort depends on the gap sequence and input values in general between O(n) and O(n 2 ) Gap sequence n/2, n/4,, 1 => worst case O(n 2 ) Gap sequence 2 k 1 (, 31, 15, 7, 3, 1) => worst case O(n 1.5 ) before: [54, 26, 93, 17, 77, 31, 44, 55, 20] for gap: 4 - [20, 26, 44, 17, 54, 31, 93, 55, 77] for gap: 2 - [20, 17, 44, 26, 54, 31, 77, 55, 93] for gap: 1 - [17, 20, 26, 31, 44, 54, 55, 77, 93] after: [17, 20, 26, 31, 44, 54, 55, 77, 93] Gap sequence, 109, 41, 19, 5, 1 => worst case O(n 1.333 ) Merge Sort 11 Merge Sort Below is the call tree for the merge sort algorithm: 12 This is a divide and conquer algorithm. Cut the list in half Sort each half Merge the two sorted halves You have already seen the divide and conquer algorithm using binary search on a sorted collection of items.

Merge Sort Below is the tree of the merged parts returned (the pink parts) by the merge sort algorithm: 17 26 54 93 17 20 26 31 44 54 55 77 93 20 31 44 55 77 13 Splitting lists def splitting_list_example(): list = [54, 26, 93, 17, 20] listl = list[:2] # elements 0 to 1 listr = list[2:] # elements 2 to end of list print(list, listl, listr) def main(): splitting_list_example() 14 26 54 17 93 31 77 20 44 55 main() 54 26 93 1 77 31 44 20 55 55 20 [54, 26, 93, 17, 20] [54, 26] [93, 17, 20] Slicing will be useful when halving the list in the merge sort code. Merging the two halves of the list 15 Merging the two halves of the list 16 def merge(a_list, left_half, right_half): i = j = k = 0 same as i = 0 j = 0 while i < len(left_half) and j < len(right_half): k = 0 if left_half[i] < right_half[j]: # CONTINUED a_list[k] = left_half[i] while i < len(left_half): i = i + 1 a_list[k] = left_half[i] else: i = i + 1 a_list[k] = right_half[j] j = j + 1 # CONTINUED ON RIGHT while j < len(right_half): a_list[k] = right_half[j] j = j + 1 a = [0, 0, 0, 0, 0, 0, 0, 0] merge (a, [1, 2, 5], [3, 4, 6, 8, 10]) print(a) [1, 2, 5] [3, 4, 6, 8, 10] [1, 2 2, 3 3, 4 4, 5, 6, 8, 10] [1, 2, 3, 4, 5, 6, 8, 10]

Merge sort Code 17 Merge Sort Big O 18 def merge_sort(a_list): if len(a_list) > 1: mid = len(a_list) // 2 left_half = a_list[:mid] right_half = a_list[mid:] The time for sorting a list of size 1 is constant, i.e. T(1)=1 The time for sorting a list of size n is the time of sorting the two halves plus the time for merging, i.e. T(n)=2*T(n/2)+n merge_sort(left_half) merge_sort(right_half) merge(a_list, left_half, right_half) Uses the function on slide 15 to merge the two halves. a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20] print("before: ", a_list) merge_sort(a_list) before: [54, 26, 93, 17, 77, 31, 44, 55, 20] print("after: ", a_list) after: [17, 20, 26, 31, 44, 54, 55, 77, 93] Can proof: T(n) = n + n log n => Big O is O(n log(n)) But there is a penalty of having to use extra space for the two halves of a split list Recursive call tree for a list of size 128 (2 7 ) MS(128) 19 Summary 20 MS(64) MS(64) Best Worst Average Extra Memory MS(16) MS(16) MS(16) MS(16) MS(16) MS(16) MS(16) MS(16) Bubble Sort (lecture) O(n^2) O(n^2) O(n^2) O(1) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) MS(8) Bubble Sort (optimised) O(n) O(n^2) O(n^2) O(1) Selection Sort O(n^2) O(n^2) O(n^2) O(1) Insertion Sort O(n) O(n^2) O(n^2) O(1) MS(1) MS(1) MS(1) MS(1) MS(1) 64 calls to MS(1). 128 calls to MS(1) Shell Sort (best gap sequence) O(n) O(n (log n)^2) O(n (log n)^2) O(1) Merge Sort O(n log n) O(n log n) O(n log n) O(n) Tim Sort (used in Python, hybrid of Merge Sort and Insertion Sort) O(n) O(n log n) O(n log n) O(n) Note: A comparison based sorting algorithm can NOT be better than O(n log n) in the average and worst case