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

• No School
• AA 1
• 68

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

Sorting

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.
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]

Subscribe to view the full document.

The Selectionsort Algorithm Start by finding the smallest entry. [0] [1] [2] [3] [4] [5]
The Selectionsort Algorithm Start by finding the smallest entry. Swap the smallest entry with the first entry . [0] [1] [2] [3] [4] [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]
The Selectionsort Algorithm Part of the array is now sorted. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

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]
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]

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]
The Selectionsort Algorithm The process continues... Sorted side Unsorted side Smallest from unsorted Smallest from unsorted [0] [1] [2] [3] [4] [5]

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
The Selectionsort Algorithm The process continues... Sorted side Unsorted side Sorted side is bigger Sorted side is bigger [0] [1] [2] [3] [4] [5]

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]
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

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]
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]) }

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]
The Insertionsort Algorithm The sorted side starts with just the first element, which is not necessarily the smallest element.

Subscribe to view the full document.

• Fall '19

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

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