sorting algorithms

22
Sorting Algorithms Trupti Agrawal 1

Upload: trupti-agrawal

Post on 02-Jul-2015

1.272 views

Category:

Education


0 download

DESCRIPTION

Sorting Algorithm and its types

TRANSCRIPT

Page 1: Sorting algorithms

Sorting Algorithms

Trupti Agrawal 1

Page 2: Sorting algorithms

• A sorting algorithm is an algorithm that puts elements of a list in a certain order.

• Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists

Sorting Algorithms

Trupti Agrawal 2

Page 3: Sorting algorithms

Why Sorting? “When in doubt, sort” – one of the principles of

algorithm design. Sorting used as a subroutine in many of the algorithms:

Searching in databases: we can do binary search on sorted data

A large number of computer graphics and computational geometry problems

Closest pair, element uniqueness, frequency distribution

3Trupti Agrawal

Page 4: Sorting algorithms

Why Sorting? (2) A large number of algorithms developed representing

different algorithm design techniques.

A lower bound for sorting W(n log n) is used to prove lower bounds of other problems

4Trupti Agrawal

Page 5: Sorting algorithms

Sorting Algorithms so far Insertion sort, selection sort, bubble sort

Worst-case running time Q(n2); in-place

Merge sort

Worst-case running time Q(n log n); but requires additional memory

5Trupti Agrawal

Page 6: Sorting algorithms

Insertion sort

• Card players all know how to sort …– First card is already sorted

– With all the rest,

Scan back from the end until you find the first card larger than the new one,

Move all the lower ones up one slot

insert it

Q

2

9

A

K

10

J

2

2

9

Trupti Agrawal 6

Page 7: Sorting algorithms

To insert 12, we need to make room for it by moving first 36 and then 24.

Insertion Sort

Trupti Agrawal 7

Page 8: Sorting algorithms

8

Insertion Sort

Trupti Agrawal

Page 9: Sorting algorithms

Insertion Sort

Trupti Agrawal 9

Page 10: Sorting algorithms

Insertion Sort

5 2 4 6 1 3

input array

left sub-array right sub-array

at each iteration, the array is divided in two sub-arrays:

sorted unsorted

Trupti Agrawal 10

Page 11: Sorting algorithms

Insertion Sort

Trupti Agrawal 11

Page 12: Sorting algorithms

Insertion Sort - Summary

• Advantages

– Good running time for “almost sorted” arrays Q(n)

• Disadvantages

– Q(n2) running time in worst and average case

– n2/2 comparisons and exchanges

Trupti Agrawal 12

Page 13: Sorting algorithms

Selection Sort

• Idea:

– Find the smallest element in the array

– Exchange it with the element in the first position

– Find the second smallest element and exchange it with the element in the second position

– Continue until the array is sorted

• Disadvantage:

– Running time depends only slightly on the amount of order in the file

Trupti Agrawal 13

Page 14: Sorting algorithms

Example-Selection Sort

1329648

8329641

8349621

8649321

8964321

8694321

9864321

9864321

Trupti Agrawal 14

Page 15: Sorting algorithms

Bubble Sort

• Idea:– Repeatedly pass through the array

– Swaps adjacent elements that are out of order

• Easier to implement, but slower than Insertion sort

1 2 3 n

i

1329648

j

Trupti Agrawal 15

Page 16: Sorting algorithms

Example - Bubble Sort

Trupti Agrawal 16

Page 17: Sorting algorithms

Quicksort Efficient sorting algorithm

Discovered by C.A.R. Hoare

Example of Divide and Conquer algorithm

Two phases

Partition phase

Divides the work into half

Sort phase

Conquers the halves!

Trupti Agrawal 17

Page 18: Sorting algorithms

Quicksort Partition / Divide

Choose a pivot

Find the position for the pivot so that

all elements to the left are less

all elements to the right are greater

< pivot > pivotpivot

Trupti Agrawal 18

Page 19: Sorting algorithms

Quicksort Conquer

Apply the same algorithm to each half< pivot > pivot

pivot< p’ p’ > p’ < p” p” > p”

Trupti Agrawal 19

Page 20: Sorting algorithms

Quicksort - Example

Trupti Agrawal 20

Page 21: Sorting algorithms

Review of Algorithms Selection Sort

An algorithm which orders items by repeatedly looking through remaining items to find the least one and moving it to a final location

Bubble Sort

Sort by comparing each adjacent pair of items in a list in turn, swapping the items if necessary, and repeating the pass through the list until no swaps are done

Insertion Sort

Sort by repeatedly taking the next item and inserting it into the final data structure in its proper order with respect to items already inserted.

Merge Sort

An algorithm which splits the items to be sorted into two groups, recursively sorts each group, and merges them into a final, sorted sequence

Quick Sort

An in-place sort algorithm that uses the divide and conquer paradigm. It picks an element from the array (the pivot), partitions the remaining elements into those greater than and less than this pivot, and recursively sorts the partitions.

Trupti Agrawal 21

Page 22: Sorting algorithms

THANK YOU….. !!!

Trupti Agrawal 22