Data Structure & Algorithm Lecture 1 – Selection & Insertion Sort JJCAO Steal some from Prof. Yoram Moses.

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

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?
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
Introduction to Sorting
1 CSE1301 Computer Programming: Lecture 28 List Sorting.
Simple Sorting Algorithms
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Computer Programming Sorting and Sorting Algorithms 1.
Algorithm Efficiency and Sorting
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Analysis of Algorithms CS 477/677
Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.
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.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Lecture 2 MAS 714 Hartmut Klauck
COMP s1 Computing 2 Complexity
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
1 MT258 Computer Programming and Problem Solving Unit 9.
Introduction to Sorting. What is Sorting? Sorting: an operation that segregates items into groups according to specified criterion. A = {
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Course Web Page Most information about the course (including the syllabus) will be posted on the course wiki:
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Jessie Zhao Course page: 1.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
1 Data Structures and Algorithms Sorting I Gal A. Kaminka Computer Science Department.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Data Structure Introduction.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
Data Structure & Algorithm
CSCI 51 Introduction to Programming March 12, 2009.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
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.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms.
Copyright Prentice Hall Modified by Sana odeh, NYU
CSCE 210 Data Structures and Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Hashing Exercises.
Teach A level Computing: Algorithms and Data Structures
Searching and Sorting Linear Search Binary Search ; Reading p
Bubble, Selection & Insertion sort
Lecture 11 Searching and Sorting Richard Gesick.
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Presentation transcript:

Data Structure & Algorithm Lecture 1 – Selection & Insertion Sort JJCAO Steal some from Prof. Yoram Moses

The Sorting Problem 2

Why is sorting worth so much attention? 1.Sorting is basic building block 2.Historically, ¼ mainframe cycles were spent sorting data [Knu98]. It remains most ubiquitous in practice. 3

Algorithm Efficiency 4

5 Applications of Sorting - Searching Binary search tests whether an item is in a dictionary in O(log n) time, provided the keys are all sorted

Applications of Sorting - Closest pair Given a set of n numbers, how do you find the pair of numbers that have the smallest difference between them? Once the numbers are sorted, the closest pair of numbers must lie next to each other somewhere in sorted order. Thus, a linear-time scan through them completes the job, for a total of O(n log n) time including the sorting. For point cloud processing: k-nearest neighbors or neighbors in ball 6

Applications of Sorting – Convex Hall rubber band stretched over the points Collision detection in Game, etc. The total time is linear after the sorting has been done (by x-coordinate) 7 [Skiena2] ch4.1

Internet 8 e&query=junjie+cao&Submit=Search

Why review standard sorting when you are better off not implementing them and using built-in library functions instead? 1.Most of interesting ideas appear in sorting: – divide-and-conquer, data structures, randomized algorithms 1.Typical computer science students study the basic sorting algorithms at least three times before they graduate: first in introductory programming, then in data structures, and finally in their algorithms course. 2.Sorting is thoroughly studied. Dozens of diff algorithms exist, most of which possess some particular advantage over others in certain situations. 9

Sorting!! It is a rare application where the running time of sorting proves to be the bottleneck Never be afraid to spend time sorting, provided you use an efficient sorting routine O(nlogn) Sorting lies at the heart of many algorithms. Sorting the data is one of the first things any algorithm designer should try in the quest for efficiency. 10

Sorting Could you think of an algorithm now? | | |

First Idea in Our Head sort A for i = 1:n [B[i], pos] <- min A A.remove(pos) end 12 min A for i = 1:n if tmpv > A[i] tmpv = A[i] tmpi = i end A.remove(pos) A.Size <- A.size-1 for i = pos:A.size A[i] <- A[i+1] end

Selection Sort (When no additional storage space is allowed) for i = 1:n, k = i //invariant: a[k] smallest of a[i..n] for j = i+1:n if a[j] < a[k] then k = j //invariant: a[1..i] in final position swap a[i,k] end 13

Several Sort Algorithms 14

Selection Sort - Complexity analysis Selection sort performs n iterations, where the average iteration takes n/2 steps, for a total of O(n 2 ) time Number of swaps may vary from zero (in case of sorted array) to n - 1 (in case array was sorted in reversed order), which results in O(n) number of swaps. So selection sort requires n - 1 number of swaps at most, makes it very efficient in situations, when write operation is significantly more expensive, than read operation. 15 for i = 1:n, k = i for j = i+1:n if a[j] < a[k] then k = j swap a[i,k] end

Insertion Sort 16 1.Although O(n 2 ), it is applied in practice for sorting a small number of elements (8-12 elements). 2.Outperforms most of quadratic sorting algorithms, like selection & bubble 3.works the way many people sort a hand of playing cards 4.Somewhat resembles selection sort

17 1.sorted one and unsorted one 2.At the beginning, sorted part contains first element of the array and unsorted one contains the rest. 3.At each step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one.

Ideas of Insertion 18 for i = 2:n, //invariant: a[1..i] is sorted end for (k = i; k > 1 and a[k] < a[k-1]; k--) swap a[k,k-1] end Next implementation eliminates those unnecessary writes.

Shifting instead of swapping 19 [Lafore24]

Shifting instead of swapping 20

Selection & Insertion Sort 21 for i = 1:n, k = i for j = i+1:n if a[j] < a[k] then k = j swap a[i,k] end

Pseudo Code A schematic description of the algorithm which can be easily translated to a “real” programming language – Common in the literature – Easily translated into real code – More information can be found in [CLRS] book 22

Pseudo Code – Basic Operators “ ← ” – assignment operator “for”, “while”, “repeat-until” – loop operators “if-else”, “case” – condition operators “return” – return from procedure Lots of other operators with their meaning obvious from their names, e.g., – “swap”, “delete”, “new”, ’==‘ etc. 23

Pseudo Code - Conventions Indentation indicates block structure Variables are local to a given procedure A[i] denotes the i-th element of the array A – A[i…j] denotes the subarray containing elements A[i], A[i+1],…, A[j] An empty pointer value is denoted by NIL Fields (and methods) in the composite objects are accessed by “.”, – e.g. Person.id, Person.FirstName, Person.FamilyName, etc. 24