1 / 34

CHAPTER 7

CHAPTER 7. SORTING. CSEB324 DATA STRUCTURES & ALGORITHM. Sorting. Sorting means “Putting things in Order” or “arranging” Arranging can be done in two ways Ascending Order ( Smallest to Largest ) Descending Order ( Largest to Smallest )

beck
Download Presentation

CHAPTER 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 7 SORTING CSEB324 DATA STRUCTURES & ALGORITHM

  2. Sorting • Sorting means “Putting things in Order” or “arranging” • Arranging can be done in two ways • Ascending Order ( Smallest to Largest ) • Descending Order ( Largest to Smallest ) • In default Sorting Means arranging in ascending order • There are many algorithms for sorting, some simple but perhaps inefficient others complicated but efficient.

  3. Types of Sorting Techniques • We now discuss several sorting methods: • Selection Sort • Insertion Sort • Bubble Sort • Quick Sort • Shell Sort • Merge Sort • Heap Sort

  4. SELECTION SORT • “SELECT” an Element and Put in PROPER PLACE • From position 0, find the smallest and then exchange it with the element in position 0. • From position 1, find the smallest and exchange with position 1. • Now from 2 do the same until you have reached the end of the list

  5. Example:

  6. 21 67 67 33 21 84 49 50 75 21 33 67 84 49 50 75 49 67 84 67 49 50 75 21 33 50 50 84 75 49 84 67 21 33 Sorted 49 50 67 84 75 75 84 21 33 Starting from position 0 find the smallest and exhange with element in position 0. start at position 1 …. 67 33 21 84 49 50 75 21 33 49 67 50 84 75

  7. Try This! Trace the selection sort as it sorts the following array of integers into ascending order: 15 75 25 30 60 31

  8. Insertion Sort • This is the way that most people sort playing cards • The cards are picked up one by one and put in the proper place. • The Elements are Picked one by one and put into the Proper Place

  9. Example

  10. Try This! Trace the insertion sort as it sorts the following array of integers into descending order: 7 13 30 2 19 21

  11. Question 1 Draw a binary search tree for the below postfix algebraic expression. 5 x – y 3 + + 5 x – y / * [5 marks]

  12. Question 2 Based on Graph 1, draw the adjacency list and matrix. [5 marks]

  13. Bubble Sort • It works by comparing neighbors in the array and exchanging them if necessary to put the smaller of the pair first. • On each pass through the array an element 'bubbles' up into place. • Algorithm: pair = n – 1; do { last = 1; for (i = 1; i <= pair; i ++) if (x[i] > x[i + 1]){ swap x[i] with x[i + 1]; last = i; } pair = last - 1; } while (pair != 0);

  14. Example

  15. Try This! Trace the bubble sort as it sorts the following array of integers into ascending order: 80 29 19 75 10 50

  16. Quick Sort • To partition the list, we first choose some key from the list for which we hope about half the keys will come before and half after. • We shall use the name pivot for this selected key. • We next partition the entries so that all those with keys less than the pivot come in one sublist, and all those with greater keys come in another. • Finally we sort the two reduced lists separately, put the sublists together and the whole list will be in order

  17. Quick Sort 1) if the list has 0 or 1 elements return else do the following 2) pick an element in the list to use as pivot 3) spilt the remaining elements into two Sublist1 = elements<pivot Sublist2 = elements>pivot 4) return the list rearranged as Quicksort(Sublist1),pivot, Quicksort (Sublist2)

  18. Quick Sort • Once a pivot value has been selected, the algorithm exchanges the other values in the list until all the elements in sublist 1 are less than the pivot, and all the elements in sublist 2 are greater than the pivot. • Once this is done, the algorithm repeats the procedure on sublist 1, and then on sublist 2. The recursion stops when there is only one element in a sublist. At that point the original list is completely sorted.

  19. Quick Sort • The algorithm is coded primarily in two functions: QuickSort and Partition. QuickSort is a recursive function. Its pseudocode is shown below. QuickSort:If Starting Index < Ending Index Partition the List around a Pivot. QuickSort Sublist 1. QuickSort Sublist 2.end If.

  20. Example 1 : Given, 3 8 4 1 5 2 Avg = 23/6 = 3.83 Pivot = 4 Avg1 = 10/4 = 2.5 Pivot = 3 Avg1 = 6/3 = 2 Pivot = 2 • Note: • 6 inputs • 3 split levels • 3 concate levels

  21. Try This! Trace the quick sortas it sorts the following array of integers into ascending order: 42 43 2 1 46 4 45

  22. Answer

  23. Quick Sort (another method - notes) pivot = A[start]; left = start; right = end; while (left < right){ while (A[right] > pivot) right --; while ((A[left] <= pivot) && (left < right)) left ++; if (left < right) Swap(&A[left], &A[right]); } *pos = right; A[start] = A[*pos]; A[*pos] = pivot; • The algorithm:

  24. Try This! Trace the quick sortas it sorts the following array of integers into ascending order: 78 32 56 8 23 45

  25. Shell Sort Algorithm: • 1- Divide the list into sublists according to increment value. • 2- Sort each sublist. • 3- Merge all sublists into one list. • 4- Change the increment value to smaller number (minus 2). • 5- Repeat the processes until list is sorted.

  26. Example Using shell sort method, derive the result of the following sequence after : 10, 20, 30, 50, 100, 1, 39, 32, 16, 14, 25, 3, 2, 6, 22, 15, 42, 5 • increment 5 • increment 3

  27. Try This! Trace the shell sortas it sorts the following array of integers into ascending order: use increment 3 42 43 2 1 46 4 45 19 13 5 9

  28. Merge Sort • What is the concept used in Merge and Quick Sort? • This two sorting techniques use “DIVIDE and CONQUER “ Technique. • The Problem is divide into similar subproblems

  29. Merge Sort

  30. Try This! Trace the merge sortas it sorts the following array of integers into ascending order: 10 7 6 5 8 1 9

  31. Heap Sort Algorithm: • Turn the original array into a heap array. • Exchange the root which is the largest element in the heap with the last element in the unsorted list. The largest element being added to the beginning of the sorted list. • Then reheap down to reconstruct the heap and exchange again. • The reheap and exchange process continues until the entire list is sorted.

  32. Heap Sort

  33. Try This! Trace the heap sortas it sorts the following array of integers into ascending order: 10 7 6 5 8 1 9

  34. The End

More Related