Upcoming SlideShare
×

# Ds 6

554
-1

Published on

Published in: Technology, Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
554
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
0
0
Likes
1
Embeds 0
No embeds

No notes for slide
• Lower Bound and Upper Bound to denote the first element number and the last element number respectively
• Lower Bound and Upper Bound to denote the first element number and the last element number respectively
• ### Ds 6

1. 1. Introduction to Sorting TechniquesObjectivesIn this lesson, you will learn to: State the different sorting techniques State the complexities in the different sorting algorithms Introduction to Sorting Techniques/Lesson 6/Slide 1 of 40 ©NIIT
2. 2. Introduction to Sorting Techniques Sorting Sorting and searching are fundamental operations performed on data Sorting refers to operation of arranging data in given order Searching refers to finding a location of given item Algorithm chosen depends on operation to be performed on data Introduction to Sorting Techniques/Lesson 6/Slide 2 of 40 ©NIIT
3. 3. Introduction to Sorting Techniques Sorting (Contd..) Some sorting algorithms in use are: Bubble sort Selection sort Insertion sort Merge sort Quick sort Introduction to Sorting Techniques/Lesson 6/Slide 3 of 40 ©NIIT
4. 4. Introduction to Sorting Techniques Bubble Sort Take a list of numbers consisting of n elements This algorithm requires to traverse the list n-1 times Traversing the entire list once is called a pass For a list of n numbers there will be n-1 passes Introduction to Sorting Techniques/Lesson 6/Slide 4 of 40 ©NIIT
5. 5. Introduction to Sorting Techniques Bubble Sort (Contd..) Step 1: Compare first two elements and arrange them Compare second and third element and arrange them Compare fourth and fifth and arrange them Compare n-1 with nth element and arrange them Step 2: Repeat step 1 with one less comparison Introduction to Sorting Techniques/Lesson 6/Slide 5 of 40 ©NIIT
6. 6. Introduction to Sorting Techniques Bubble Sort (Contd..) Step 3: Repeat step 1 with two less comparison Step N: This step will have only one comparison Example: Sort 5, 2, 6, 7, 3 using bubble sort Introduction to Sorting Techniques/Lesson 6/Slide 6 of 40 ©NIIT
7. 7. Introduction to Sorting Techniques Bubble Sort (Contd..) Pass 1: Compare first and second element. If X1 X2 interchange as 2, 5, 6, 7, 3 Compare X2 with X3. If X2 X3 interchange else the list remains as it is Compare X3 with X4. Since X3 X4, list remains the same Introduction to Sorting Techniques/Lesson 6/Slide 7 of 40 ©NIIT
8. 8. Introduction to Sorting Techniques Bubble Sort (Contd..) Compare X4 with X5. Since X4 X5 interchange happens 2, 5, 6, 3, 7 At the end of this pass, the largest number is placed at the last position. Let us see what happens for the remainder of passes Pass 2: 2 5 6 3 7 2 5 6 3 7 2 5 3 6 7 Introduction to Sorting Techniques/Lesson 6/Slide 8 of 40 ©NIIT
9. 9. Introduction to Sorting Techniques Bubble Sort (Contd.) Pass 3 : 2 5 3 6 7 2 3 5 6 7 Pass 4 : 2 3 5 6 7 All the elements are sorted after the fourth pass Introduction to Sorting Techniques/Lesson 6/Slide 9 of 40 ©NIIT
10. 10. Introduction to Sorting Techniques Problem Statement 6.D.1 Write a program to store 10 student marks. After implementing the bubble sorting, display the contents of sorted array. Introduction to Sorting Techniques/Lesson 6/Slide 10 of ©NIIT 40
11. 11. Introduction to Sorting Techniques Selection Sort The algorithm finds the smallest element and puts it in first position Then finds second smallest and puts in the second position This is done for n-1 times Introduction to Sorting Techniques/Lesson 6/Slide 11 of ©NIIT 40
12. 12. Introduction to Sorting Techniques Selection Sort (Contd..) Consider an array Num of n elements Step 1: Search for location of smallest element Interchange the contents with the first location in the array Step 2: Search for smallest element in sublist of n-1 elements Interchange with the contents of the second location Introduction to Sorting Techniques/Lesson 6/Slide 12 of ©NIIT 40
13. 13. Introduction to Sorting Techniques Selection Sort (Contd..) Step n: Search for the smaller between n-1 and n and interchange Take a variable MIN to contain the current smallest value A variable LOC to contain the location of the smallest value Traverse the list comparing minimum with other elements Introduction to Sorting Techniques/Lesson 6/Slide 13 of ©NIIT 40
14. 14. Introduction to Sorting Techniques Selection Sort (Contd..) If MIN = Num[i] then MIN=Num[i] else move to next location MIN will contain the smallest value after comparison with the last element LOC will contain the location of MIN Introduction to Sorting Techniques/Lesson 6/Slide 14 of ©NIIT 40
15. 15. Introduction to Sorting Techniques Selection Sort (Contd..) This procedure finds the smallest of the elements among NUM[I], NUM[I+1], . .. , NUM[N] MINIMUM(NUM,I,N,LOC) 1. Set MIN=NUM[I] and LOC = I 2. Repeat for J = I + 1, I + 2 , . . . , N If MIN NUM[J] then set MIN = NUM[J] and LOC = J 3. Return Introduction to Sorting Techniques/Lesson 6/Slide 15 of ©NIIT 40
16. 16. Introduction to Sorting Techniques Selection Sort (Contd..) Algorithm: This algorithm sorts an array with N elements. 4. Repeat steps 2 and 3 for I=1,2,3,..., N 5. Call MINIMUM(NUM,I,N, LOC) 6. [Interchange Num[I] with NUM[LOC]] Introduction to Sorting Techniques/Lesson 6/Slide 16 of ©NIIT 40
17. 17. Introduction to Sorting Techniques Selection Sort (Contd.) Set TEMP= NUM[I], NUM[I] = NUM[LOC] NUM[LOC]=TEMP. [loop of step 1 ends] 6. Exit Introduction to Sorting Techniques/Lesson 6/Slide 17 of ©NIIT 40
18. 18. Introduction to Sorting Techniques Selection Sort (Contd..) Pass Num[1] Num[2] Num[3] Num[4] Num[5] Num[6]I=1 LOC=5 35 57 30 76 3 10I=2 LOC=6 3 57 30 76 35 10I=3 LOC=3 3 10 30 76 35 57I=4 LOC=5 3 10 30 76 35 57I=5 LOC=6 3 10 30 35 76 57Sorted 3 10 30 35 57 76 Introduction to Sorting Techniques/Lesson 6/Slide 18 of ©NIIT 40
19. 19. Introduction to Sorting Techniques Insertion Sort An element is put into its correct position with each iteration of the main loop, relative to the sub-array that has already been processed Inserts each element to its proper location Frequently used with small number of elements Introduction to Sorting Techniques/Lesson 6/Slide 19 of ©NIIT 40
20. 20. Introduction to Sorting Techniques Insertion Sort (Contd..) The following steps are performed Pass 1: Num[1] by itself is sorted Pass 2: Num[2] is inserted either before or after Num[1] , such that Num[1] and Num[2] are sorted Pass 3: Num[3] is inserted into its proper place, i.e. before Num[1], between Num[1] and Num[2], or after Num[2], such that Num[1], Num[2], Num[3] are sorted Introduction to Sorting Techniques/Lesson 6/Slide 20 of ©NIIT 40
21. 21. Introduction to Sorting Techniques Insertion Sort (Contd..) .... .... .... .... .... .... Pass N: Num[n] is inserted into its proper place such that Num[1], Num[2],... ,Num[n] is sorted Introduction to Sorting Techniques/Lesson 6/Slide 21 of ©NIIT 40
22. 22. Introduction to Sorting Techniques Insertion Sort (Contd..) Pass Num[1] Num[2] Num[3] Num[4] Num[5] Num[6] Num[6] K=1 -∞ 35 57 30 76 3 10 K=2 -∞ 35 57 30 76 3 10 K=3 -∞ 35 57 30 76 3 10 K=4 -∞ 30 35 57 76 3 10 K=5 -∞ 30 35 57 76 3 10 K=6 -∞ 3 30 35 57 76 10 Sorted -∞ 3 10 30 35 57 76 Introduction to Sorting Techniques/Lesson 6/Slide 22 of ©NIIT 40
23. 23. Introduction to Sorting Techniques Insertion Sort (Contd..) Algorithm: INSERTION (NUM,N) The algorithm sorts the array NUM with N elements 1. Set Num[0] = -∞ [ Initialize the sentinel element] 2. Repeat steps 3 to 5 for K=2,3,4,... , N 3. Set TEMP=Num[K] and PTR =K-1 Introduction to Sorting Techniques/Lesson 6/Slide 23 of ©NIIT 40
24. 24. Introduction to Sorting Techniques Insertion Sort (Contd..) 1. Do while TEMP Num[PTR] a. Set Num[PTR+1] = Num[PTR] b. Set PTR=PTR – 1 (end of loop) 1. Set Num[PTR+1]=TEMP (end of loop) 1. Return Introduction to Sorting Techniques/Lesson 6/Slide 24 of ©NIIT 40
25. 25. Introduction to Sorting Techniques Problem Statement 6.P.1 Write a function implementing insertion sorting. Introduction to Sorting Techniques/Lesson 6/Slide 25 of ©NIIT 40
26. 26. Introduction to Sorting Techniques Merge Sort Step 1: Split the list into sublists of equal size Step 2: Sort them separately Step 3: Merge them together Introduction to Sorting Techniques/Lesson 6/Slide 26 of ©NIIT 40
27. 27. Introduction to Sorting Techniques Merge Sort (Contd..) Example: Let us sort 53, 10,30,76,3,57,24 using merge sort Step 1: Chop them into pairs and sort them 53, 10 30, 76 3, 57 24 Step 2: Merge the pairs to obtain the following sorted sublists 10,53 30,76 3,57 24 Introduction to Sorting Techniques/Lesson 6/Slide 27 of ©NIIT 40
28. 28. Introduction to Sorting Techniques Merge Sort (Contd..) Step 3: Merge the sorted sublists to obtain a single sorted array 3, 10, 24, 30, 53, 57, 76 The original array is now sorted Introduction to Sorting Techniques/Lesson 6/Slide 28 of ©NIIT 40
29. 29. Introduction to Sorting Techniques Quick Sort We arrange the list in some logical order Algorithm is divide and conquer type Sorting is reduced to sorting two smaller subsets Reduction step finds the final position of one number Introduction to Sorting Techniques/Lesson 6/Slide 29 of ©NIIT 40
30. 30. Introduction to Sorting Techniques Quick Sort (Contd..) Step1: Take first element as key element Scan right to left for an element less than key element Interchange positions of the elements Step 2: Scan right to left for element greater than key element Interchange positions of the elements Introduction to Sorting Techniques/Lesson 6/Slide 30 of ©NIIT 40
31. 31. Introduction to Sorting Techniques Quick Sort (Contd..) Step 3: Continue step 1 and step 2 till ® all element in right are greater than key ® all element in left are less than key The key element will be placed in its correct position Repeat reduction step for each sub list with two or more elements Introduction to Sorting Techniques/Lesson 6/Slide 31 of ©NIIT 40
32. 32. Introduction to Sorting Techniques Quick Sort (Contd..) Example: 35 57 30 76 3 10 In this example we take the number 35 as the key STEP 1: 10 57 30 76 3 35 STEP 2: 10 35 30 76 3 57 STEP 3: 10 3 30 76 35 57 STEP 3: 10 3 30 35 76 57 35 is correctly placed in its final position Introduction to Sorting Techniques/Lesson 6/Slide 32 of ©NIIT 40
33. 33. Introduction to Sorting Techniques Problem Statement 6.D.2 Write a program to store 10 numbers in an array. After implementing quick sorting, display the contents of the sorted array. Introduction to Sorting Techniques/Lesson 6/Slide 33 of ©NIIT 40
34. 34. Introduction to Sorting Techniques Complexities For bubble sort The number of comparisons f(n) = (n-1) + (n-2) + (n-3) +...+ 2 + 1 = n(n-1) 2 = n2 + O(n) = O(n2) 2 Introduction to Sorting Techniques/Lesson 6/Slide 34 of ©NIIT 40
35. 35. Introduction to Sorting Techniques Complexities (Contd..) For quick sort Worst case (when the list is already sorted) f(n) = n + (n-1) + (n-2) +...+2 + 1 = n(n-1) = n2 + O(n) = O(n2) 2 2 Average case ® In kth level finds the location of 2K-1 elements f(n) = O(n log n) Introduction to Sorting Techniques/Lesson 6/Slide 35 of ©NIIT 40
36. 36. Introduction to Sorting Techniques Complexities (Contd..) For selection sort Number of comparisons depends on original order of elements There are n-1 comparisons in pass1 There are n-2 comparisons in pass 2 f(n) = (n-1) + (n-2) + (n-3) +...+ 2 + 1 = n(n-1) = n2 + O(n) = O(n2) 2 2 Introduction to Sorting Techniques/Lesson 6/Slide 36 of ©NIIT 40
37. 37. Introduction to Sorting Techniques Complexities (Contd..) For insertion sort Worst case occurs when the array is in the reverse order Inner loop executes maximum number K-1 of comparisons f(n) = 1+ 2 + . . . + (n-1) = n(n-1) = O(n2) 2 Introduction to Sorting Techniques/Lesson 6/Slide 37 of ©NIIT 40
38. 38. Introduction to Sorting Techniques Complexities (Contd..) Average case There will be approximately (K-1)/2 comparisons in the inner loop f(n) = 1+ 2+ 3+ . . .+ (n-1) = n(n-1) = O(n2) 2 2 2 2 4 Introduction to Sorting Techniques/Lesson 6/Slide 38 of ©NIIT 40
39. 39. Introduction to Sorting TechniquesSummaryIn this lesson, you learned that: Different types of sorting are: Bubble sort Selection sort Insertion sort Merge sort Quick sort Introduction to Sorting Techniques/Lesson 6/Slide 39 of ©NIIT 40
40. 40. Introduction to Sorting TechniquesSummary (Contd..) The time required to execute bubble sort algorithm is proportional to n2, where n is the number of inputs The number of comparisons in a selection sort is independent of the original order of the elements The insertion sort is a very slow algorithm when the number of elements in the array is very large The Quick sort algorithm has a worst case running time of order n2/2 Introduction to Sorting Techniques/Lesson 6/Slide 40 of ©NIIT 40