csc212 data structure - section fg lecture 21 quadratic sorting instructor: zhigang zhu department...
Post on 19-Dec-2015
225 views
TRANSCRIPT
![Page 1: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/1.jpg)
CSC212 Data Structure - Section FG
CSC212 Data Structure - Section FG
Lecture 21
Quadratic Sorting
Instructor: Zhigang Zhu
Department of Computer Science
City College of New York
![Page 2: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/2.jpg)
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 Structuresand Other ObjectsUsing C++
![Page 3: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/3.jpg)
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 4: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/4.jpg)
[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.
[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 5: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/5.jpg)
[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. [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 6: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/6.jpg)
[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. [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: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/7.jpg)
[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.
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 8: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/8.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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
Find the smallest element in the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 9: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/9.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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
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 10: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/10.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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
We have increased the size of the sorted side by one element.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 11: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/11.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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
The process continues...
Sorted side Unsorted side
Smallestfrom
unsorted
Smallestfrom
unsorted
[0] [1] [2] [3] [4] [5]
![Page 12: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/12.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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
The process continues...
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Swap
with
front
Swap
with
front
![Page 13: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/13.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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
The process continues...
Sorted side Unsorted sideSorted side
is bigger
Sorted sideis bigger
[0] [1] [2] [3] [4] [5]
![Page 14: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/14.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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
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 15: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/15.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6] [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
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 16: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/16.jpg)
[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
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 17: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/17.jpg)
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 deterministic?
![Page 18: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/18.jpg)
[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 19: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/19.jpg)
[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.
[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]
Sorted side Unsorted side
![Page 20: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/20.jpg)
[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
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
![Page 21: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/21.jpg)
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.
[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]
Sorted side Unsorted side
![Page 22: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/22.jpg)
[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.
[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]
Sorted side Unsorted side
![Page 23: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/23.jpg)
[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.
[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]
Sorted side Unsorted side
![Page 24: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/24.jpg)
[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.
[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]
Sorted side Unsorted side
![Page 25: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/25.jpg)
[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
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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]
Sorted side Unsorted side
![Page 26: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/26.jpg)
[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.
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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 27: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/27.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[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.
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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 28: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/28.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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...
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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 29: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/29.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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...
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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 30: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/30.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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.
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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 31: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/31.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][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.
[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]
Sorted side Unsorted side
![Page 32: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/32.jpg)
[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
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The last element must also be inserted. Start by copying it...
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
![Page 33: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/33.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
A QuizA Quiz
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How many shifts will occur before we copy this element back into the array?
[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 34: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/34.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
A QuizA Quiz
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Four items are shifted.
[0] [1] [2] [3] [4] [5]
![Page 35: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/35.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
A QuizA Quiz
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Four items are shifted.And then the element is copied back into the array.
[0] [1] [2] [3] [4] [5]
![Page 36: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/36.jpg)
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 deterministic?
![Page 37: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/37.jpg)
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 38: CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649d3a5503460f94a142c5/html5/thumbnails/38.jpg)
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.