Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.

Slides:



Advertisements
Similar presentations
Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search.
Advertisements

Consider an array of n values to be sorted into ascending order. Sorting.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
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.
Back to Sorting – More efficient sorting algorithms.
Garfield AP Computer Science
I can order decimal numbers up to three decimal places (4a)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 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 - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Selection Sorting Lecture 21. Selection Sort Given an array of length n, –In first iteration: Search elements 0 through n-1 and select the smallest Swap.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
Simple Sorting Algorithms
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
COMP 14 Introduction to Programming Miguel A. Otaduy June 4, 2004.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Lecture 08 Sorting. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. – We first looked at.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
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.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Chapter 8 ARRAYS Continued
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Array operations II manipulating arrays and measuring performance.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 19, 2005.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
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.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
1 CSE 373 Sorting 4: Heap Sort, Bucket Sort, Radix Sort, Stooge Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
The Selection Sort Mr. Dave Clausen La Cañada High School.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Sorting Arrays ANSI-C. Selection Sort Assume want to sort a table of integers, of size length, in increasing order. Sketch of algorithm: –Find position.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Starting Out with C++, 3 rd Edition 1 Sorting Arrays.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
Sort Algorithm.
Bohyung Han CSE, POSTECH
Chapter 9: Sorting and Searching Arrays
CS212: Data Structures and Algorithms
Introduction to Search Algorithms
Simple Sorting Algorithms
Sorting array The bubblesort.
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.
Mr. Dave Clausen La Cañada High School
Selection Sort – an array sorting algorithm
Selection sort Given an array of length n,
Mr. Dave Clausen La Cañada High School
Simple Sorting Algorithms
CSE 373 Data Structures and Algorithms
Searching/Sorting/Searching
Workshop for CS-AP Teachers
Chapter 19 Searching, Sorting and Big O
Simple Sorting Algorithms
Presentation transcript:

Selection Sort

Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting at index 0). 3.Finished if at the end of the array. Otherwise, repeat 1 and 2 for the next index.

Selection Sort Example(ascending) – Smallest is 37 – Swap with index – Smallest is 61 – Swap with index – Smallest is 70 – Swap with index – Smallest is 75 – Swap with index 3 Swap with itself – Don’t need to do last element because there’s only one left

Selection Sort Example(ascending) Write out each step as you sort this array of 7 numbers (in ascending order)

Swapping a = b; b = a; //Does this work? – a gets overwritten with b’s data – b get overwritten with the new data in a (same data now as b) Need a temporary variable to store a value while we swap. temp = a; a = b; b = temp;

Selection Sort Code (ascending) public static void selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int minIndex = i; int min = arr[minIndex]; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < min) { minIndex = j; min = arr[minIndex]; } int temp = arr[minIndex]; // swap arr[minIndex] = arr[i]; arr[i] = temp; }

Selection Sort Algorithm (descending) 1.Find largest element (of remaining elements). 2.Swap largest element with current element (starting at index 0). 3.Finished if at the end of the array. Otherwise, repeat 1 and 2 for the next index.

Selection Sort Example(descending) – Largest is 98 – Swap with index – Largest is 84 – Swap with index 1 Swap with itself – Largest is 67 – Swap with index – Largest is 35 – Swap with index – Don’t need to do last element because there’s only one left

Selection Sort Example(descending) Write out each step as you sort this array of 7 numbers (in descending order)

Selection Sort Code (ascending) public static void selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int maxIndex = i; int max = arr[maxIndex]; for (int j = i + 1; j < arr.length; j++) { if (arr[j] > max) { maxIndex = j; max = arr[maxIndex]; } int temp = arr[maxIndex]; // swap arr[maxIndex] = arr[i]; arr[i] = temp; }

Selection Sort Efficiency

Why use it? Memory required is small – Size of array (you’re using this anyway) – Size of one variable (temp variable for swap) Selection sort is useful when you have limited memory available – Inefficient otherwise when you have lots of extra memory Relatively efficient for small arrays