Sorting Algorithms.

Slides:



Advertisements
Similar presentations
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Advertisements

Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
CMPS1371 Introduction to Computing for Engineers SORTING.
Sorting Algorithms What is it: An algorithm that puts the elements of a list in a certain order.
Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.
Searching and Sorting Arrays
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Bubble Sort Example
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
OCR A Level F453: Data structures and data manipulation Data structures and data manipulation a. explain how static data structures may be.
Sort Algorithm.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Sorting Chapter 14.
Searching and Sorting Algorithms
Lesson Objectives Aims Key Words
Tips and tools for creating and presenting wide format slides
May 17th – Comparison Sorts
Lecture 14 Searching and Sorting Richard Gesick.
Recitation 13 Searching and Sorting.
Merging Merge. Keep track of smallest element in each sorted half.
Department of Computer Science
Lesson Objectives Aims Understand the following “standard algorithms”:
Data Structures 2018 Quiz Answers
Adapted from slides by Marty Stepp and Stuart Reges
Sorting Algorithms Written by J.J. Shepherd.
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Bubble, Selection & Insertion sort
Selection Sort Sorted Unsorted Swap
Welcome to CIS 068 ! Lesson 9: Sorting CIS 068.
“Human Sorting” It’s a “Problem Solving” game:
Sorting Algorithms Ellysa N. Kosinaya.
Lecture 11 Searching and Sorting Richard Gesick.
Topic 1: Problem Solving
IT 4043 Data Structures and Algorithms
Bubble Sort Key Revision Points.
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
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.
A G L O R H I M S T A Merging Merge.
Sorting Algorithms 2.1 – Algorithms.
Sorting.
Heaps By JJ Shepherd.
A G L O R H I M S T A Merging Merge.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
A G L O R H I M S T A Merging Merge.
CSCE 3110 Data Structures & Algorithm Analysis
“Human Sorting” It’s a “Problem Solving” game:
Insertion Sort Array index Value Insertion sort.
Sorting.
Stacks, Queues, ListNodes
Presentation transcript:

Sorting Algorithms

Lesson Objectives Aims To understand the following SORTING algorithms: Bubble Sort Merge Sort Insertion Sort To be able to construct a flow chart for each algorithm

Bubble Sort Bubble sort – moving through a list repeatedly, comparing pairs of elements, swapping elements that are in the wrong order.

Bubble Sort Algorithm SwapsMade = True Repeat until SwapsMade = False Take the first element and second element from the list IF element 1 > element 2 THEN Swap them ELSE Do nothing IF no more elements are left: IF SwapsMade = True, then return to the start – GOTO Step 3 ELSE set SwapsMade = False End Repeat

Bubble Sort - Example Element Number Value 1 2 3 4 5 6 7 8 13 9 10 12 1) Compare elements 1 and 2 2) Is element 1 > element 2? 1 2 3 4 5 6 7 8 13 9 10 12 3) Yes: so swap them 1 2 3 4 5 6 7 8 13 9 10 12 4) Compare elements 2 and 3 5) Is element 2 > element 3? 1 2 3 4 5 6 7 8 13 9 10 12 6) Yes: so swap them

Bubble Sort - Example Talk through the next steps that are taken in the bubble sort: 1 2 3 4 5 6 7 8 13 9 10 12 1 2 3 4 5 6 7 8 13 9 10 12 1 2 3 4 5 6 7 8 13 9 10 12 1 2 3 4 5 6 7 8 9 13 10 12

Bubble Sort - Example 1 2 3 4 5 6 7 8 9 13 10 12 Compare elements 5 and 6 Is element 5 > element 6? 1 2 3 4 5 6 7 8 9 10 13 12 Yes, so swap them 1 2 3 4 5 6 7 8 9 10 13 12 Compare elements 6 and 7 Is element 6 > element 7? 1 2 3 4 5 6 7 8 9 10 12 13 Yes, so swap them

You have come to the end of the list. Bubble Sort - Example 1 2 3 4 5 6 7 8 9 10 12 13 Compare elements 7 and 8 Is element 7 > element 8? 1 2 3 4 5 6 7 8 9 10 12 13 Yes, so swap them You have come to the end of the list. A change has been made. So you start again. Compare elements 1 and 2 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No

Bubble Sort - Example Compare 2 and 3 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No Compare 3 and 4 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No Compare 4 and 5 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No Compare 5 and 6 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No

Bubble Sort - Example You’ve reached the end. Have you made any swaps? Compare 6 and 7 1 2 3 4 5 6 7 8 9 10 12 13 Swap? Yes 1 2 3 4 5 6 7 8 9 10 12 13 Compare 7 and 8 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No You’ve reached the end. Have you made any swaps? Yes, so start again.

Sample exam question

Merge Sort List – a set of data. Merge sort: The list is split into individual elements These lists are then combined into a NEW list, 2 lists at a time.

Merge Sort Algorithm Split the list into individual elements Get two lists Compare the first element in both lists. Put the smallest into a new list. Continue steps 3 and 4 with each remaining element in the two lists Get the next two lists and repeat 3-5 Repeat 2 – 6 until only 1 list remains

Merging just two lists List A List B Merged List 2 5 16 or 1 8 15 22 

Show each step of the process. Task Merge the two lists: Show each step of the process. If you have two values the same, add one (it doesn’t matter from which list), and then add the second. 3 7 8 10 11 4 6 8 13

Merge Sort – Complete Example 22 13 5 2 8 15 Split the list into individual elements: 22 13 5 2 8 15 22 Merge individual lists 13 22 2 5 8 15 22 Merge sub-lists 2 5 13 22 8 15 22 Merge sub-lists 2 5 8 13 15 22

Use a merge sort to sort the list: Task Use a merge sort to sort the list: 2 19 3 45 77 10 25 29 60 1

Answer 2 19 3 45 77 10 25 29 60 1 2 19 3 45 77 10 25 29 60 1 2 19 3 45 10 77 25 29 1 60 2 3 19 45 10 25 29 77 1 60 2 3 10 19 25 29 45 77 1 60 1 2 3 10 19 25 29 45 60 77

Insertion Sort Inserting each element in to the correct location! Uses the concept of a “sorted list” and an “unsorted list”

Insertion Sort - Algorithm Take the first element 1 consider this to be in the right place - the ‘sorted’ list. The remaining elements are an ‘unsorted’ list. Get the next element from the ‘unsorted’ list. Compare this to the first element in the ‘sorted’ list. IF it is smaller, put it in front of that element (move the others along). ELSEIF it is larger, compare with the next. ELSEIF there are no more elements in the ‘sorted’ list put it in the final position. REPEAT UNTIL all element in the ‘unsorted’ list are in the ‘sorted’ list.

Insertion Sort Insertion sort this list 12 9 3 15 2 7 12 is the sorted list 12 9 3 15 2 7 Take element 1 of unsorted list 12 9 3 15 2 7 Compare to first element in sorted list 12 9 3 15 2 7 First element is greater: so put it on left 9 12 3 15 2 7 Take next element or unordered list 9 12 3 15 2 7 Compare to first element in sorted list 9 12 3 15 2 7 First element is greater: so put it on left 3 9 12 15 2 7

Insertion Sort Take element 1 of unsorted list 3 9 12 15 2 7 Compare to first element in sorted list 3 9 12 15 2 7 First element is smaller: so move to next element in ordered list 3 9 12 15 2 7 First element is smaller: so move to next element in ordered list 3 9 12 15 2 7 No more elements left, so insert at the end of the ordered list 3 9 12 15 2 7

Insertion Sort Can you talk through the next steps? 3 9 12 15 2 7 3 9

Take element 1 of unsorted list 2 3 7 9 12 15 Compare to element 1 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 2 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 3 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 4 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 5 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 6 in sorted list. 2 3 7 9 12 15 It is the same, so place it to the right. 2 3 7 9 12 15

Task Use an Insertion Sort to sort the list: 3 19 2 44 56 7 12

Review/Success Criteria