C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

CSE Lecture 3 – Algorithms I
Jun 23, 2014IAT 2651 Binary Search Sorting. Jun 23, 2014IAT 2652 Search  Frequently wish to organize data to support search –Eg. Search for single item.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
HST 952 Computing for Biomedical Scientists Lecture 9.
System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
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.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
1 Searching and Sorting Linear Search Binary Search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
 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.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
CSC 211 Data Structures Lecture 13
Data Structure Introduction.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
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 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.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
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.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Data Structures Arrays and Lists Part 2 More List Operations.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and 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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Searching and Sorting Arrays
CHAPTER 9 SORTING & SEARCHING.
Presentation transcript:

C# PROGRAMMING Searching & Sorting

Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures to sort and search arrays. (6%)

Searching vs. Sorting When you search an array, it is to determine if a specified value is present.  There are two primary searching algorithms 1. Linear Search 2. Binary Search Sorting is done to order the values in the array based upon some key value.  There are three primary sorting algorithms 1. Selection Sort 2. Insertion Sort 3. Merge Sort

C# PROGRAMMING Searching Algorithms

Linear Search A linear search algorithm searches the array in a sequential manner. The algorithm moves through the array, comparing the key value with the values of the elements. If it does not find the key value, it simply moves to the next element intArray

Linear Search for (int index = 0; index < intArray.Length; index++) { if (array [index] == searchValue) return index; return -1; } Note: The Loop will iterate until the value is found or it reaches the end.

Binary Search The binary search algorithm is more efficient than the linear search algorithm. Binary search requires that the array be sorted first. The algorithm splits the array and checks the middle value.  If it is not found it compares the values.  If the search value is higher than the middle value, the algorithm moves to the upper half (now a subarray). (Lower – it moves to the lower half.  It splits the subarray in half, checks the middle for a match.  If not found, it checks to see if it is higher/lower and moves to appropriate subarray.  This continues until it has no more values or finds a match.

Binary Search Binary Search Code int low = 0;//starts with beginning position at 0 int high = arr.Length – 1;//starts with end position at last element int mid = (low + high + 1)/2;//calculates middle position int index = -1;//sets index to -1, not found at beginning do { if (searchVal == arr[mid])//checks middle index = mid; else if (searchVal arr[mid])//sets subarray if searchVal > mid low = mid + 1; mid = (low + high + 1)/2;//recalculate middle value } while ((low <= high) && (index ==-1))//continues loop while not found return index;

C# PROGRAMMING Sorting Algorithms

Selection Sort This is a simple sorting algorithm. It moves through the array looking for the lowest value, then moves it to the front. The second pass through the array, it looks for the second lowest and moves it to the second position. It keeps passing through the array until the last iteration.

Selection Sort

int smallestVal; for ( int i = 0; i < arrName.Length – 1; i++) { smallestVal = i; for ( int index = i + 1; index < arrName.Length; index ++) { if ( arrName [ index ] < arrName [ smallestVal ]) smallestVal = index; int temp = arrName [ i ]; data [ i ] = data [ smallestVal ]; data [ smallestVal ] = temp; }

Insertion Sort Another simple sorting algorithm. 1 st Iteration – Compares element 1 & 2 – Swaps if element 1 > element 2. 2 nd Iteration – Looks at element 3 – Inserts it in position given element 1 & 2. It keeps comparing and inserting until the end.

Insertion Sort

arr = new int [size]; int insertVal; for(int next = 1; next < arr.Length; next ++) { insertVal = arr[next];//hold value int move = next;//initialize index pos to put element while (move > 0 && arr [move – 1] > insert) { arr [move ] = arr [move – 1]; move --; } arr [move] = insertVal;//insert value }

Merge Sort The merge sort algorithm sorts the array by splitting the array into two subarrays, sorting the subarrays, then merging them back together sorted

Measuring Efficiency How efficiency an algorithm is can be measured using Big-O notation. Big-O notation measure the worst-case runtime for an algorithm.

Array Class There are methods in the Array class that can be used to search and sort an array. You can sort an array in ascending order as follows Array.Sort(arrName); You can sort an array in descending order as well. Array.Reverse(arrName);

Array Class You can also use the BinarySearch method to perform a binary search.  Remember the array has to be sorted first.  This method returns the index position of the found item, if not found, -1. Example intPosition = Array.BinarySearch(arrName, search);

ArrayList Class There are methods in the ArrayList class that can be used to search and sort an ArrayList. You can sort an array in ascending order as follows ArrListName.Sort(); You can sort an ArrayList in descending order as well. ArrListName.Reverse();

ArrayList Class You can also use the BinarySearch method to perform a binary search with an ArrayList.  Remember the ArrayList has to be sorted first.  This method returns the index position of the found item, if not found, -1. Example intIndex = ArrListName.BinarySearch(searchObj);

Check Your Understanding Given the following code, what will be the content of the array after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); chest.Add(Potion); chest.Add(Potion); chest.Add(Life); chest.Remove(Potion); chest.Reverse();  Potion, Life

Check Your Understanding Given the following code, what will be the value of Location after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); Treasure.Add(Potion); Treasure.Add(Potion); Treasure.Add(Life); Treasure.Remove(Potion); int Location = Treasure.BinarySearch(Life); Location = 1

Check Your Understanding Given the following code, what will be the value of Location after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); Treasure.Add(Potion); Treasure.Add(Potion); Treasure.Add(Life); Treasure.Remove(Potion); int Location = Treasure.BinarySearch(Heart); Location = -1

Check Your Understanding Given the following code, what will be the contents of the array myHealth after execution? intPlayerPoints = new int [] {451, 249, -377} Array.Sort(intPlayerPoints);  -377, 249, 451

Check Your Understanding Given the following code, what will be the value of result after execution? int myLevelPoints = new int [] {203, 478, 1785} Array.Sort(myLevelPoints ); int result = Array.BinarySearch(myLevelPoints, 1785);  2

Check Your Understanding Given the following code, what would be the values in the sub-arrays after the second iteration using a MergeSort? int myLevels = new int [] {211, 103, 15, 49, 73, 172}  //first  //second

Searching & Sorting For more information on the Array class. us/library/czz5hkty.aspx us/library/czz5hkty.aspx For more information on the ArrayList class. us/library/system.collections.arraylist.aspx us/library/system.collections.arraylist.aspx