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.

Slides:



Advertisements
Similar presentations
Consider an array of n values to be sorted into ascending order. Sorting.
Advertisements

Garfield AP Computer Science
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
CPS120: Introduction to Computer Science Searching and Sorting.
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.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
C++ Plus Data Structures
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Searching and Sorting Arrays
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.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Sorting Course Lecture Slides 24 May 2010 “The real focus here is bringing.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
CSC220 Data Structure Winter
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 16: Searching, Sorting, and the vector Type.
Searching and Sorting Topics Sequential Search on an Unordered File
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Computer Science Searching & Sorting.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Chapter 7: Sorting Algorithms Insertion Sort. Sorting Algorithms  Insertion Sort  Shell Sort  Heap Sort  Merge Sort  Quick Sort 2.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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.
SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
Selection Sort Given an array[0-N], place the smallest item in the array in position 0, the second smallest in position 1, and so forth. We do thisby comparing.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
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.
Chapter 16: Searching, Sorting, and the vector Type.
Sort Algorithm.
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Sorting Mr. Jacobs.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Sorting Algorithms.
Searching and Sorting Arrays
Chapter 10 Sorting Algorithms
Principles of Computing – UFCFA3-30-1
Algorithm Efficiency and Sorting
Applications of Arrays
Presentation transcript:

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. 2) Now find the smallest element in the subarray a[1]…a[n-1] and swap it with a[1], the second element in the array. 3) Continue this process until just the last two elements remain to be sorted, a[n-2] and a[n-1]. 4) The smaller of these two elements is placed in a[n-2]; the larger in a[n-1]; and the sort is complete.

Selection Sort Example * Data items stored at asterisk and above are sorted in order relative to each other Unsorted Array After 1 st Pass After 2 nd Pass After 3 rd Pass After 4 th Pass

Selection Sort cont…  Before writing the algorithm for this sorting method, note the following. 1) If the array is of length n, we need n-1 steps 2) We must be able to find the smallest number 3) We need to exchange appropriate array items.

Insertion Sort  Think of the first element in the array, a[0], as being sorted with respect to itself.  The array can now be thought of as consisting of two parts, a sorted list followed by an unsorted list.  The idea of insertion is to move elements from the unsorted list to the sorted list one at a time; as each is moved, it is inserted into its correct position in the sorted list.  In order to place the new item, some elements may need to be moved down to create a slot.

Insertion Sort Example Insertion Sort Example Data items stored at asterisk and above are sorted in order relative to each other Data items stored at asterisk and above are sorted in order relative to each other Current element being compared Current element being compared Unsorted Array After 1 st Pass After 2 nd Pass After 3 rd Pass After 4 th Pass *

Bubble Sort  Compares adjacent pairs of items.  Whenever two items are out of order with respect to each other, they are swapped.  After one pass, we are assured that the array will have the item that comes last in order in the final array position.  That is, the last item will “sink” to the bottom of the array, and preceding items will gradually “float” to the top.

Bubble Sort Example Unsorted Array After 1 st Pass After 2 nd Pass After 3 rd Pass After 4 th Pass 54*444 45*2*22 225*1*1 1115*3* 33335* * Items just swapped

Merge Sort  If there is more than one element in the array: 1) Break the array into two halves 2) Mergesort the left half. 3) Mergesort the right half. 4) Merge the two subarrays into a sorted array.  The main disadvantage of mergesort is that it uses a temporary array.

Merge Sort Example

Sequential Search  Assume that you are searching for a key in a list of n elements.  A sequential search starts at the first element and compares the key to each element in turn until the key is found or there are no more elements to examine in the list.  If the list is sorted, in ascending order, say, stop searching as soon as the key is less than the current list element.

Binary Search  *Works only if the array is sorted.  Compare key to middle number of list.  If key is greater, compare key to middle number of greater sub-array.  If key is less, compare key to middle number of smaller sub-array.  Continue steps until key is found or there is not middle number.