Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College."— Presentation transcript:

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

2 Template classes in visual studio p Building C++ Applications with Template Classes in Visual Studio http://www.ecs.fullerton.edu/~sowell/cs331/TemplateClasses.html

3 Review: hashing/hash table

4 p p Chapter 13 presents several common algorithms for sorting an array of integers. p p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting Data Structures and Other Objects Using C++

5 How to sort an array of integers? p Data: {40, 20, 50,60, 10,15}

6 Sorting an Array of Integers p p The picture shows an array of six integers that we want to sort from smallest to largest [0] [1] [2] [3] [4] [5]

7 The Selectionsort Algorithm p p Start by finding the smallest entry. [0] [1] [2] [3] [4] [5]

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

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

10 The Selectionsort Algorithm p p Part of the array is now sorted. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

11 The Selectionsort Algorithm p p Find the smallest element in the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

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

13 The Selectionsort Algorithm p p We have increased the size of the sorted side by one element. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

14 The Selectionsort Algorithm p p The process continues... Sorted side Unsorted side Smallest from unsorted Smallest from unsorted [0] [1] [2] [3] [4] [5]

15 The Selectionsort Algorithm p p The process continues... Sorted side Unsorted side [0] [1] [2] [3] [4] [5] Swap with front Swap with front

16 The Selectionsort Algorithm p p The process continues... Sorted side Unsorted side Sorted side is bigger Sorted side is bigger [0] [1] [2] [3] [4] [5]

17 The Selectionsort Algorithm p p The process keeps adding one more number to the sorted side. p p The sorted side has the smallest numbers, arranged from small to large. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

18 The Selectionsort Algorithm p p 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

19 The Selectionsort Algorithm p p The array is now sorted. p p We repeatedly selected the smallest element, and moved this element to the front of the unsorted side. [0] [1] [2] [3] [4] [5]

20 The Selectionsort Algorithm p p Question 1: p p Can you write out the code? p p Question 2: p p What is the Big-O of the selectionsort algorithm? p p Question 3: p p Best case, worst case and average case

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

22 The Insertionsort Algorithm p p 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

23 The Insertionsort Algorithm p p The sorted side grows by taking the front element from the unsorted side... [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

24 The Insertionsort Algorithm p p...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

25 The Insertionsort Algorithm p p 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

26 The Insertionsort Algorithm p p 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

27 The Insertionsort Algorithm p p Sometimes we are lucky twice in a row. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

28 How to Insert One Element ¶ ¶ Copy the new element to a separate location. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

29 How to Insert One Element · · Shift elements in the sorted side, creating an open space for the new element. [0] [1] [2] [3] [4] [5]

30 How to Insert One Element · · Shift elements in the sorted side, creating an open space for the new element. [0] [1] [2] [3] [4] [5]

31 How to Insert One Element · · Continue shifting elements... [0] [1] [2] [3] [4] [5]

32 How to Insert One Element · · Continue shifting elements... [0] [1] [2] [3] [4] [5]

33 How to Insert One Element · ·...until you reach the location for the new element. [0] [1] [2] [3] [4] [5]

34 How 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

35 How to Insert One Element p The last element must also be inserted. Start by copying it... [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

36 Question: How many shifts will occur before we copy this element back into the array? [0] [1] [2] [3] [4] [5]

37 Question: p Four items are shifted. [0] [1] [2] [3] [4] [5]

38 Question: p Four items are shifted. p And then the element is copied back into the array. [0] [1] [2] [3] [4] [5]

39 The Insertionsort Algorithm p p Question 1: p p Can you write out the code easily? p p Question 2: p p What is the Big-O of the insertsort algorithm? p p Question 3: p p Best case, worst case and average case

40 p p Both Selectionsort and Insertionsort have a worst- case time of O(n 2 ), making them impractical for large arrays. p p But they are easy to program, easy to debug. p p Insertionsort also has good performance when the array is nearly sorted to begin with. p p But more sophisticated sorting algorithms are needed when good performance is needed in all cases for large arrays. Timing and Other Issues

41 Quiz: (turn in your code) p Implement one of the sorting methods: Selectionsort, Insertionsort or Bubblesort

42 Assignment 7: ( optional for extra credits ) p Assignment: p Implement and Test the Priority Queue using a Heap p Purposes: p Ensure that you understand and can use heap which is implemented in an array. p Due date: p Dec. 19 th (the last day of the exam period) p It is online now!

43 T HE E ND 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 (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.


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

Similar presentations


Ads by Google