14_Sorting.pptx - Sorting Quadratic Sorting We next...

This preview shows page 1 - 20 out of 68 pages.

Sorting
Image of page 1

Subscribe to view the full document.

Quadratic Sorting We next presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort . This presentation demonstrates how the two algorithms work.
Image of page 2
Sorting an Array of Integers The picture shows an array of six integers that we want to sort from smallest to largest [0] [1] [2] [3] [4] [5]
Image of page 3

Subscribe to view the full document.

The Selectionsort Algorithm Start by finding the smallest entry. [0] [1] [2] [3] [4] [5]
Image of page 4
The Selectionsort Algorithm Start by finding the smallest entry. Swap the smallest entry with the first entry . [0] [1] [2] [3] [4] [5]
Image of page 5

Subscribe to view the full document.

The Selectionsort Algorithm Start by finding the smallest entry. Swap the smallest entry with the first entry . [0] [1] [2] [3] [4] [5]
Image of page 6
The Selectionsort Algorithm Part of the array is now sorted. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
Image of page 7

Subscribe to view the full document.

The Selectionsort Algorithm Find the smallest element in the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
Image of page 8
The Selectionsort Algorithm Find the smallest element in the unsorted side. Swap with the front of the unsorted Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
Image of page 9

Subscribe to view the full document.

The Selectionsort Algorithm We have increased the size of the sorted side by one element. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
Image of page 10
The Selectionsort Algorithm The process continues... Sorted side Unsorted side Smallest from unsorted Smallest from unsorted [0] [1] [2] [3] [4] [5]
Image of page 11

Subscribe to view the full document.

The Selectionsort Algorithm The process continues... Sorted side Unsorted side [0] [1] [2] [3] [4] [5] Swap with front Swap with front
Image of page 12
The Selectionsort Algorithm The process continues... Sorted side Unsorted side Sorted side is bigger Sorted side is bigger [0] [1] [2] [3] [4] [5]
Image of page 13

Subscribe to view the full document.

The Selectionsort Algorithm The process keeps adding one more number to the sorted side. The sorted side has the smallest numbers, arranged from small to large. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
Image of page 14
The Selectionsort Algorithm We can stop when the unsorted side has just one number, since that number must be the largest number. [0] [1] [2] [3] [4] [5] Sorted side Unsorted sid
Image of page 15

Subscribe to view the full document.

The Selectionsort Algorithm The array is now sorted. We repeatedly selected the smallest element, and moved this element to the front of the unsorted side. [0] [1] [2] [3] [4] [5]
Image of page 16
17 Selection Sort: Arrays Note: x[0] reserved for special purposes in later algorithms Problem: sort x[1], … , x[n] into ascending order int i,j,smallPos; ElementType smallest; for (i=1;i<=n-1;i++) { smallPos = i; smallest = x[smallPos]; for (j=i+1;j<=n-1;j++) if (x[j] < smallest) { smallest = x[j]; smallPos = j;} swap(x[smallest],x[i]) }
Image of page 17

Subscribe to view the full document.

The Insertionsort Algorithm The Insertionso rt algorithm also views the array as having a sorted side and an unsorted side. [0] [1] [2] [3] [4] [5]
Image of page 18
The Insertionsort Algorithm The sorted side starts with just the first element, which is not necessarily the smallest element.
Image of page 19

Subscribe to view the full document.

Image of page 20
  • Fall '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes