instructor: alexander stoytchev cpre 185: intro to problem solving (using c)

67
Instructor: Alexander Stoytchev http://www.ece.iastate.edu/~alexs/classes/ 2009_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Upload: phoebe-newman

Post on 17-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

Administrative Stuff Midterm 2 is next week  Same format as before:  Lab exam during your regular lab time (Oct 27 or Oct 28)  Lecture exam on Oct 28 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).

TRANSCRIPT

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

Instructor: Alexander Stoytchev

http://www.ece.iastate.edu/~alexs/classes/2009_Fall_185/

CprE 185: Intro to Problem Solving

(using C)

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

Administrative Stuff

• HW6 is today (Oct 21) @ 8pm.

• HW7 is due this Friday (Oct 23) @ 8pm.

Page 3: 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 27 or Oct 28) Lecture exam on Oct 28

• 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).

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

Linear Search in a Sorted Array

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

Problem: Find the index of a number in a

sorted array of integers

LinearSearch_InSortedArray.c

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

LinearSearch_InSortedArray.c#include <stdio.h>#include <stdlib.h>#define N 12int main(){

int a[N]= { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91};

int target = 62; //int target = 72;// Try this target next int i, idx=-1;for(i=0; i< N; i++){

if(a[i] == target){

idx=i;break;

}else if(a[i]>target)

break; // we can stop here}if(idx == -1)

printf("Target not found.\n\n");else

printf("Target found at index: %d. \n\n", idx);}

Page 7: Instructor: Alexander Stoytchev  CprE 185: Intro to Problem Solving (using 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?

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

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

[http://web.ics.purdue.edu/~cs154/lectures/lecture011.htm]

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

[http://web.ics.purdue.edu/~cs154/lectures/lecture011.htm]

Example:SuccessfulBinarySearch

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

Example: BinarySearch.c

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

Binary_Search.c#include <stdio.h>#include <stdlib.h>#define N 12

int main(){

int a[N]= { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91}; //sorted in increasing orderint i;int target = 22; //int target = 72; // Try this target nextint idx=-1; // if the target is found its index is stored here

int first=0; // initial values for the three search varaiblesint last= N-1; int mid= (first + last)/2;

while(last >= first) {

if( a[mid] == target){

idx=mid; // Found it!break; // exit the while loop

} else if(a[mid] > target){

// don't search in a[mid] ... a[last]last = mid-1;

}else{

// don't search in a[first] ... a[mid]first = mid +1;

}

// recalculate mid for the next iterationmid = (first + last)/2; // integer division!

} // end of while loop

if(idx == -1)printf("Target not found.\n\n");

elseprintf("Target found at index: %d \n\n", idx);

}

Page 12: Instructor: Alexander Stoytchev  CprE 185: Intro to Problem Solving (using C)
Page 13: Instructor: Alexander Stoytchev  CprE 185: Intro to Problem Solving (using 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)

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

Other Stuff in Chapter 8

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

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]

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

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;

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

Two-Dimensional Arrays

• Initialization when defined:

int Matrix[2][3] = { {3, 4, 5}, {1, -20, 7}};

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

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”);

}

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

Sorting Algorithms

CprE 185: Intro to Problem SolvingIowa State University, Ames, IACopyright © Alexander Stoytchev

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

Chapter 8 (Sorting)

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

Insertion Sort

[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]

0

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

Analogy: Sorting Cards

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

Analogy: Sorting Cards

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

First Iteration

First Iteration(All face up cards are sorted.

End of the first iteration.)

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

Second Iteration

Second Iteration(All face up cards are sorted. End of the second iteration.)

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

Third Iteration

Third Iteration(All face up cards are sorted.

End of the third iteration.)

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

Fourth Iteration

Fourth Iteration(All face up cards are sorted.

End of the fourth iteration.)

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

Fifth Iteration

Fifth Iteration(All face up cards are sorted.

End of the fifth iteration.)

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

Swapping Array Elements

[http://www.scs.ryerson.ca/jpanar/w2006/cps125/example.gifs/ch7/Arr.Swap.gif]

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

First Iteration (array-like swapping)

First Iteration(All face up cards are sorted.

End of the first iteration.)

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

Second Iteration (array-like swapping)

Second Iteration(All face up cards are sorted. End of the second iteration.)

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

[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]

Example:InsertionSort

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

Animations for Insertion Sort

[http://maven.smith.edu/~thiebaut/java/sort/demo.html]

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

Animations of Sorting Algoritms

• http://maven.smith.edu/~thiebaut/java/sort/demo.html

• http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html

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

C code

// Swap a[i] with the smallest element

int temp = a[i];a[i] = a[minIndex];a[minIndex] = temp;

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

Example: InsertionSort.c

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

// Insertion Sort#include <stdlib.h>#define N 6int main() {

int a[N]= { 23, 78, 45, 8, 32, 56}; int i; for(i = 1; i < N; i++) { int j = i; int INS = a[i]; while ((j > 0) && (a[j-1] > INS)) { a[j] = a[j-1]; // shift elements to the right j--; } a[j] = INS; // insert the element } system("pause");

}

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

Selection Sort(Cards Example)

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

Initial Configuration

(search all cards and find the largest)

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

Swap the two cards

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

As before, the swap is performed in three steps.

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

Sorted Unsorted

Among the remaining cardsthe king is the largest.

It will remain in place.

But the algorithm may performSome empty operations(ie., swap it with itself in place)

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

Sorted Unsorted

Among the remaining cardsthe queen is the largest.

It will remain in place.

But the algorithm may performSome empty operations(i.e., swap it with itself in place)

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

Sorted Unsorted

Among the remaining cardsthe Jack is the largest.

It will remain in place.

But the algorithm may performSome empty operations(i.e., swap it with itself in place)

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

As before, the swap is performed in three steps.

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

Sorted Unsorted

We are down to the last card.Because there is only one and Because we know that it is Smaller than all the restWe don’t need to do anything Else with it. This is why the Algorithm goes up to < N-1

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

Sorted

All cards are now sorted.

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

Selection Sort

[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]

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

Example:SelectionSort

[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]

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

Example: SelectionSort.c

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

// Selection Sort#include <stdlib.h>#define N 6int main() {

int a[N]= { 23, 78, 45, 8, 32, 56}; int i,j; // Sort the array using Selection Sort int minIndex; for(i=0; i < N-1; i++) { // find the minimum element in the unsorted part of the array minIndex=i; for(j=i+1; j < N; j++) if(a[j] < a[minIndex]) minIndex = j;

// Swap a[i] with the smallest element int temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; } system("pause");

}

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

Bubble Sort(Cards Example)

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

Initial Configuration

Unsorted

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

First iteration of the outer loop

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

End of the first iteration

(the smallest card is placed last)

SortedUnsorted SortedUnsorted

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

End of the 2nd iteration

(the smallest two cards are placed last)

2nd Iteration

SortedUnsorted

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

End of the 3rd iteration

(the smallest three cards are placed last)

3rd Iteration

SortedUnsorted

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

End of the 4-th iteration

all cards are sorted

4-th Iteration

SortedUnsorted

Sorted

The last card (the Ace)Is automatically sorted.We don’t need to do anything extra.

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

Bubble Sort

[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]

0

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

[http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]

Example:BubbleSort

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

Example: BubbleSort.c

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

// Bubble Sort#include <stdlib.h>#define N 6 int main() {

int a[N]= { 23, 78, 45, 8, 32, 56}; int i, j; // Sort the array using Bubble Sort // Last elements in the array are sorted first (in decreasing order)for(i=0; i < N; i++) { for(j=0; j< N-1-i; j++) if (a[j+1] > a[j]) /* compare the two neighbors */ { int tmp = a[j]; /* swap a[j] and a[j+1] */ a[j] = a[j+1]; a[j+1] = tmp; } }

}

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

Analysis: all three run in O(n2) time

[http://linux.wku.edu/~lamonml/algor/sort/sort.html]

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

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.

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

O(n log n) sorting algorithms

[http://linux.wku.edu/~lamonml/algor/sort/sort.html]

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

Questions?

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

THE END