View
152
Download
1
Category
Preview:
Citation preview
1
Welcome to our presentation
Sorting
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.
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.
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
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.
6
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)
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
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.
10
Divide-and-conquer
11
History Of Merge Sort• Merge sort is a divide and conquer algorithm that was
invented by John von Neumann in 1945
12
13
Time Complexity• Best Case, Worst Case, Average Case :
Time complexity= O(n log(n))
• Space Complexity• Space Complexity = O(n)
14
History of quick sort• Developed by Tony Hoare in 1959
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.
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
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
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))
19
History of counting sort invented/discovered by Harold Seward
20
Counting SortA =
Count=
Count’=
Result=
4 1 3 4 3
1 0 2 2
1 1 3 5
1 3 3 4 4
21
Time Complexity• Best Case, Worst Case, Average Case :
Time complexity= O(n+k)
• Space Complexity• Space Complexity = O(k)
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
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
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
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
26
Recommended