Python: Sorting – Selection Sort

Slides:



Advertisements
Similar presentations
Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
Advertisements

Algorithmics - Lecture 21 LECTURE 2: Algorithms description - examples -
Searching Damian Gordon. Google PageRank Damian Gordon.
Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Lists in Python Selection Sort Algorithm. Sorting A very common activity for computers Not just in business, sorting is used in databases, graphics, simulations,
Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways.
Data Structures: Multi-Dimensional Arrays Damian Gordon.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Python: File Management Damian Gordon. File Management We’ve seen a range of variable types: – Integer Variables – Real Variables – Character Variables.
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
Zoom-In: Broadway and Film Posters Through the Ages.
Python: Exception Handling Damian Gordon. Exception Handling When an error occurs in a program that causes the program to crash, we call that an “exception”
Sort Algorithm.
Introduction to Python
Lecture 5 of Computer Science II
Linear Algebra review (optional)
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Python: Programming the Google Search
Two-dimensional arrays
Merging Merge. Keep track of smallest element in each sorted half.
Python: Advanced Sorting Algorithms
Linked Lists Damian Gordon.
CS150 Introduction to Computer Science 1
Chapter 8 Arrays Objectives
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Main Points: - More Fundamental Algorithms on Arrays.
C Passing arrays to a Function
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Selection Sort Sorted Unsorted Swap
Boolean Logic Damian Gordon.
Compound Data CSCE 121 J. Michael Moore.
Lists in Python.
Arrays And Functions.
Python: Array Slicing Damian Gordon.
Chapter 8 Arrays Objectives
Straight Selection Sort
Lab 4: C Array Review: Array Lab assignments.
CSCI N207 Data Analysis Using Spreadsheet
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Topics discussed in this section:
24 Searching and Sorting.
Algorithmic Complexity
Simple Statistics on Arrays
Simple Statistics on Arrays
Some Common Issues: Iteration
Given value and sorted array, find index.
Multidimensional array
Sorting Arrays Chapter 10
Sorting Damian Gordon.
ARRAYS 2 GCSE COMPUTER SCIENCE.
A G L O R H I M S T A Merging Merge.
Lets Play with arrays Singh Tripty
Chapter 8 Arrays Objectives
Multi-Dimensional Arrays
2-Dimensional Lists (Matrices) in Python
Linear Algebra review (optional)
Introduction to Computer Science
CS1100 Computational Engineering
Sorting.
A G L O R H I M S T A Merging Merge.
A G L O R H I M S T A Merging Merge.
Insertion Sort.
COMS 261 Computer Science I
Insertion Sort Array index Value Insertion sort.
6 ages are listed from smallest to biggest
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

Python: Sorting – Selection Sort Damian Gordon

Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways that’s more natural for a person to understand. It’s called SELECTION SORT.

Sorting: Selection Sort It works as follows: Find the smallest number, swap it with the value in the first location of the array Find the second smallest number, swap it with the value in the second location of the array Find the third smallest number, swap it with the value in the third location of the array Etc.

# PROGRAM SelectionSort: Age = [44, 23, 42, 33, 18, 54, 34, 16] for outerindex in range(0,len(Age)): # DO MinValLocation = outerindex for index in range(outerindex,len(Age)): if Age[index] < Age[MinValLocation]: # THEN MinValLocation = index # ENDIF; # ENDFOR; if MinValLocation != outerindex: Age[outerindex], Age[MinValLocation] = Age[MinValLocation], Age[outerindex] print(Age) # END.

Python: Multi-dimensional Arrays Damian Gordon

Multi-dimensional Arrays We declare a multi-dimensional array as follows: Ages = [[0 for x in range(8)] for x in range(8)]

Multi-dimensional Arrays Or like this: Ages = [[2,6,3],[7,5,9]]

Multi-dimensional Arrays To print out the whole array, I can say: print(Ages)

Multi-dimensional Arrays To print out the first value in the array: print(Ages[0][0])

Multi-dimensional Arrays To assign a new value to the first element in the array: Ages[0][0] = 34

Multi-dimensional Arrays If we wanted to add 1 to each cell:

# PROGRAM Add1ToMatrix: Ages = [[2,4,7],[3,6,3]] for n in range(0,2): # DO for m in range(0,3): Ages[n][m] = Ages[n][m] + 1 # ENDFOR; print(Ages) # END.

Multi-dimensional Arrays If we want to add up all the values in the array:

# PROGRAM TotalOfMatrix: Ages = [[2,4,7],[3,6,3]] print(Ages) total = 0 for n in range(0,2): # DO for m in range(0,3): total = total + Ages[n][m] # ENDFOR; print("The total value of the matrix is", total) # END.

etc.