Merge and Quick Sort

28
CS126 ZV2005 Merge Sort 1 Zabin Visram Room CS115 Lecture Structure Summary of simple sorts References – Books & web sites Complex Sorts Merge Sort Quick Sort

description

Merge and Quick Sort

Transcript of Merge and Quick Sort

Page 1: Merge and Quick Sort

CS126 ZV2005 Merge Sort 1

Zabin Visram Room CS115

Lecture Structure Summary of simple sortsReferences – Books & web sitesComplex Sorts

Merge SortQuick Sort

Page 2: Merge and Quick Sort

CS126 ZV2005 Merge Sort 2

Summary of simple sorts

Bubble sort (or exchange sort) is generally not a good practical method of sorting.For random data in an array:

Selection sort requires fewer moves than insertion sort but a constant number of comparisons. Thus if the elements are large and costly to move but have small sort keys then selection sort is likely to give best performance.

Page 3: Merge and Quick Sort

CS126 ZV2005 Merge Sort 3

Summary of simple sortsIf the elements are easy to move - but have complex sort keys - additional comparisons involved in selection become significant, -then insertion is likely to be better.You can compare insertion sort and selection

sort and bubble sort by demonstrationhttp://www.cosc.canterbury.ac.nz/people/mu

kundan/dsal/appldsal.htmlIn a linked list the data are not moved while sorting and so insertion sort, which minimises the number of comparisons, is preferable.

Page 4: Merge and Quick Sort

CS126 ZV2005 Merge Sort 4

Merge SortBooks

Java Structures D.A. Bailey P115-121Data structures using Java D.S Malik , P.S. Nair P549-556Java collections D.A.Watt&D.F.Brown P53-56Data structures with Abstract Data Types & Pascal –Stubbs & Webre ISBN 0534092640 P2263-266

Web siteshttp://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/merge/mergen.htm

Page 5: Merge and Quick Sort

CS126 ZV2005 Merge Sort 5

Merge SortMergeSort is a sorting algorithm which produces a sorted sequence by sorting its two halves and merging them.MergeSort algorithm (Like QuickSort) is based on a divide and conquer strategy.First the sequence to be sorted is

decomposed into two halves (Divide). Each half is sorted independently (Conquer).Then the two sorted halves are merged to a sorted sequence (Combine)

Page 6: Merge and Quick Sort

CS126 ZV2005 Merge Sort 6

Divide & Conquer StrategyThe divide & conquer strategy for solving a problem consists of three steps1) Divide – the problem is decomposed into subproblems2) Conquer – the subproblems are solved3) Combine - the solutions of the subproblem are recombined to the solution of the original problem

Merge(n)

Divide Conquer Combine

Mergesort(n/2)

Mergesort(n/2)

Merge sort(n)

Page 7: Merge and Quick Sort

CS126 ZV2005 Merge Sort 7

Merge Sort – Divide & Conquer Algorithm

The divide & conquer technique is used to sort a list. The algorithm partitions the lists into two sublists,sorts the sublists, and then combines the sorted sublists into one sorted listAlgorithm can be used for linked lists (reference Malik & Nair book P549-557) and array based lists

Page 8: Merge and Quick Sort

CS126 ZV2005 Merge Sort 8

Merge Sort – overview

The simple sorts considered so far were all of complexity O(n2). consider the idea of subdividing the sorting of a long list into sorting shorter lists and then put these together in a way that keeps the combined list still sorted.The essential idea of merge sort is to make

repeated use of a method which merges two lists (each of which is already sorted into ascending order) into a third list in ascending order.

Page 9: Merge and Quick Sort

CS126 ZV2005 Merge Sort 9

3 5 7 9 0 2 4 6

3 5 7 9 0 2 4 6

02

3

3 5 7 9 0 2 4 6

New

Merged

array

Page 10: Merge and Quick Sort

CS126 ZV2005 Merge Sort 10

Brief overview of methodThe basic merge method compares the first items in the two (already sorted) lists and places the lower of the two in the new list.One of the original lists now has a new

lowest value and this is again compared with the lowest of the other list, and the lower of the two placed next in the new list. This process is repeated until one of the lists is exhausted.Then the remaining values from the other list

are placed in order in the new list and the merge is completed.It is a ‘divide and conquer’ algorithm & has a recursive version

Page 11: Merge and Quick Sort

CS126 ZV2005 Merge Sort 11

The merge sort chops the list into two sublistsof nearly equal sizeFor exampleList : 35 28 18 45 62 48 30 38

The merge sort algorithm partitions this list into two sublistsFirst sublist : 35 28 18 45Second sublist : 62 48 30 38

The two sublists are sorted using the same algorithm (i.e merge sort) First sublist : 18 28 35 45Second sublist : 30 38 48 62

Next the merge sort algorithm combines –that is merges – the two sorted sublists into one sorted list

Page 12: Merge and Quick Sort

CS126 ZV2005 Merge Sort 12

Merge Sort Algorithm -RECURSION 35 28 18 45 62 48 30 38

35 28 18 45

35 28 18 45

35 28 18 45

18 4528 35

18 28 35 45

18 28 30 35 38 45 48 62

62 48 30 38

62 48 30 38

30 3848 62

30 38 48 62

62 48 30 38

split

splitsplit

split split

split split

merge merge

merge merge

merge

merge

merge

If the list is of a size greater than 1

{1) Divide the list into

two sublists2) Merge sort the first

sublist3) Merge sort the second

sublist4) Merge the first sublist

and the second sublist

}Thus we are using

recursion to implement the merge sort algorithm

Page 13: Merge and Quick Sort

CS126 ZV2005 Merge Sort 13

Merge Sort – Array based

to sort an array – we can divide the array into two subarrays of equal lengthAnd finally merge the two subarrays

Page 14: Merge and Quick Sort

CS126 ZV2005 Merge Sort 14

To sort a[left..right]:1. If left < right

1.1 Let m be an integer about midway between left and right (such that left <= m <right)1.2 sort a[left ..m] sorts the left subarray call the merge-sort

algorithm recursively1.3 sort a[m+1 .. right] sorts the right subarray call the merge-sort

algorithm recursively1.4 Merge a[left...m] and a[m+1 .. right] into an auxiliary array b

merges the two subarrays into an auxillary array1.5 Copy all components of b into a[left..right] overwrites the

original array

Page 15: Merge and Quick Sort

CS126 ZV2005 Merge Sort 15

Suitable code for this merge algorithm is as follows (after Bailey):private static void merge(int data[], int temp[],int low,int middle, int high){// pre: data[middle..high] are ascending// temp[low..middle-1] are ascending// post: data[low..high] contains all //values in ascending order

int ri = low; // result indexint ti = low; // temp indexint di = middle; // destination index// while two lists are not empty merge

// smaller valuewhile (ti < middle && di <= high) {

if (data[di] < temp[ti]) data[ri++] = data[di++];

else data[ri++] = temp[ti++];

}// possibly values left in temp array

while (ti < middle) {data[ri++] = temp[ti++];

}// ...or possibly values left (in

// correct place) in data array}

Page 16: Merge and Quick Sort

CS126 ZV2005 Merge Sort 16

Another example or Merge Sort

split40 2 1 43 3 65 0 –1 58 3 42 4

40 2 1 43 3 65 0 –1 58 3 42 4

40 2 1 43 3 65 0 –1 58 3 42 4

40 2 1 43 3 65 0 –1 58 3 42 4

40 1 2 43 3 65 0 –1 58 3 4 42

1 2 40 3 43 65 –1 0 58 3 4 42

1 2 3 40 43 65 –1 0 3 4 42 58

-1 0 1 2 3 3 4 40 42 43 58 65

2 6531 -1 58 42 4

Values are recursively split into unsorted lists that are then recursively merged into ascending order

Merge

Concentrate on this –next slide

Page 17: Merge and Quick Sort

CS126 ZV2005 Merge Sort 17

–1 0 3 4 42 58

1 2 3 40 43 650 1 2 3 4 5 6 7 8 9 10 11 12 13

data(a)

tempdi 6 0 0ti ri

-1 0 1 2 3 3 4 42 58

40 43 650 1 2 3 4 5 6 7 8 9 10 11 12 13

data

(b) temp8 3 5di ti ri

–1 0 1 2 3 3 4 40 42 43 58

650 1 2 3 4 5 6 7 8 9 10 11 12 13

data(c)

temp12 5 11di ti ri

-1 0 1 2 3 3 4 40 42 43 58 65data0 1 2 3 4 5 6 7 8 9 10 11 12 13 (d)

temp12 6 12di ti ri

Merge of two six elements list (from last slide) –

(a) Initial location of data

(b) merge of several values

(c) The point at which list is emptied

(d) final result

Page 18: Merge and Quick Sort

CS126 ZV2005 Merge Sort 18

–1 0 3 4 42 58

1 2 3 40 43 65

data(a) 0 1 2 3 4 5 6 7 8 9 10 11 12 13

tempdi ri6 ti 0 0

data -1 0 1 2 3 3 4 42 58

40 43 650 1 2 3 4 5 6 7 8 9 10 11 12 13

(b) temp8 3 5ti ri

The data from the two lists are located in two arrays – lower half in temp and upper half in data (see (a) in previous slide)The first loop compares first element of each list – to determine – which should be copied over to the result list first (b)

Page 19: Merge and Quick Sort

CS126 ZV2005 Merge Sort 19

That loop continues until one list is emptied (c ) If data is the emptied list – then the remainder of temp list is transferred (d). If temp list was emptied, remainder of the data list is already located in correct placeThus merge sort recursively splits, sorts and reconstructs

–1 0 1 2 3 3 4 40 42 43 58

65

data0 1 2 3 4 5 6 7 8 9 10 11 12 13

temp12 5 11di ti ri

-1 0 1 2 3 3 4 40 42 43 58 65data

temp0 1 2 3 4 5 6 7 8 9 10 11 12 13

12 6 12

(c )

(d)

Page 20: Merge and Quick Sort

CS126 ZV2005 Merge Sort 20

Task in pairs :-

Trace the steps that a merge sort takes when sorting the following array into ascending order

9 6 2 4 8 7 5 3

Page 21: Merge and Quick Sort

CS126 ZV2005 Merge Sort 21

Answer9 6 2 4 8 7 5 3

9 6 2 4 8 7 5 39 6 2 4 8 7 5 3

9 6 2 4 8 7 5 36 9 2 4 7 8 3 52 4 6 9 3 5 7 8

2 3 4 5 6 7 8 9

Page 22: Merge and Quick Sort

CS126 ZV2005 Merge Sort 22

Performance

This merge method performs at most n-1 comparisons and so is of O(n).At most log)n) merges so sort is O(nlogn)How do we get started with this method since it requires the sublists to be already sorted? In an iterative version we could begin from lists which are of length one (so sorted) and merge these lists into sorted lists of length 2, and so on until we had sorted the list required.

Page 23: Merge and Quick Sort

CS126 ZV2005 Merge Sort 23

Additional Storage

To do this it would require merging into an array in a different location from the original data. It is a feature of any form of the mergesortalgorithm that it requires additional storage – roughly an amount twice the size of the array to be sorted. The recursive process can be coded as follows:

Page 24: Merge and Quick Sort

CS126 ZV2005 Merge Sort 24

private static void mergeSortRecursive(int data[],

int temp[], int low, int high) {

// pre: 0 <= low <= high < data.length// post: values in data[low..high] are

//in ascending order

int n = high-low+1;int middle = low + n/2;int i;

if (n < 2) return;// move lower half of data into

// temporary storage

for (i = low; i < middle; i++)

temp[i] = data[i];

// sort lower half of array mergeSortRecursive(temp,data,low,middle-1);// sort upper half of array

mergeSortRecursive(data,temp,middle,high);// merge halves togethermerge(data,temp,low,middle,high);

}

Page 25: Merge and Quick Sort

CS126 ZV2005 Merge Sort 25

The above method recursively splits, sorts, and reconstructs (through merging) the original list. It makes use of a temporary array for each merge operation. This additional array can be dynamically created as required. So the overall ‘wrapper’ program that allocates the storage and calls the recursive sort can be written:

Page 26: Merge and Quick Sort

CS126 ZV2005 Merge Sort 26

public static void mergeSort(intdata[],int n) {// pre: 0 <= n <= data.length// post: values in data[0..n-1] are in// ascending order

mergeSortRecursive(data,newint[n],0,n-1); }

Page 27: Merge and Quick Sort

CS126 ZV2005 Merge Sort 27

The iterative and recursive processes corresponding to the mergesort algorithm are very different. Some explanation and account of these is given in lectures. The depth of splitting is clearly log2n and so the overall complexity of mergesort is O(nlogn).

Page 28: Merge and Quick Sort

CS126 ZV2005 Merge Sort 28

Demonstration

http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/MSort.html