1 / 225

Sorting

Sorting. Chapter 8. Chapter Objectives. To learn how to use the standard sorting methods in the Java API To learn how to implement the following sorting algorithms: selection sort bubble sort insertion sort Shell sort merge sort heapsort quicksort

kamea
Download Presentation

Sorting

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. Sorting Chapter 8

  2. Chapter Objectives • To learn how to use the standard sorting methods in the Java API • To learn how to implement the following sorting algorithms: • selection sort • bubble sort • insertion sort • Shell sort • merge sort • heapsort • quicksort • To understand the differences in performance of these algorithms, and which to use for small, medium, and large arrays

  3. Introduction • Sorting entails arranging data in order • Familiarity with sorting algorithms is an important programming skill • The study of sorting algorithms provides insight into • problem solving techniques such as divide and conquer • the analysis and comparison of algorithms which perform the same task

  4. Using Java Sorting Methods • The Java API provides a class Arrayswith several overloaded sort methods for different array types • The Collections class provides similar sorting methods for Lists • Sorting methods for arrays of primitive types are based on the quicksort algorithm • Sorting methods for arrays of objects and Lists are based on the merge sort algorithm • Both algorithms are O(n log n)

  5. Using Java Sorting Methods (cont.)

  6. Selection Sort Section 8.2

  7. Selection Sort • Selection sort is relatively easy to understand • It sorts an array by making several passes through the array, selecting a next smallest item in the array each time and placing it where it belongs in the array • While the sort algorithms are not limited to arrays, throughout this chapter we will sort arrays for simplicity • All items to be sorted must be Comparable objects, so, for example, any int values must be wrapped in Integer objects

  8. Trace of Selection Sort • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20

  9. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill

  10. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill posMin

  11. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill posMin

  12. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill posMin

  13. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill posMin

  14. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill posMin

  15. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill posMin

  16. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill posMin

  17. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 35 60 65 fill posMin

  18. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 35 60 65 fill posMin

  19. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 35 60 65 fill posMin

  20. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 35 60 65 fill posMin

  21. Trace of Selection Sort (cont.) • n= number of elements in the array • for fill = 0 to n – 2 do • Set posMin to the subscript of a smallest item in the subarray starting at subscript fill • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 35 60 65

  22. Trace of Selection Sort Refinement • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20

  23. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill

  24. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill posMin

  25. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  26. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  27. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  28. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  29. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  30. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  31. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  32. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  33. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  34. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 35 65 30 60 20 fill next posMin

  35. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  36. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  37. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  38. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  39. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  40. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  41. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  42. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  43. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  44. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 65 30 60 35 fill next posMin

  45. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill next posMin

  46. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill next posMin

  47. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill next posMin

  48. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill next posMin

  49. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill next posMin

  50. Trace of Selection Sort Refinement (cont.) • for fill = 0 to n – 2 do • Initialize posMin to fill • for next = fill + 1 to n – 1 do • if the item at next is less than the item at posMin • Reset posMin to next • Exchange the item at posMin with the one at fill 0 1 2 3 4 20 30 65 60 35 fill next posMin

More Related