February 4, 2005 Searching and Sorting Arrays. Searching.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
MATH 224 – Discrete Mathematics
Introduction to Computer Science Theory
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Simple Sorting Algorithms
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
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.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Array operations II manipulating arrays and measuring performance.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
UNIT 18 Searching and Sorting.
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 UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Lecture 16: Searching and Sorting Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
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.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CSCI 51 Introduction to Programming March 12, 2009.
3 – SIMPLE SORTING ALGORITHMS
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski.
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.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Data Structures Arrays and Lists Part 2 More List Operations.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
The Linear and Binary Search and more Lecture Notes 9.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Starting Out with C++, 3 rd Edition 1 Sorting Arrays.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
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 and Sorting Arrays
Searching and Sorting Arrays
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
Sorting Algorithms.
Topic 14 Searching and Simple Sorts
Algorithm design and Analysis
CSC215 Lecture Algorithms.
Searching and Sorting Arrays
Chapter 8 – Searching and Sorting Arrays
Topic 14 Searching and Simple Sorts
Searching and Sorting Arrays
Presentation transcript:

February 4, 2005 Searching and Sorting Arrays

Searching

The Problem Given an array of elements and a particular element, determine whether that element is in the array.

Linear Search One obvious approach is to simply check every element in the array. Pros: conceptually simple easy to implement Cons: slow for large arrays

int linearSearch(int list[], int numElements, int value) { int index=0; int position = -1; bool found = false; while (index < numElements && !found) { if (list[index] == value) { found = true; position = index; } index++; } return position; }

Binary Search The binary search is cleverer than linear search. Pros: Fast Fairly easy to implement Cons: Values in array need to be in order

int binarySearch(int array[], int numElems, int value) { int first = 0; int last = numElems-1; int middle; int position = -1; bool found = false; while (!found && first <=last) { middle = (first+last)/2; //calculate middle point if (array[middle]==value) { found = true; position = middle; } else if (array[middle]>value) { last = middle-1; } else { first = middle +1; } return position; }

Sorting

The Problem Given an array of elements, put them in order.

There are many ways to do this. The various ways differ in execution speed, conceptual simplicity and in how easy they are to implement.

Bubble Sort Pro: Simplest way to sort Con: Slowest way

The Idea First put the elements one and two in order. Then put elements two and three in order. Then put elements three and four in order. Etc. After this process is over, you can be assured of one thing – The largest element is in the last position.

The Idea Now forget about the largest element and repeat the previous process on the remaining elements. After n-1 times, where n is the number of elements in the array, the array will be sorted.

Of course it may be sorted before n-1 times, but you can’t guarantee that. We say n-1 times is the worst case scenario.

#include using namespace std; void bubbleSort(int array[], int elems) { bool swap; int temp; do { swap = false; for (int count =0; count < (elems-1); count++) { if (array[count] > array[count+1]) { temp = array[count]; array[count]=array[count+1]; array[count+1]=temp; swap=true; } } while (swap); } int main() { int values[] = {2,7,3,6,1}; bubbleSort(values, 5); for (int i=0; i<5; i++) { cout << values[i] << " "; } cout << endl; system("PAUSE"); return 0; }

Application to 3n+1 Problem Statement of Problem: Take a positive integer. If it is even, divide it by 2. If it is odd, multiply it by 3 and add 1 Do this recursively: Stop if you get to 1. e.g. 5 -> 16 -> 8 -> 4 -> 2 -> 1

Examples 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 We can stop here because we did 5 already 9->28->14 -> 7 -> 22 -> 11 -> 34 -> 17 The sequence for 27 takes 111 steps to get to 1

Question: Is there a number that does not go to 1. This question was first asked in It remains unsolved. All numbers up to 2 * 2^58 have been checked as of December 16, 2004

If there are no such numbers, the problem is to prove it. Some have said this problem is to hard for modern day mathematics and computer science.

Application of Searching to 3n+1 Write a function that takes a positive integer as an input and returns the number of steps it takes to get to 1. If it doesn’t go to 1 (!), the function will never return. Then create an array of one million random integers, and a parallel array of the number of steps to 0 for that random integer. Using a search algorithm, you can investigate what sorts of numbers are in the second array. Perhaps there is a pattern.

Next Monday Faster sorting algorithms