+ All Categories
Home > Documents > CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of...

CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of...

Date post: 14-Jan-2016
Category:
Upload: whitney-reed
View: 215 times
Download: 1 times
Share this document with a friend
43
CSC211 Data Structures Lecture 21 Lecture 21 Quadratic Sorting Quadratic Sorting Instructor: Prof. Xiaoyan Instructor: Prof. Xiaoyan Li Li Department of Computer Department of Computer Science Science Mount Holyoke College Mount Holyoke College
Transcript
Page 1: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

CSC211 Data Structures CSC211 Data Structures

Lecture 21Lecture 21

Quadratic SortingQuadratic Sorting

Instructor: Prof. Xiaoyan LiInstructor: Prof. Xiaoyan LiDepartment of Computer Science Department of Computer Science

Mount Holyoke CollegeMount Holyoke College

Page 2: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Template classes in visual studioTemplate classes in visual studio

Building C++ Applications with Template Building C++ Applications with Template Classes in Visual StudioClasses in Visual Studio

http://www.ecs.fullerton.edu/~sowell/cs331/TemplateClasses.htmlhttp://www.ecs.fullerton.edu/~sowell/cs331/TemplateClasses.html

Page 3: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Review: hashing/hash tableReview: hashing/hash table

Page 4: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Chapter 13 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.

Quadratic SortingQuadratic Sorting

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing C++Using C++

Page 5: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

How to sort an array of integers?How to sort an array of integers?

Data: {40, 20, 50 ,60, 10 ,15}Data: {40, 20, 50 ,60, 10 ,15}

Page 6: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Sorting an Array of IntegersSorting an Array of Integers

The picture shows an array of six integers that we want to sort from smallest to largest

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]

Page 7: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selectionsort AlgorithmThe Selectionsort Algorithm

Start by finding the smallest entry.

[0] [1] [2] [3] [4] [5]

Page 8: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selectionsort AlgorithmThe Selectionsort Algorithm

Start by finding the smallest entry.

Swap the smallest entry with the first entry.

[0] [1] [2] [3] [4] [5]

Page 9: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selectionsort AlgorithmThe Selectionsort Algorithm

Start by finding the smallest entry.

Swap the smallest entry with the first entry.

[0] [1] [2] [3] [4] [5]

Page 10: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selectionsort AlgorithmThe Selectionsort Algorithm

Part of the array is now sorted.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 11: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe Selectionsort Algorithm

Find the smallest element in the unsorted side.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 12: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe Selectionsort Algorithm

Find the smallest element in the unsorted side.

Swap with the front of the unsorted side.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 13: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe Selectionsort Algorithm

We have increased the size of the sorted side by one element.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 14: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe Selectionsort Algorithm

The process continues...

Sorted side Unsorted side

Smallestfrom

unsorted

Smallestfrom

unsorted

[0] [1] [2] [3] [4] [5]

Page 15: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe Selectionsort Algorithm

The process continues...

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Swap

with

front

Swap

with

front

Page 16: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe Selectionsort Algorithm

The process continues...

Sorted side Unsorted sideSorted side

is bigger

Sorted sideis bigger

[0] [1] [2] [3] [4] [5]

Page 17: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe 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]

Page 18: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe 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 side

Page 19: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe 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]

Page 20: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Selectionsort AlgorithmThe Selectionsort Algorithm

Question 1: Can you write out the code?

Question 2: What is the Big-O of the selectionsort algorithm?

Question 3: Best case, worst case and average case

Page 21: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

The Insertionsort algorithm also views the array as having a sorted side and an unsorted side. [0] [1] [2] [3] [4] [5]

Page 22: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

The sorted side starts with just the first element, which is not necessarily the smallest element. [0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 23: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

The sorted side grows by taking the front element from the unsorted side...

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 24: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

...and inserting it in the place that keeps the sorted side arranged from small to large. [0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 25: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

In this example, the new element goes in front of the element that was already in the sorted side. [0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 26: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

Sometimes we are lucky and the new inserted item doesn't need to move at all.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 27: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

Sometimes we are lucky twice in a row.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 28: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Copy the new element to a separate location.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 29: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Shift elements in the sorted side, creating an open space for the new element.

[0] [1] [2] [3] [4] [5]

Page 30: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Shift elements in the sorted side, creating an open space for the new element.

[0] [1] [2] [3] [4] [5]

Page 31: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Continue shifting elements...

[0] [1] [2] [3] [4] [5]

Page 32: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Continue shifting elements...

[0] [1] [2] [3] [4] [5]

Page 33: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

...until you reach the location for the new element.

[0] [1] [2] [3] [4] [5]

Page 34: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Copy the new element back into the array, at the correct location.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 35: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

How to Insert One ElementHow to Insert One Element

The last The last element element must also be must also be inserted. inserted. Start by Start by copying it...copying it...

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 36: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Question:Question:

How many shifts will occur before we copy this element back into the array?

[0] [1] [2] [3] [4] [5]

Page 37: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Question:Question:

Four items Four items are shifted.are shifted.

[0] [1] [2] [3] [4] [5]

Page 38: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Question:Question:

Four items Four items are shifted.are shifted.And then the And then the element is element is copied back copied back into the array.into the array.

[0] [1] [2] [3] [4] [5]

Page 39: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

The Insertionsort AlgorithmThe Insertionsort Algorithm

Question 1: Can you write out the code easily?

Question 2: What is the Big-O of the insertsort algorithm?

Question 3: Best case, worst case and average case

Page 40: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Both Selectionsort and Insertionsort have a worst-case time of O(n2), making them impractical for large arrays.

But they are easy to program, easy to debug. Insertionsort also has good performance when the

array is nearly sorted to begin with. But more sophisticated sorting algorithms are

needed when good performance is needed in all cases for large arrays.

Timing and Other Issues Timing and Other Issues

Page 41: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Quiz: (turn in your code) Quiz: (turn in your code)

Implement one of the sorting methods: Implement one of the sorting methods: Selectionsort, Insertionsort or BubblesortSelectionsort, Insertionsort or Bubblesort

Page 42: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Assignment 7: (optional for extra credits)Assignment 7: (optional for extra credits)

Assignment:Assignment: Implement and Test the Priority Queue using a Implement and Test the Priority Queue using a

HeapHeap

Purposes:Purposes: Ensure that you understand and can use heap which Ensure that you understand and can use heap which

is implemented in an array.is implemented in an array. Due date: Due date:

Dec. 19Dec. 19thth (the last day of the exam period) (the last day of the exam period)

It is online now!It is online now!

Page 43: CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

THE ENDTHE END

Presentation copyright 1997 Addison Wesley Longman,For use with Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.

Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.


Recommended