Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient.
Advertisements

Back to Sorting – More efficient sorting algorithms.
Introduction to Computer Science Theory
Garfield AP Computer Science
Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?
Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE.
Visual C++ Programming: Concepts and Projects
HST 952 Computing for Biomedical Scientists Lecture 9.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
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
Algorithm Efficiency and Sorting
Lecture 08 Sorting. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. – We first looked at.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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 Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
Chapter 19 Searching, Sorting and Big O
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Searching and Sorting Topics Sequential Search on an Unordered File
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Chapter 19: Searching and Sorting Algorithms
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Sorting List is rearranged into sorted order How is the sorted order determined? – The ItemType is responsible for determining the key to be used in comparison.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
3 – SIMPLE SORTING ALGORITHMS
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.
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
ARRAYS Lecture void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp;
The Linear and Binary Search and more Lecture Notes 9.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
Searching and Sorting Searching algorithms with simple arrays
May 17th – Comparison Sorts
Warmup What is an abstract class?
Simple Sorting Algorithms
Divide and Conquer.
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Topics Sequential Search on an Unordered File
Algorithm Efficiency and Sorting
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
Simple Sorting Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Searching/Sorting/Searching
Chapter 19 Searching, Sorting and Big O
Simple Sorting Algorithms
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Presentation transcript:

Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE

Q-2 Overview Sorting defined Algorithms for sorting Selection Sort algorithm Efficiency of Selection Sort

Q-3 Sorting

Q-4 Sorting The problem: put things in order Usually smallest to largest: “ascending” Could also be largest to smallest: “descending” Lots of applications! ordering hits in web search engine preparing lists of output merging data from multiple sources to help solve other problems faster search (allows binary search) too many to mention!

Q-5 Sorting: More Formally Given an array b[0], b[1],... b[n-1], reorder entries so that b[0] <= b[1] <=... <= b[n-1] Shorthand for these slides: the notation array[i..k] means all of the elements array[i],array[i+1]...array[k] Using this notation, the entire array would be: b[0..n-1] P.S.: This is not C syntax !

Q-6 Sorting Algorithms Sorting has been intensively studied for decades Many different ways to do it! We’ll look at only one algorithm, called “Selection Sort” Other algorithms you might hear about in other courses include Bubble Sort, Insertion Sort, QuickSort, and MergeSort. And that’s only the beginning!

Q-7 Sorting Problem What we want: Data sorted in order

Q-8 Sorting Problem What we want: Data sorted in order sorted: b[0]<=b[1]<=…<=b[n-1] 0 n b

Q-9 Sorting Problem What we want: Data sorted in order sorted: b[0]<=b[1]<=…<=b[n-1] 0 n b Initial conditions

Q-10 Sorting Problem What we want: Data sorted in order sorted: b[0]<=b[1]<=…<=b[n-1] 0 n b unsorted 0 n b Initial conditions

Q-11 General situation Selection Sort

Q-12 General situation Selection Sort smallest elements, sorted 0 k n b remainder, unsorted

Q-13 General situation Selection Sort smallest elements, sorted 0 k n b remainder, unsorted Step: Find smallest element x in b[k..n-1] Swap smallest element with b[k], then increase k

Q-14 General situation Selection Sort smallest elements, sorted 0 k n b remainder, unsortedsmallest elements, sorted 0 k n b x Step: Find smallest element x in b[k..n-1] Swap smallest element with b[k], then increase k

Q-15 General situation Selection Sort smallest elements, sorted 0 k n b remainder, unsortedsmallest elements, sorted 0 k n b x Step: Find smallest element x in b[k..n-1] Swap smallest element with b[k], then increase k

Q-16 Subproblem: Find Smallest

Q-17 Subproblem: Find Smallest /* Find location of smallest element in b[k..n-1] */ /* Assumption: k < n */ /* Returns index of smallest, does not return the smallest value itself */

Q-18 Subproblem: Find Smallest /* Find location of smallest element in b[k..n-1] */ /* Assumption: k < n */ /* Returns index of smallest, does not return the smallest value itself */ int min_loc (int b[ ], int k, int n) { int j, pos; /* b[pos] is smallest element */ /* found so far */ pos = k; for ( j = k + 1; j < n; j = j + 1) if (b[j] < b[pos]) pos = j; return pos; }

Q-19 Code for Selection Sort /* Sort b[0..n-1] in non-decreasing order (rearrange elements in b so that b[0]<=b[1]<=…<=b[n-1] ) */ void sel_sort (int b[ ], int n) { int k, m; for (k = 0; k < n - 1; k = k + 1) { m = min_loc(b,k,n); swap(&a[k], &b[m]); }

Q b Example

Q b min_loc Example

Q b b min_loc Example

Q b b min_loc Example

Q b b b min_loc Example

Q b b b min_loc Example

Q b Example (cont.)

Q b min_loc Example (cont.)

Q b b min_loc Example (cont.)

Q b b min_loc Example (cont.)

Q b b b min_loc Example (cont.)

Q b b b min_loc Example (cont.)

Q-32 Example (concluded) b

Q-33 Example (concluded) b min_loc

Q-34 Example (concluded) b b min_loc

Q-35 Sorting Analysis How many steps are needed to sort n things? For each swap, we have to search the remaining array length is proportional to original array length n Need about n search/swap passes Total number of steps proportional to n 2 Conclusion: selection sort is pretty expensive (slow) for large n

Q-36 Can We Do Better Than n 2 ? Sure we can! Selection, insertion, bubble sorts are all proportional to n 2 Other sorts are proportional to n log n Mergesort Quicksort etc. log n is considerably smaller than n, especially as n gets larger As the size of our problem grows, the time to run a n 2 sort will grow much faster than a n log n one.

Q-37 Any better than n log n? In general, no. But in special cases, we can do better Example: Sort exams by score: drop each exam in one of 101 piles; work is proportional to n Curious fact: efficiency can be studied and predicted mathematically, without using a computer at all! This branch of mathematics is called complexity theory and has many interesting, unsolved problems.

Q-38 Comments about Efficiency Efficiency means doing things in a way that saves resources Usually measured by time or memory used Many small programming details have little or no measurable effect on efficiency The big differences comes with the right choice of algorithm and/or data structure

Q-39 Summary Sorting means placing things in order Selection sort is one of many algorithms At each step, finds the smallest remaining value Selection sort requires on the order of n 2 steps There are sorting algorithms which are greatly more efficient It’s the algorithm that makes the difference, not the coding details