Sorting algorithms.

Slides:



Advertisements
Similar presentations
Recursive binary search
Advertisements

For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
The structured programming theorem
Pointers.
Dynamically allocating arrays
Anatomy of a program.
Switch statement.
Binary search.
Do-while loops.
Command-line arguments
Console input.
Dangling pointers.
Sorted arrays.
Floating-point primitive data types
Dynamically allocating arrays within structures
Break statements.
Linked Lists.
Wild pointers.
The comma as a separator and as an operator
Selection sort.
Bucket sort.
The ternary conditional operator
Dynamically allocating structures
Memory leaks.
Pushing at the back.
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Templated Linked Lists
Repetitious operations
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Dynamically allocating arrays
Code-development strategies
Throwing exceptions.
Anatomy of a program.
Selection sort.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Class variables and class functions
Operator overloading.
The std::string class.
Dynamic allocation of arrays
Templates.
Insertion sort.
Sorted arrays.
Issues with classes.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Selection sort.
Searching and sorting arrays
Protecting pointers.
Data structures: class
An array class: constructor and destructor
Recursive binary search
The structured programming theorem
Algorithms and templates
Presentation transcript:

Sorting algorithms

Outline In this lesson, we will: Describe sorting algorithms Given an overview of existing algorithms Describe the sorting algorithms we will learn

Sorting This array is not sorted: int array[10]{82, 25, 32, 85, 16, 36, 40, 4, 28, 7}; Goal: Write an algorithm that converts an array to one that is sorted: Such an algorithm is described as a sorting algorithm 1 2 3 4 5 6 7 8 9 82 25 42 85 16 32 28 1 2 3 4 5 6 7 8 9 16 25 28 32 42 82 85

Sorting algorithms There are many different sorting algorithms selection sort insertion sort shell sort comb sort gnome sort heap sort merge sort quick sort bucket sort radix sort cube sort counting sort block sort Some are funny: bozo sort bogo sort stooge sort Some sorting algorithms are worse than others If you have ever learned bubble sort, forget it now… Even Senator Obama knew not to use bubble sort: https://www.youtube.com/watch?v=koMpGeZpu4Q

Sorting algorithms We will look at three algorithms: Selection sort Insertion sort Counting sort Each algorithm has its own benefits and drawbacks: There is no perfect one-size-fits-all sorting algorithm Except bubble sort: its only redeeming feature is a memorable name In your algorithms course, you will see faster algorithms: Heap sort Merge sort Quick sort

Sorting algorithms The function declaration of each sorting algorithm will be similar: void algorithm_name( double array[], std::size_t const capacity ); std::size_t const begin, std::size_t const end ); Only the capacity or end points are declared constant The entries of the array will be changed by the algorithm

Summary Following this lesson, you now Understand the definition of a sorting algorithm Know the signature that these algorithms will use

References [1] Wikipedia https://en.wikipedia.org/wiki/Sorting_algorithm [2] nist Dictionary of Algorithms and Data Structures https://xlinux.nist.gov/dads/HTML/sort.html [3] President Obama on sorting algorithms https://www.youtube.com/watch?v=koMpGeZpu4Q

Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see https://www.rbg.ca/ for more information.

Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.