Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort

Similar documents
Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Sorting. Task Description. Selection Sort. Should we worry about speed?

Programming II (CS300)

We can use a max-heap to sort data.

Sorting. Bringing Order to the World

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Searching and Sorting

Sorting Pearson Education, Inc. All rights reserved.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Sorting. Weiss chapter , 8.6

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Searching in General

Sorting is ordering a list of objects. Here are some sorting algorithms

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

DATA STRUCTURE AND ALGORITHM USING PYTHON

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Programming II (CS300)

Cpt S 122 Data Structures. Sorting

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Sorting and Searching Algorithms

Parallel Sorting Algorithms

Better sorting algorithms (Weiss chapter )

Search,Sort,Recursion

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

The B-Tree. Yufei Tao. ITEE University of Queensland. INFS4205/7205, Uni of Queensland

Sorting. Chapter 12. Objectives. Upon completion you will be able to:

Search and Sorting Algorithms

Programming in OOP/C++

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Module 2: Classical Algorithm Design Techniques

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

CISC 1100: Structures of Computer Science

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Sorting Goodrich, Tamassia Sorting 1

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

A loose end: binary search

Module 08: Searching and Sorting Algorithms

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Searching & Sorting in Java Bubble Sort

Searching, Sorting. Arizona State University 1

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Mergesort again. 1. Split the list into two equal parts

Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

CP222 Computer Science II. Searching and Sorting

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Faster Sorting Methods

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009

Divide and Conquer. Algorithm Fall Semester

COSC242 Lecture 7 Mergesort and Quicksort

UCS-406 (Data Structure) Lab Assignment-1 (2 weeks)

2. Sorting. 2.1 Introduction

Intro to Algorithms. Professor Kevin Gold

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

About this exam review

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

(Refer Slide Time: 01.26)

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Unit-2 Divide and conquer 2016

100 points total. CSE 3353 Homework 2 Spring 2013

Searching and Sorting

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Quick Sort. CSE Data Structures May 15, 2002

CS 171: Introduction to Computer Science II. Quicksort

UNIT 7. SEARCH, SORT AND MERGE

More recursion, Divide and conquer

CSC Design and Analysis of Algorithms

O(n): printing a list of n items to the screen, looking at each item once.

Recursive Algorithms. We would like to write an algorithm that computes the factorial of a given non-negative integer number n.

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Sorting. Data structures and Algorithms

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

CSE 2123 Sorting. Jeremy Morris

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Lecture 15: Algorithms. AP Computer Science Principles

Sorting Algorithms. + Analysis of the Sorting Algorithms

Introduction to Computers and Programming. Today

Divide-and-Conquer Algorithms

Sorting. Order in the court! sorting 1

Lecture 8: Mergesort / Quicksort Steven Skiena

We will stamp HW Block day:

ITEC2620 Introduction to Data Structures

Sorting. Introduction. Classification

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

CS 171: Introduction to Computer Science II. Quicksort

Transcription:

Sorting Sorting is the process of placing elements from a collection in some kind of order. For example, a list of words could be sorted alphabetically or by length. A list of cities could be sorted by population, by area, or by post code. Within computer science, sorting algorithms are an important area of study. Sorting a large number of items can take a substantial amount of computing resources. Like searching, the efficiency of a sorting algorithm is related to the number of items being processed. Consider the operations used to conduct the sorting process; Need to compare two values (e.g. which is smaller or larger). Need to exchange values if out of position.

Bubble Sort Bubble sort is so named because the numbers are said to bubble into their correct positions! In a bubble sort, the first two items (i.e. 1 and 2) are compared and are swopped round if they are not in the required order. Then the next pair (items 2 and 3) are compared. This continues until the end of the list. If they are being sorted into ascending order (lowest to highest), the item with the highest value will be in its correct position at the end of the first pass. Passes are repeated until there are no swaps.

Bubble sort code - Python The following code example will complete a bubble sort on the following list: [54, 26, 93, 17, 77, 31, 44, 55, 20] At the start of the second pass, the largest value is now in place. There are n 1 n 1 items left to sort, meaning that there will be n 2 n 2 pairs. Since each pass places the next largest value in place, the total number of passes necessary will be n 1 n 1. After completing the n 1 n 1 passes, the smallest item must be in the correct position with no further processing required.

Making the bubble sort more efficient A bubble sort is often considered the most inefficient sorting method since it must exchange items before the final location is known. These wasted exchange operations are very costly. However, because the bubble sort makes passes through the entire unsorted portion of the list, it has the capability to do something most sorting algorithms cannot. In particular, if during a pass there are no exchanges, then we know that the list must be sorted. A bubble sort can be modified to stop early if it finds that the list has become sorted.

Bubble Sort Pesudocode

Bubble Sort: Pseudocode Pseudocode explained n is the length of the list, e.g. 6 A is the example list, e.g. [10,25,15,66,8,2] i is a looping number from 1 to 1 from the end of the length of the array, e.g. 1-5 5. This process is repeated until n=1 or there is no more swops to make whichever comes first. 4. Once the code has made a full pass across all 6 items (items 0-5 comparing 0>1, 1>2, 2>3, 3>4, 4>5), the largest number will be in place. There is no need to check this number from now on so n = n-1. End of pass 3. The code loops 5 times (n-1), and i now begins at 1, which means items 1 and 2 are now compared. 1. For the first pass of the list, A[i-1] is list item number 0, which in the example list is 10 and A[i] is 1 (as it s the first pass through the list). 2. The if statement uses Boolean to compare the size of list item 0 and 1. If item zero is bigger than 1, then they are swopped around.

Task: Understanding Bubble sort Complete the sorting process table to show your understanding of how bubble sort works. Numbers used: 10,25,15,66,8,2 List length = 6 Pass 1 (n=5) i=1 10 25 15 66 8 2 i=2 10 25 15 66 8 2 i=3 10 15 25 66 8 2 i=4 i=5 Pass 3 (n=3) i=1 i=1 i=2 i=3 Final order: Yellow = numbers being compared Pass 2 (n=4) i=1 10 15 25 8 2 66 i=2 i=3 i=4 Pass 4 (n=2) i=2 Pass 5 (n=1) i=1

Bubble sort: Answers Complete the sorting process table to show your understanding of how bubble sort works. Numbers used: 10,25,15,66,8,2 List length = 6 Yellow = numbers being compared Pass 1 (n=5) i=1 10 25 15 66 8 2 i=2 10 25 15 66 8 2 i=3 10 15 25 66 8 2 i=4 10 15 25 66 8 2 i=5 10 15 25 8 66 2 Pass 2 (n=4) i=1 10 15 25 8 2 66 i=2 10 15 25 8 2 66 i=3 10 15 25 8 2 66 i=4 10 15 8 25 2 66 Pass 4 (n=2) Pass 3 (n=3) i=1 10 8 2 15 25 66 i=1 10 15 8 2 25 66 i=2 10 15 8 2 25 66 i=3 10 8 15 2 25 66 i=2 8 10 2 15 25 66 Pass 5 (n=1) i=1 8 2 10 15 25 66 Final order: 2 8 10 15 25 66

Bubble sort Python challenge 1. Write the bubble sort code into python. Choose 10 random numbers. Print them out in ascending order. 2. Adapt the code so that a user can input the numbers. Store them in an array. Only use 5 numbers. 3. Amend the code so that the numbers are printed out in descending order. Print screen your evidence on the next slide.

Bubble sort Python Challenge - Evidence Print screen your code and output evidence here.

Bubble Sort algorithm Review Question Answer 1. Explain why the bubble sort technique could be regarded as a robust sorting technique? [2 marks] 2. Explain why the bubble sort technique is regarded as an inefficient method of sorting arrays? [2 marks] 3. Suppose you have the following list of numbers to sort: [19, 1, 9, 7, 3, 10, 13, 15, 8, 12] Which list represents the partially sorted list after three complete passes of bubble sort (Highlight the correct answer in RED)? (A) [1, 9, 19, 7, 3, 10, 13, 15, 8, 12] (B) [1, 3, 7, 9, 10, 8, 12, 13, 15, 19] (C) [1, 7, 3, 9, 10, 13, 8, 12, 15, 19] (D) [1, 9, 19, 7, 3, 10, 13, 15, 8, 12] [1 mark]

Merge Sort We now turn our attention to using a divide and conquer strategy as a way to improve the performance of sorting algorithms. The Merge sort technique uses a Divide & Conquer strategy. Merge sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one item, it is sorted by definition (expressed through the base case). If the list has more than one item, the list is split and recursively invokes a merge sort on both halves. Once the two halves are sorted, the fundamental operation, called a merge, is performed. Merging is the process of taking two smaller sorted lists and combining them together into a single, sorted, new list.

Merge Sort and Recursion The Merge Sort algorithm is an example of recursion. Recursion takes place when a rule calls itself. A commonly used example of recursion is that of a rule about ancestry; Grandparent(Ancestor, Descendant) <- parent(ancestor, Person), parent(person, Descendant). GreatGrandparent(Ancestor, Descendant) <- parent(ancestor, Person), parent(person, Descendant). GreatGreatGrandparent(Ancestor, Descendant) <- parent(ancestor, Person), parent(person, Descendant). A clear pattern can be seen which can be expressed in a rule defining the relationship ancestor(ancestor, Descendant); ancestor(ancestor, Descendant) <- parent(ancestor, Person), ancestor(person, Descendant) Or ancestor_of(x,y) <- parent_of(x,z), ancestor_of(z,y) This reads as 'X is an ancestor of Y if X is a parent of Z and Z is an ancestor of Y'. Recursion is evident here as the rule ancestor_of(x,y) calls itself within the rule definition ancestor_of(z,y).

Merge Sort - Python

Merge Sort: Pseudocode

Merge Sort: Pseudocode Note: the symbol <- in pseudocode is used to state that the value to the right is placed in the variable to the left. Base condition Identifies the length of the array, then identifies the halfway point and assigns two new arrays to half the size of the original array. Recursive calls Merge sorted halves Assigns the numbers in the array to the two new arrays. Applies the Mergesort function declared on line 1 (left arrays first, followed by right. Once there is no more numbers to split, the left and right arrays are merged back together.

How it works Watch the following video: https://www.youtube.com/watch?v=tzebrdu-jay

Understanding merge sort 1. In the diagram below, show how a computer would split and merge the numbers in the array below; Splitting 2, 4, 1, 6, 8, 5, 3, 7 2. What order would this occur? Complete the following table; Use the pseudocode and python merge sort examples to assist you. Action Array values Splitting 2, 4, 1, 6, 8, 5, 3, 7 Merging

Merge Sort Python Challenges 1. Write the merge sort code into python. Choose 10 random numbers. Print them out in ascending order. 2. Adapt the code so that a user can input the numbers. Store them in an array. Only use 5 numbers. 3. Amend the code so that the numbers are printed out in descending order. Print screen your evidence on the next slide.

Merge Sort Python Challenges - Evidence Print screen your code and output evidence here.

Merge Sort algorithm Review Question 1. Explain why a programmer would prefer to use a merge sort over a bubble sort. [2 marks] 2. Why would a merge sort be more suitable than a bubble sort with regards to organising large arrays? [2 marks] Answer 3. Given the following list of numbers: [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] which answer illustrates the list to be sorted after 3 recursive calls to Mergesort (Highlight the correct answer in RED)? (A) [16, 49, 39, 27, 43, 34, 46, 40] (B) [21,1] (C) [21, 1, 26, 45] (D) [21] [1 mark] 4. Given the following list of numbers: <br> [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] <br> which answer illustrates the first two lists to be merged (Highlight the correct answer in RED)? (A) [21, 1] and [26, 45] (B) [[1, 2, 9, 21, 26, 28, 29, 45] and [16, 27, 34, 39, 40, 43, 46, 49] (C) [21] and [1] (D) [9] and [16] [1 mark]