Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Chapter 9: Searching, Sorting, and Algorithm Analysis
 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 Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Computation for Physics 計算物理概論 Algorithm. An algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing,
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CHAPTER 11 Sorting.
Computer Programming Sorting and Sorting Algorithms 1.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching and Sorting Arrays
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lesson 2. Starter Look at the hand out you have been given. Can you sort the numbers into ascending order. What mental or physical methods did you use.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Bubble Sort Example
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Review 1 Insertion Sort Insertion Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Merge Sort. In plain English: if the size of the array > 1, split the array into two halves, and recursively sort both halves; when the sorts return,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Sort Algorithm.
Searching and Sorting Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Advanced Sorting Methods: Shellsort
Lecture 11 Searching and Sorting Richard Gesick.
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Searching and Sorting Arrays
CHAPTER 9 SORTING & SEARCHING.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Sorting and Searching Algorithms Week 11 DSA

Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures exist which are designed to maintain a sorted arrangement.

Searching introduction Assuming that the amount of data which needs to be searched is small enough to be stored in an array and that the list so produced is in random order. Consider the task of finding an element in a list of integers which is equal to a given integer called a key.

Consider searching the following list for Key = 10 Element number Element content Comparison s 13Key : 1 211Key : 2 36Key : 3 44Key : 4 59Key : 5 65Key : 6 77Key : 7 88Key : 8 910Key : Table 1 Key 10 found in element number 9.

Linear Search. {algorithm 1} Read Key j := 1 While Key <> a[j] And j < 12 j := j + 1 Endwhile If j = 12 Then Print ‘key not found’ Else Print ‘key found’, Key, ‘position’, j Endif

Algorithm 1 assumes a specific length of list to be searched, but algorithm 2 assumes that the length of the list is stored in a variable Upperlimit. This is more general and therefore superior to the previous algorithm

{algorithm 2} Read Key j := 1 While Key <> a[j] And j < Upperlimit + 1 j := j + 1 Endwhile If j = Upperlimit + 1 Then Print ‘key not found’ Else Print ‘key found’, Key, ‘position’, j Endif

Algorithm 2 could be made to execute much more quickly i.e. to be more efficient by introducing a ‘sentinel value’ so that less instructions are executed to do the same work. The reason for this is that in algorithm 2 the While instruction has a two part condition to check i.e. ‘Key <> a[j] And j a[j]’.

{algorithm 3} Read Key j := 1 a[Upperlimit + 1] := Key While Key <> a[j] j := j + 1 Endwhile If j = Upperlimit + 1 Then Print ‘key not found’ Else Print ‘key found’, Key, ‘position’, j Endif

Element number Element content ComparisonsAlg 2 total no. Comparisons Alg 3 total no. Comparisons 13Key : Key : Key : Key : Key : Key : Key : Key : Key :

Binary Split algorithm Successively split search zone into upper and lower part. Search recursively Eventually reach point at which item either found or can be shown to be missing. Only works on pre-sorted data. Research algorithm in own time

Sorting. The purpose for sorting is to facilitate the later search for members of the sorted list because the most efficient searching algorithms require sorted lists. It is therefore vitally important to develop efficient sorting algorithms

Linear Selection( Insertion) Sort. When using the linear selection sort a single pass through the initial list to be sorted will result in the element with the lowest (say) value being moved from this list and entered into its correct place in an output list. The element in the initial list will then be replaced by some arbitrarily large value so that it never has the lowest value again.

Element number Initial listWorking storage (Temp) ComparisonsInitial list after 1 pass Output list 133Temp = Temp : Temp : 36 44Temp : 44 59Temp : 59 65Temp : 65 77Temp : 77 88Temp : Temp : 910 2Temp : 10Temp = Temp : 11Temp = 19999

The algorithm assumes that element number 1 of the initial list is the lowest and Temp is set to that value i.e. 3. Temp is then compared with each successive element in the initial list until the first one which is smaller than Temp is found i.e. element number 10 with value 2. Temp is then set to 2. This process is repeated Eventually Temp is set to 1. Because this value is in the last element in the initial list Temp is transferred to the output list and element number 11 is set to a very high value i.e This process is then repeated 10 more times. The results in the initial and output lists are displayed on the next two slides.

Initial List after successive Passes

Output List after successive Passes

{Insertion Sort algorithm} { a is the initial list and b is the output list} For k = 1 to Upperlimit Temp := a[1] Position := 1 For j = 2 to Upperlimit If Temp > a[j] Then Temp := a[j] Position := j Endif Endfor b[k] := Temp a[Position] := 9999 Endfor

Standard Exchange (Bubble).  Several passes through list  Each pass currently largest element moves to its correct position in the list. That is, pass number 1 moves the largest element into the last position in the list, pass number 2 moves the next largest element into the penultimate position in the list etc.  A pass is made up of a series of nearest neighbour comparisons i.e. the first element is compared with the second. The larger of the two moves to the second position and the smaller to the first.  This is repeated for the second and third elements etc, until the next to last and last elements are compared and swapped as necessary.  If the list has 20 elements then 19 passes will be needed to sort it.

Pass 1Comparisons El no list1:22:33:44:55:66:77:88:99:1010: * *6 * *4 * *9 * *5 * *7 * *8 * *10 * *2 * *1 * * Swap count

Pass no El no Swap Count

{Bubble Sort} For j = 1 to Upperlimit - 1 For k = 1 to Upperlimit – 1 If a[k] > a[k + 1] Then Temp := a[k] a[k] := a[k + 1] a[k + 1] := Temp Endif Endfor

Improved Bubble Sort pass = 0 NoSwitches = 0 While NoSwitches = 0 pass = pass + 1 NoSwitches = 0 For i = 1 To (Upperlimit - pass) If a(i) > a(i + 1) Then NoSwitches = 1 temp = a(i) a(i) = a(i + 1) a(i + 1) = temp End If Next i End While In this case will not do all passes if the list is sorted at an earlier pass. What if list is initially sorted in exactly the opposite order to the intended order? Suggest further improvement.

Next.. Experiment with application of these mechanisms to list of items stored as arrays in VB applications. Implement a search routine and a sort routine. Save in Basic Module. Can we use as generic routines in other applications?