Sorting Algorithm

26
Welcome to our presentation Sorting 1

Transcript of Sorting Algorithm

Page 1: Sorting Algorithm

1

Welcome to our presentation

Sorting

Page 2: Sorting Algorithm

2

Introduction• Sorting refers to arranging a set of data in some logical

order

• For ex. A telephone directory can be considered as a list where each record has three fields - name, address and phone number.

• Sorting is among the most basic problems in algorithm design.

Page 3: Sorting Algorithm

3

Things to remember• Sorting can be performed in many ways.

• Over a time several methods (or algorithms) are being developed to sort data(s). • Counting Sort , Quick sort, Merge sort, Insertion sort, are the

few sorting techniques discussed in this presentation.

• It is very difficult to select a sorting algorithm over another. And there is no sorting algorithm better than all others in all circumstances.

Page 4: Sorting Algorithm

4

Insertion Sort History• It's only a very simple algorithm but it's still hard to find

the original developer.

• The idea seems to be from 1959 or earlier

Page 5: Sorting Algorithm

5

Insertion Sort• Like sorting a hand of playing cards start with an empty

hand and the cards facing down the table.

• Pick one card at a time from the table, and insert it into the correct position in the left hand.

• Compare it with each of the cards already in the hand, from right to left

• The cards held in the left hand are sorted.

Page 6: Sorting Algorithm

6

Page 7: Sorting Algorithm

7

Time Complexity• Best Case:

• If the array is all but sorted then• So Time complexity= O(n)

• Average Case: Time complexity=O(n2)

• Worst Case:

• Array element in reverse sorted order• Time complexity=O(n2)

• Space Complexity• Since no extra space beside n variables is needed for sorting so• Space Complexity = O(n)

Page 8: Sorting Algorithm

8

Divide and conquer algorithms• The sorting algorithms we’ve seen so far have worst-

case running times of O(n2)

• When the size of the input array is large, these algorithms can take a long time to run.

• Now we will discuss two sorting algorithms whose running times are better• Merge Sort• Quick Sort

Page 9: Sorting Algorithm

9

Divide-and-conquer• Divide-and-conquer, breaks a problem into sub

problems that are similar to the original problem, recursively solves the sub problems, and finally combines the solutions to the sub problems to solve the original problem.

Page 10: Sorting Algorithm

10

Divide-and-conquer

Page 11: Sorting Algorithm

11

History Of Merge Sort• Merge sort is a divide and conquer algorithm that was

invented by John von Neumann in 1945

Page 12: Sorting Algorithm

12

Page 13: Sorting Algorithm

13

Time Complexity• Best Case, Worst Case, Average Case :

Time complexity= O(n log(n))

• Space Complexity• Space Complexity = O(n)

Page 14: Sorting Algorithm

14

History of quick sort• Developed by Tony Hoare in 1959

Page 15: Sorting Algorithm

15

Quick Sort• Quick sort is one of the most popular sorting

techniques.

• As the name suggests the quick sort is the fastest known sorting algorithm in practice.

• It has the best average time performance.

Page 16: Sorting Algorithm

16

Algorithm• Choosing a pivot• To partition the list we first choose a pivot element

• Partitioning• Then we partition the elements so that all those with values

less than pivot are placed on the left side and the higher vale on the right• Check if the current element is less than the pivot.

• If lesser replace it with the current element and move the wall up one position

• else move the pivot element to current element and vice versa• Recur• Repeat the same partitioning step unless all elements are

sorted

Page 17: Sorting Algorithm

17

Condition:

If A[n]>Pivot-

j move forward, i still

If A[n]<Pivot –

i move forward value exchange i & j j move forward

Page 18: Sorting Algorithm

18

Analysis of Quick Sort• Best & Average case• The best case analysis assumes that the pivot is always in the

middle• T(N)= O(nlogn)

• Worst case• When we pick minimum or maximum as pivot then we have to

go through each and every element so• T(N) = O(n2)Space Complexity- O(log(n))

Page 19: Sorting Algorithm

19

History of counting sort invented/discovered by Harold Seward

Page 20: Sorting Algorithm

20

Counting SortA =

Count=

Count’=

Result=

4 1 3 4 3

1 0 2 2

1 1 3 5

1 3 3 4 4

Page 21: Sorting Algorithm

21

Time Complexity• Best Case, Worst Case, Average Case :

Time complexity= O(n+k)

• Space Complexity• Space Complexity = O(k)

Page 22: Sorting Algorithm

22

Sort Algorithm Performance (2GHz PC OS- Windows XP)

50 Elements 479 Elements 1000 Elements

Insertion sort 2ms 168ms 379sec

Quick Sort 1.5ms 23ms 2.1sec

Merge Sort 1.9ms 24ms 4.0sec

Counting Sort 0.9ms 2ms 0.042ms

Page 23: Sorting Algorithm

23

Performance On Sorted Array100 Elements 1000 Elements 100000 Elements

Insertion sort 0.003ms 0.004ms 0.004ms

Quick Sort .12ms 3.73ms 29.94sec

Merge Sort 0.012ms .23ms 20.35ms

Counting Sort 0.009ms .01ms 0.04ms

Page 24: Sorting Algorithm

24

Performance On Reverse Sorted Array

100 Elements 1000 Elements 100000 Elements

Insertion sort 0.049ms 3.17ms 25.79sec

Quick Sort 0.060ms 5.94ms 44sec

Merge Sort 0.017ms 0.23ms 21.60ms

Counting Sort 0.015ms 0.02ms 0.042ms

Page 25: Sorting Algorithm

25

Performance On Randomly Sorted Array

100 Elements 1000 Elements 100000 Elements

Insertion sort 0.015ms 1.73ms 12.32sec

Quick Sort 0.011ms 0.16ms 20.01ms

Merge Sort 0.016ms 0.22ms 29.4ms

Counting Sort 0.9ms 0.16ms 0.042ms

Page 26: Sorting Algorithm

26