Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
Sorting. Sorting Considerations We consider sorting a list of records, either into ascending or descending order, based upon the value of some field of.
Visual C++ Programming: Concepts and Projects
Sorting1 Sorting Order in the court!. sorting2 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Simple Sorting Algorithms
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Algorithm Efficiency and Sorting
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]
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Upcoming schedule F 11/12 hw#4 due F 11/19 hw#5 due F 11/25 midterm #2.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
UNIT 18 Searching and Sorting.
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.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
© 2004 Pearson Addison-Wesley. All rights reserved October 17, 2007 Searching (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2004 Pearson Addison-Wesley. All rights reserved October 19, 2007 Sorting ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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.
3 – SIMPLE SORTING ALGORITHMS
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
© 2004 Pearson Addison-Wesley. All rights reserved March 10, 2006 Sorting ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Data Structures and Algorithms Lecture 17, 18 and 19 (Sorting) Instructor: Quratulain Date: 10, 13 and 17 November, 2009 Faculty of Computer Science, IBA.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Sorting Chapter 14.
CS1010 Programming Methodology
Growth of Functions & Algorithms
CprE 185: Intro to Problem Solving (using C)
Lecture 14 Searching and Sorting Richard Gesick.
Instructor: Alexander Stoytchev
CS1010 Programming Methodology
Instructor: Alexander Stoytchev
Sorting Data are arranged according to their values.
CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev
Sorting October 20, 2006 ComS 207: Programming I (in Java)
CSC215 Lecture Algorithms.
CprE 185: Intro to Problem Solving (using C)
Sorting Data are arranged according to their values.
Lecture 11 Searching and Sorting Richard Gesick.
Sorting.
CprE 185: Intro to Problem Solving (using C)
UNIT – IV Searching – Linear search - binary search Sorting
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Administrative Stuff Midterm 2 is next week!!! Same format as before:  Lab exam during your regular lab time (Oct 28 or Oct 29)  Lecture exam on Oct 29 (10-11am)  Note: There will be NO night exam. The exam will be cumulative with emphasis on conditional statements (if, if-else, switch), loops (do, while, for), arrays (to be covered), and searching and sorting algorithms (to be covered).

Administrative Stuff HW7 is out today. It’s due next 8pm. HW6 is due this Friday (Oct 8pm.

Other Stuff in Chapter 8

Two-Dimensional Arrays int Matrix[2][3]; Defines the following elements:  Row1: Matrix[0][0], Matrix[0][1], Matrix[0][2]  Row2: Matrix[1][0], Matrix[1][1], Matrix[1][2]

Two-Dimensional Arrays int Matrix[2][3]; Initialization: Matrix[0][0]=3; Matrix[0][1]=4; Matrix[0][2]=5; Matrix[1][0]=1; Matrix[1][1]= -20; Matrix[1][2]=7;

Two-Dimensional Arrays Initialization when defined: int Matrix[2][3] = { {3, 4, 5}, {1, -20, 7};

Two-Dimensional Arrays 2D arrays and nested for loop int Matrix[2][3] = { {3, 4, 5}, {1, -20, 7}}; int i,j; for(i=0; i< 2; i++) { for(j=0; j<3; j++) printf(“%3d ”, Matrix[i][j]); printf(“\n”); }

Sorting Algorithms CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Quick Review of the Last Lecture

Problem: Find the minimum number in an array of unsorted integers find_minimum.c

Search [

Linear Search The most basic Very easy to implement The array DOESN’T have to be sorted All array elements must be visited if the search fails Could be very slow

Example: Successful Linear Search [ Example: Successful Linear Search

[ Example: Failed Linear Search

Problem: Find the index of a number in an unsorted array of integers linear_search.c

Problem: Find the all occurrences of a number in an array and replace it with a new value. search_and_replace.c

Linear Search in a Sorted Array

Problem: Find the index of a number in a sorted array of integers LinearSearch_InSortedArray.c

Analysis If the list is unsorted we have to search all numbers before we declare that the target is not present in the array. Because the list is sorted we can stop as soon as we reach a number that is greater than our target Can we do even better?

Binary Search At each step it splits the remaining array elements into two groups Therefore, it is faster than the linear search Works only on an already SORTED array Thus, there is a performance penalty for sorting the array [

Example: Successful Binary Search

Example: BinarySearch.c

Analysis of Searching Methods For an array of size n Sequential Search (Average-Case) n/2 Sequential Search (Worst-Case) n Binary Search (Average-Case) log(n)/2 Binary Search (Worst-Case) log(n)

Chapter 8 (Sorting)

Insertion Sort [ 0

Analogy: Sorting Cards

[ Example: Insertion Sort

Animations for Insertion Sort [

Animations of Sorting Algoritms

Swapping Array Elements [

C code // Swap a[i] with the smallest element int temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp;

Example: InsertionSort.c

Selection Sort [

Example: Selection Sort [

Example: SelectionSort.c

Bubble Sort [ 0

Example: Bubble Sort

Example: BubbleSort.c

Analysis: all three run in O(n 2 ) time [

Analysis There are faster sorting algorithms  Heap sort  Quick sort  Merge Sort We will not cover those but feel free to study them on your own. They run in O(n log n) time.

O(n log n) sorting algorithms [

Questions?

THE END