Sorting - Selection Sort Cmput 115 - Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.

Slides:



Advertisements
Similar presentations
Selection Sort Wei Guo. Selection sort Algorithm 1 1 st. Find the index of the largest element and exchange the position with the element at the last.
Advertisements

Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
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.
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
 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
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting1 Sorting Order in the court!. sorting2 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of.
1 CSE1301 Computer Programming Lecture 32: List Sorting.
Container Traversal Cmput Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Simple Sorting Algorithms
Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
Vectors Cmput Lecture 4 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from.
Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
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 Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 11 Arrays Continued
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
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.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Introduction to Search Algorithms
Alg2_1c Extra Material for Alg2_1
Algorithm Analysis and Big Oh Notation
Algorithm design and Analysis
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Build Heap and Heap Sort
Algorithm Analysis and Big Oh Notation
Simple Sorting Algorithms
Simple Sorting Algorithms
Presentation transcript:

Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 1/26/00

©Duane Szafron About This Lecture In this lecture we will learn about a sorting algorithm called the Selection Sort. We will study its implementation and its time and space complexity.

©Duane Szafron Outline Selection Sort Algorithm Selection Sort - Arrays Time and Space Complexity of Selection Sort Selection Sort - Vectors

©Duane Szafron The Sort Problem Given a collection, with elements that can be compared, put the elements in increasing or decreasing order

©Duane Szafron Selection Sort Algorithm 1 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index largest last

©Duane Szafron Selection Sort Algorithm 2 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index No exchanges necessary

©Duane Szafron Selection Sort Algorithm 3 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index No exchanges necessary

©Duane Szafron Selection Sort Algorithm 4 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index

©Duane Szafron Selection Sort Algorithm 5 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index

©Duane Szafron Selection Sort Algorithm 6 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index

©Duane Szafron Selection Sort Algorithm 7 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index

©Duane Szafron Selection Sort Algorithm 8 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index

©Duane Szafron Selection Sort Algorithm 9 Stop

©Duane Szafron Sorting Using Arrays Often when we are sorting elements in an Array, the physical size of the array is larger than its logical size (the number of “real” elements it contains). To accommodate this, we add the logical size of the array as an extra parameter to our sorting methods. If we know the logical size and physical size are the same, we can omit this extra parameter. Why wouldn’t we expect them to always be the same?

©Duane Szafron Selection Sort Code - Arrays public static void selectionSort(Comparable anArray[ ], int size) { // pre: 0 <= size <= anArray.length // post: values in anArray are in ascending order intmaxIndex; //index of largest object int index; //index of last unsorted element for (index = size - 1; index > 0; index--) { maxIndex = this.getMaxIndex(anArray, index); this.swap(anArray, index, maxIndex); } code based on Bailey pg. 82

©Duane Szafron Method - getMaxIndex() - Arrays public static int getMaxIndex(Comparable anArray[], int last) { // pre: 0 <= last < anArray.length // post: return the index of the max value in the // given Array up to the last index intmaxIndex; //index of largest object int index; //index of inspected element maxIndex = last; for (index = last - 1; index >= 0; index--) { if (anArray[index].compareTo(anArray[maxIndex]) > 0) maxIndex = index; } code based on Bailey pg. 82

©Duane Szafron Counting Comparisons How many comparison operations are required for a selection sort of an n-element collection? How many comparison operations are required for a selection sort of an n-element collection? getLargest The sort method executes getLargest in a loop for the indexes: k = n-1, n-2, … 1. k comparisons Each time getLargest is executed for an index, k, it does a comparison in a loop for the indexes: k-1, k-2, … 0. This is k comparisons. The total number of comparisons is: (n-1) + (n-2) + … + 1= ( … n) - 1 = n(n + 1) - 1 = O(n 2 ). 2

©Duane Szafron Counting Assignments How many assignment operations are required for a selection sort of an n-element collection? How many assignment operations are required for a selection sort of an n-element collection? The only time we do an assignment is in a reference exchange which takes three assignments. The sort method executes swap() in a loop for the indexes: k = n-1, n-2, … 1. The total number of assignments is: 3*(n-1) = O(n).

©Duane Szafron Time Complexity of Selection Sort independent of the data The number of comparisons and assignments is independent of the data (the initial order of elements doesn’t matter). bestaverageworst Therefore, the best, average and worst case time complexities of the Selection Sort are all O(n 2 ).

©Duane Szafron Space Complexity of Selection Sort Besides the collection itself, the only extra storage for this sort is the single temp reference used in the swap method. Therefore, the space complexity of Selection Sort is O(n).

©Duane Szafron Sorting Using Vectors When we are sorting elements in a Vector, the Vector knows both its logical and physical size so we don’t need any extra parameters in the sort method. However, we must access the elements in a Vector using the message elementAt() and we must cast the elements as we access them. We must modify elements in a Vector using the message setElementAt(). Using access methods adds execution time.

©Duane Szafron Selection Sort Code - Vectors public static void selectionSort(Vector aVector) { // post: values in aVector are in ascending order intmaxIndex; //index of largest object int index; //index of last unsorted element for (index = aVector.size()-1; index > 0; index--) { maxIndex = this.getMaxIndex(aVector, index); this.swap(aVector, index, maxIndex); }

©Duane Szafron Method - getMaxIndex() - Vectors public static int getMaxIndex(Vector aVector, int last) { // pre: 0 <= last < aVector.size // post: return the index of the max value in the // given Vector up to the last index //index of largest object intmaxIndex; //index of largest object //index of inspected element int index; //index of inspected element maxIndex = last; for (index = last - 1; index >= 0; index--) { if (((Comparable) aVector.elementAt(index)).compareTo( (Comparable) aVector.elementAt(maxIndex)) > 0) maxIndex = index; } }