+ All Categories
Home > Education > Daa final

Daa final

Date post: 16-Jul-2015
Category:
Upload: gagan019
View: 8 times
Download: 2 times
Share this document with a friend
30
By:- Gagan.R.Popale-2BV13CS034 Ankit Gaonkar -2BV13CS014 Anung Tajo-2BV13CS015 Alan S Malekar-2BV13CS010 Sorting
Transcript
Page 1: Daa final

• By:-

• Gagan.R.Popale-2BV13CS034

• Ankit Gaonkar -2BV13CS014

• Anung Tajo-2BV13CS015

• Alan S Malekar-2BV13CS010

Sorting

Page 2: Daa final

WHAT IS SORTING?

Sorting is the process of putting a list or a group of items in a specific order . Some common sorting criteria are: alphabetical or numerical.

Ex:- Merge sort, Quick sort etc

Page 3: Daa final

Merge sort and Quick sort use : Divide And Conqure Technique

3

Divide the problem into a number of sub-problems

◦ Similar sub-problems of smaller size

Conquer the sub-problems

◦ Solve the sub-problems recursively

◦ Sub-problem size small enough solve the problems in

straightforward manner

Combine the solutions of the sub-problems

◦ Obtain the solution for the original problem

Page 4: Daa final

Merge Sort

Page 5: Daa final

In the Beginning…

Invented by

John von Neumann (1903-1957)

Follows divide and conquer paradigm.

Developed merge sort for EDVAC in 1945

Page 6: Daa final

Merging

The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1x2

…xm) and Y(y1y2

…yn) the resulting list is Z(z1z2

…zm+n)

Example:

L1 = { 3 8 9 } L2 = { 1 5 7 }

merge(L1, L2) = { 1 3 5 7 8 9 }

Page 7: Daa final

Divide And Conquer

1.Divide: Divide the unsorted list into two sub lists of about half the size.

2.Conquer: Sort each of the two sub lists recursively until we have list sizes of length 1,in which case the list itself is returned.

3.Combine: Merge the two-sorted sub lists back into one sorted list.

Page 8: Daa final

Merge sort algorithm

Merge-Sort (A, n)

if n=1 return

else

n1 ← n2 ← n/2

create array L[n1], R[n2]

for i ← 0 to n1-1 do L[i] ← A[i]

for j ← 0 to n2-1 do R[j] ← A[n1+j]

Merge-Sort(L, n1)

Merge-Sort(R, n2)

Merge(A, L, n1, R, n2 )

Page 9: Daa final

Merge Sort Example

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

Page 10: Daa final

Merge Sort Example

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

Page 11: Daa final

Merge Sort Example

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

99 6 86 15 58 35 86 4 0

Page 12: Daa final

Merge Sort Example

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

99 6 86 15 58 35 86 4 0

4 0

Page 13: Daa final

Merge Sort Example

99 6 86 15 58 35 86 4 0

Page 14: Daa final

Merge Sort Example

99 6 86 15 58 35 86 0 4

4 0Merge

Page 15: Daa final

Merge Sort Example

15 866 99 58 35 0 4 86

99 6 86 15 58 35 86 0 4

Merge

Page 16: Daa final

Merge Sort Example

6 15 86 99 0 4 35 58 86

15 866 99 58 35 0 4 86

Merge

Page 17: Daa final

Merge Sort Example

0 4 6 15 35 58 86 86 99

6 15 86 99 0 4 35 58 86

Merge

Page 18: Daa final

Merge Sort Example

0 4 6 15 35 58 86 86 99

Page 19: Daa final

Implementing Merge Sort

There are two basic ways to implement merge sort:

In Place: Merging is done with only the input array

Double Storage: Merging is done with a temporary array of the same size as the input array.

Page 20: Daa final

20

Merge-Sort Analysis

• Time, merging

log n levels

• Total running time: order of nlogn• Total Space: order of n

Total time for merging: cn log n

n

n/2 n/2

n/4 n/4 n/4 n/4

Page 21: Daa final

Additional

Merge sort’s merge operation is useful in online sorting, where the list to be sorted is received a piece at a time,instead of all at the beginning..

In this We sort each new piece that is received using any sorting algorithm, and then merge it into our sorted list so far using the merge operation.

Page 22: Daa final

Finally

Best Case, Average Case, and Worst Case = O(N logN)

• Storage Requirement:

Double that needed to hold the array to be sorted.

Page 23: Daa final
Page 24: Daa final
Page 25: Daa final
Page 26: Daa final
Page 27: Daa final
Page 28: Daa final
Page 29: Daa final
Page 30: Daa final

Recommended