Lab3003
  • FacultyAlgorithmChallenge
  • README
  • Question Bank
  • Python Documentation
    • BetterMap
    • Board
    • Card
    • Circle
    • Deck
    • README.md
    • Guide For Bookmarklet
    • Hand
    • HashMap
    • Hist
    • Kangaroo
    • LinearMap
    • Markov
    • Point
    • PokerDeck
    • PokerHand
    • Rectangle
    • State
    • Test
    • ThinkPython
    • ThinkPython2
    • Time
    • Think Python
    • thinkpython
  • Files
  • Image Files
  • lab
    • InsertSortTDD
    • countingBits
    • cs3003mergeSortLectureGuideLine
    • index_min
    • insort
    • One-line factorial function in Python
  • Manual for Python Lab
    • COURSE OBJECTIVES
    • 1_timelines
    • FDPNotes
    • PC-2_Python
    • lab1-kgashok.py
    • Lab 10 :Matrix Multiplication
    • Lab 11: Programs that take command line arguments (word count)
    • Lab 11: Programs that take command line arguments
    • Lab 12: Compute the most frequently used words from the text file
    • Lab 12: Find the most frequent words in a text read from a file
    • Lab 12a: File content sorter
    • Lab 2: Find the square root of a number (Newton’s method)
    • Lab 3: Compute power of a number (Exponentiation)
    • Lab 3: Exponentiation (power of a number)
    • lab4-binaraysearch-anvar.py
    • Solution for Binary Search
    • lab4-joelanandraj-binarysearch.py
    • Lab 4: Linear and Binary Search
    • lab4-lin-anvar.py
    • Linear Searcher
    • Lab 5 : find n prime numbers
    • lab6-kgashok.py
    • lab7-kgashok.py
    • lab8-kgashok.py
    • Lab 8: Selection and Insertion sort using Python
    • Merge Sort
    • Quick Sort
    • labSheet
    • pc0
    • sortPythonic
    • Sorting
  • misc
    • Bookmarklets
    • Guide For Bookmarklet
    • pythonResources
    • FDP for Python
    • Agenda for Workshop
      • Agenda
  • notes
    • Problem Set
    • InsertSortTDD
    • MergeSort
    • cs3003-unit1-notes
    • cs3003-unit3
    • cs3003-unit4-examples
    • Unit 4 Lists, Tuples and Dictionaries
    • cs3003insertsortLectureGuide
    • cs3003mergeSortLectureGuideLine
    • Designing and Delivering Effective Lectures
    • histogram.py
    • selectSortLectureGuide
  • Sandbox to try ThinkPy2 code
    • Code from ThinkPython Github repository
  • Important Topics
    • 3003-syllabus
    • GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY L T P C
    • Unit II Version 2
    • Unit IV material
    • UNIT III CONTROL FLOW, FUNCTIONS
    • Unit 1 Algorithm Problem Solving
    • Unit_V_Notes
    • UpdatedSyllabus
    • glossary
    • glossaryGeneration.py
    • importantTopics-kgashok
    • memCSV
    • Tower of Hanoi
    • Notes for Unit 2 - Illustrative programs
    • unit5-updated_notes
    • unit_1_notes
    • Unit 3 Control Flow Version 1
    • unit 3
      • UNIT III CONTROL FLOW, FUNCTIONS
    • unit_i_img
Powered by GitBook
On this page
  • Merge Sort
  • Context setting
  • Agenda
  • Pre-req quiz
  • Interactive Coding
  • Stage - 1
  • Stage 2
  • Merge Sort Description
  • Algorithm
  • Source code
  • Part 2
  • Summary
  • Last 2 Minute
  • Program File for Demo
  • Solve the Million Element Merge Sort
  1. notes

cs3003mergeSortLectureGuideLine

Merge Sort

Lecture Guidelines

Context setting

Unit 4 -> Lists, Tuples and Dictionaries -> Illustrative Programs

  • It is very important to use a data type in a real-world case, to understand its true value and benefit.

  • We are covering three types of sorts

    • Comparison Algorithms - Insertion Sort and Selection Sort

    • Divide and Conquer Algorithms - Merge Sort, Quick Sort

Agenda

  • Over the next 20 minutes, we shall see how lists can be used to implement Merge Sort

Pre-req quiz

  • Are you ready?

    • Known to Unknown

  • Best practice of a engineering student is to come prepared for the class.

    • You must spend at least 2 hours reviewing the material taught in class, and preparing for the class

  • Quiz

    • 44+ MCQ in Online Socrative portal

      • Immediate feedback

    • Paper based - Show of hands? How many completed it? Attempted it?

  • http://j.mp/mergeListCC

Interactive Coding

Stage - 1

  • How to divide a sorted list into equal halves?

  • How to merge the list back into original form?


Stage 2

  • How to merge (combine) sorted list?

  • How to merge odd number list and even number list?

  • Use PythonAnywhere.com to zoom in and out of a mergesort verbose run

Notice that at each level we divide the array into two halves until we get bunch of single element arrays. This is the divide portion of the divide and conquer method. Then, we start merging and sorting the smaller arrays in a series of steps which is the conquer portion of divide and conquer.

Merge Sort Description

  • InsertSort and SelectionSort are examples of comparison sorts

  • However, MergeSort, is an example of the Divide-and-Conquer algorithm sorts

    • Divide means portioning the n-element array to be sorted into two sub-arrays of n/2 elements. If A is an array containing zero or one element, then it is already sorted. However, if there are more elements in the array, divide A into two sub-arrays, A1 and A2, each containing about half of the elements of A.

    • Conquer means sorting the two sub-arrays recursively using merge sort. Combine means merging the two sorted sub-arrays of size n/2 to produce the sorted array of n elements.

Algorithm

  • In mergesort, a divide-and-conquer partitioning algorithm (which more often requires extra memory), the input array is divided in two halves, calls itself recursively for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves.

The basic steps in the recursive MergeSort Algorithm is as follows:

Func MERGESORT:
    input (unsorted list of elements)  `ulist`
    output (sorted list of elements)   `slist`

- TERMINAL CASE:
	- if size of list is 0 or 1, return (since it is sorted)
- Split the unsorted list (`ulist`) into equal sublists: left sublist and right sublist
- Recursively call MergeSort on the left sublist (`leftA`)
- Recursively call MergeSort on the right sublist (`leftB`)
- Merge the sorted left and sorted right sublists to form one sorted list
- Return the sorted list (`slist`)

Source code

Visualize at http://tinyurl.com/visualMerge

# a helper function for the mergesort function
# function to merge already sorted lists
def merge(A, B):
  C = []
  while A and B:
    candidate = (A if A[0] < B[0] else B).pop(0)
    C.append(candidate)

  # pick up the residual elements in A or B
  return C + A + B

def mergesort(ulist):
  if len(ulist) <= 1:
    return ulist

  mid = len(ulist)//2

  leftA    = ulist[:mid]
  rightB   = ulist[mid:]

  sortedA  = mergesort(leftA)
  sortedB  = mergesort(rightB)

  slist = merge(sortedA, sortedB)
  return slist

# test cases
assert (merge([1, 2, 3], [4, 5, 10]) == [1, 2, 3, 4, 5, 10])
alist = [6, 2, 1, 5, 8, 7, 4, 3]
print(mergesort(alist))
# [1, 2, 3, 4, 5, 6, 7, 8]

Part 2

Break Activity for the 20th min which also helps students review what was covered until now.

  • Lead with the question: Can you improve on the above logic? How can you do it? Please discuss with your immediate neighbour.

    • Refer to the last question of the Pre-requisite Quiz

      • Ask your faculty for more clues

Summary

  1. We incrementally developed MergeSort in a "demo mode"

  2. We arrived at the pseudo code

  3. We wrote the equivalent code for MergeSort

The beauty of MergeSort is that regardless of the list and how badly it is sorted, O(n) is nlog(n) which is favourable among almost most sorting algorithms.

Last 2 Minute

  • respond to Questions from Students

    • review the relevant Question Paper snippets

    • Ask 1 or more students questions from Pre-requisites

    • Practice your MergeSort code on http://j.mp/mergeSortCC

  • Review Pre-requisites for Histogram (next class)

Program File for Demo

Modify this file as you deem fit to demonstrate on PythonAnywhere.com or on the mu editor

  • http://j.mp/mergePAW

  • This is the script I use in my sample video http://bit.ly/mergeVideo

Solve the Million Element Merge Sort

    • Use elzm6m as the session ID.

    • Use KG.id file to claim the Avatar that is assigned to you.

def test_a_million_mostly_sorted_elements():
  '''1_000_000 number of elements, mostly sorted'''
  alist = list(range(950_000))
  sublist = list(range(950_000, 1_000_000))
  random.shuffle(sublist)
  alist += sublist
  assert mergesort(alist, True) == list(range(1_000_000))
Previouscs3003insertsortLectureGuideNextDesigning and Delivering Effective Lectures

Last updated 3 years ago

merge

tl;dr

Use (yes, RASAM!) to get to the CyberDojo server.

http://ras.am
tldr