Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.

Slides:



Advertisements
Similar presentations
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Advertisements

Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
CMPS1371 Introduction to Computing for Engineers SORTING.
CSE 373: Data Structures and Algorithms
Simple Sorting Algorithms
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Searching and Sorting Gary Wong.
Computer Science Searching & Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSE 373 Data Structures and Algorithms
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
CSCI 51 Introduction to Programming March 12, 2009.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
3 – SIMPLE SORTING ALGORITHMS
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
CSCI 51 Introduction to Programming March 10, 2009.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
CSCE 210 Data Structures and Algorithms
Introduction to Search Algorithms
Simple Sorting Algorithms
10.3 Bubble Sort Chapter 10 - Sorting.
Linear and Binary Search
Quicksort analysis Bubble sort
Sorting … and Insertion Sort.
Searching and Sorting 1-D Arrays
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Simple Sorting Algorithms
Simple Sorting Algorithms
Simple Sorting Algorithms
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Computer Science 1620 Sorting

cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection (e.g. select 5 students with highest gpas) etc the sorting problem given a list of non-sorted data, rearrange the data into ascending or descending order

Sorting Sort

Sorting a well-studied area of Computer Science many clever algorithms exist Simple: bubble sort selection sort insertion sort Complex: merge sort quick sort (very popular!!) We will focus on the simple sorting algorithms, as the complex sorts involve recursion.

Sorting – the general idea the sort will be conducted using a series of swaps (discussed on next slide) we will not use any extra arrays inline the quality of a sorting algorithm is typically based on two metrics: # of comparisons # of swaps

Simple Sorting The swap procedure swap(a[i], a[j]) swaps the elements i and j in a, so that a[i] becomes a[j] and vice versa eg. swap(a[3], a[5]) provided by algorithm header file #include Before After

Bubble Sort a very simple sorting algorithm the basic idea: when two adjacent items are found to be out of order, swap them adjacent in this case means two items that are side by side in an array (a[1], a[2]) or (b[i], b[i+1])

Bubble Sort – Algorithm Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements (that is, (1,2), (2,3), (3,4), and so on) Step If a pair of adjacent elements are found to be out of order, swap them

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, so we don't swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Are items still out of order? yes, so we repeat

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Are items still out of order? yes, so we repeat

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 4517

31 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 45

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap. 45

50 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 63

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap 50

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap 50

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 Are items still out of order? yes, so we repeat

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 Out of order, so we swap 2417

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Are items still out of order? no, we are done

Bubble Sort guaranteed to sort our data requires up to n passes through the array

#include using namespace std; void bubble_sort(int a[], int sz) { Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them }

#include using namespace std; void bubble_sort(int a[], int sz) { Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: for each pair of adjacent elements Step If these items out of order, swap }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step If these items out of order, swap }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step If these items out of order, swap }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step If these items out of order Step swap the two items }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step If these items out of order Step swap the two items }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { Step swap the two items }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { Step swap the two items }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); } What is condition here?

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); } Will this work?

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }

How do we know if the list is out of order? Question: What happens in our "check" loop when all of the items are in order?

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

What did you notice? no swaps took place our list is sorted if during our check loop, no swaps took place during our check loop, if a swap occurs, then remember this use a boolean variable initially false true if a swap occurs

#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }

#include using namespace std; void bubble_sort(int a[], int sz) { bool swapped = true; while (swapped) { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; }

Why do I initially set swapped to true to guarantee that the while loop executes at least once what is another way to guarantee this? make it a do-while loop

#include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } } while (swapped); }

An Improvement to Bubble Sort this initial version of bubble sort works fine how many comparisons does it make? each time through the "check" loop: n-1 how many times does it go through the check loop: n (in the worst case) therefore, it can make up to n(n-1) comparisons

Better Bubble Sort what do you notice at the end of each "check" loop

Bubble Sort After first time through loop

Bubble Sort After second time through loop

63 Bubble Sort After third time through loop 50

Each time through the loop, the ith largest value gets put in its final position first time through the loop, largest item placed in last spot second time through the loop, second largest item placed in second-last spot ith time through the loop, ith largest item placed in ith largest spot

What does this mean? on the second pass through the loop, do I need to compare the last item? no – it's the largest item, so belongs in the last spot this means that each time through the loop, the number of items to sort gets smaller

Bubble Sort After first time through loop Only these elements need to be sorted

Bubble Sort After second time through loop Only these elements need to be sorted

63 Bubble Sort After third time through loop 50 Only these elements need to be sorted

#include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } } while (swapped); }

#include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } sz--; } while (swapped); }

How good is this simple fix? how many comparisons: first time through the loop: n – 1 second time through the loop: n-2 ith time through the loop: n-i Number of comparisons in the worst case:

Hence: this very small extension to the bubble sort algorithm improves the # of comparisons by 50%

Bubble Sort – Problems suppose you are sorting an array of strings each time you call swap, the following must occur* copy first string to temporary storage copy second string value to first string copy temporary storage value to second string if strings are big, this can be costly for time ie. swap can be expensive

Bubble Sort what makes bubble sort so "expensive" in terms of computation is the number of swaps that it does number of comparisons: n(n-1)/2 in worst case number of swaps: n(n-1)/2 in worst case

Selection Sort suppose my items are arranged like this what will the first "check" pass of bubble sort look like?

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, so we don't swap

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.

Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them

What did you notice? the biggest number (96) got placed in the last spot (discussed last day) however, the number gets "dragged" along by a number of swaps what if, instead of dragging it to its final location, I swapped it directly there?

Selection Sort Algorithm: Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array in other words find the smallest element, swap it into the first spot find the 2 nd smallest element, swap it into the 2 nd spot find the 3 rd smallest element, swap it into the 3 rd lspot

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 8550

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 8550 smallest

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array nd smallest

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array rd smallest

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest

Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest

#include using namespace std; void selection_sort(int a[], int n) { Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }

#include using namespace std; void selection_sort(int a[], int n) { Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 1; i <= n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array } Will this work?

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array } Remember: arrays are ordered 0 to n-1, not 1 to n

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); Step 1.2 swap that number in the i-th spot in the array }

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); Step 1.2 swap that number in the i-th spot in the array }

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); }

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); } How do we find the ith smallest?

Let's simplify this: how do we set ith to point to the smallest number? One solution: assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a bigger number at another location, remember that location

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a bigger number at another location, remember that location

This works fine for finding the smallest but what about finding the 2 nd smallest, or 3 rd smallest, etc? let's consider what the array looks like after the ith smallest element is found

Bubble Sort Our array, initially 8550

Bubble Sort After smallest is found 1750

Bubble Sort After second smallest is found 1750

Bubble Sort After third smallest is found 1750

Bubble Sort After third smallest is found 1750

What do you notice? after the smallest is found, it is placed in location 0 this means that the 2 nd smallest item is the smallest item between location 1 and location n-1 after the 2 nd smallest is found, it is placed in location 1 this means that the 3 rd smallest item is the smallest item between location 2 and location n-1

Bubble Sort Our array, initially 8550 Find smallest in this set

Bubble Sort After smallest is found 1750 Find smallest in this set

Bubble Sort After second smallest is found 1750 Find smallest in this set

Bubble Sort After third smallest is found 1750 Find smallest in this set

In other words we only need to search the elements beginning at i

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); }

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = i; for (int j = i+1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); }

A Couple of Minor Improvements 1) We do not need to do the last pass through the outer loop 2) We do not need to swap if i == ith

#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n-1; i++) { int ith = i; for (int j = i+1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } if (i != ith) swap(a[ith], a[i]); }

Selection Sort how many comparisons does selection sort make? first pass through loop: n-1 second pass through loop: n-2 third pass through loop: n-3 …. n-1 pass through loop: 1

Selection Sort just as many compares as bubble sort how many swaps (worst case)? N -1 this is a big improvement over bubble sort, which required n(n-1)/2 swaps

Insertion Sort nested loop, just like selection sort outer loop moves one at a time through each element of the array at each element "picks up" element out of the array places the element in its corresponding spot behind this index result when outer loop is at index i, everything behind index i is sorted

Insertion Sort Example:

Insertion Sort – Outer Loop loop through all elements one at a time starting at 2 nd element current

Insertion Sort – Outer Loop Step 1: take current element out of the array current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 24, so it's shifted current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop current 85

Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 85

Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 85

Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 85

Insertion Sort – Outer Loop Step 1: take current element out of the array current 85

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 63, so shift current 85

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 < 63, so stop! current 85

Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 8563

Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 8563

Insertion Sort – Outer Loop Step 1: take current element out of the array current 8563

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 45, so shift current 8563

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 63 > 45, so shift current 8563

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 < 45, so stop! current 8563

Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 8563

Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 8563

Insertion Sort – Outer Loop Step 1: take current element out of the array current 8563

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 17, so shift current 8563

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 63 > 17, so shift current 8563

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 45 > 17, so shift current 8563

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 > 17, so shift current 8563

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop! current 8563

Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 8563

Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 8563

Insertion Sort – Outer Loop And so on notice that the list to the left of current is always sorted current 8563

How do we code this? tricky Algorithm: for each element (starting at 2 nd ) take element out of array Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift place the current element in the empty spot created by shifting

#include using namespace std; void insertion_sort(int a[], int n) { Step 1: for each element (starting at 2nd) Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

#include using namespace std; void insertion_sort(int a[], int n) { Step 1: for each element (starting at 2nd) Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting } NOTE: This doesn't take the actual number out of the array – just a copy of it.

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } Step 1.3 place the current element in the empty spot created by shifting }

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } Step 1.3 place the current element in the empty spot created by shifting }

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; }

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( (j > 0) ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element

#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( (j > 0) && (a[j-1] > element) ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element

And so on What is the complexity of insertion sort? How many comparisons: First pass: 1 comparisons Second pass: 2 comparisons … ith passi comparisons n-1 passn-1 comparison

Swaps how many swaps? no swaps – just shifts shifting takes 1/3 as much work as swap how many shifts? possibly one for each comparison hence:

Which is the best algorithm? one source reports: selection sort roughly 60% faster than bubble sort (without optimization) why? less swaps insertion sort is roughly 40% faster than selection sort why? selection sort guaranteed to do n(n-1) comparisons insertion sort only does this many in the worst case in the best case, insertion sort requires linear comparisons

Insertion Sort Example:

Insertion Sort – Outer Loop loop through all elements one at a time starting at 2 nd element current

Insertion Sort – Outer Loop Step 1: take current element out of the array current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop current

Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1724 current

Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current

Insertion Sort – Outer Loop Step 1: take current element out of the array current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop current

Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1731 current

Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 1731 current

Insertion Sort – Outer Loop Step 1: take current element out of the array current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift current

Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop current

Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1745 current

Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 1745 current

And so on as you can see, insertion sort only does 1 comparison per item when the list is already sorted

Insertion Sort insertion sort is often daunting to new programmers much less intuitive than bubble sort or selection sort remember: after 1 st pass of inner loop, first two items are sorted after 2 nd pass of inner loop, first three items are sorted after 3 rd pass of inner loop, first four items are sorted etc …

Insertion Sort Example:

Insertion Sort – Outer Loop After first pass notice that first two items are sorted

Insertion Sort – Outer Loop After second pass notice that first three items are sorted

Insertion Sort – Outer Loop After third pass notice that first four items are sorted

Insertion Sort – Outer Loop After 4 th pass notice that first 5 th items are sorted