1 lecture 22:applications of arrays introduction to computer science spring 2006

34
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

Post on 22-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

1

Lecture 22:Applications of Arrays

Introduction to Computer Science

Spring 2006

Page 2: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

2

Contents

Review of Arrays Application of Arrays

Page 3: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

3

Contents

Review of Arrays Application of Arrays

Page 4: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

4

Arrays Array - a collection of a fixed number of elements

All the elements of an array must be the same data type; The elements of an array are stored sequentially in memory.

One-dimensional array - an array in which the components are arranged in a list form

The syntax for declaring a one-dimensional array is:

dataType arrayName[intExp]; where intExp is any expression that evaluates to a positive integer

Example: int num[5];

Page 5: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

5

#include <iostream>#include <iomanip>using namespace std;const int arraySize = 10;

void fill_Array(double list[], int listSize) {

for(int i=0; i<listSize; ++i) cin>>list[i];

}

void print_Array(double list[], int listSize){

for(int i=0; i<listSize; ++i) cout<<setw(5)<<list[i];

}

void copy_Array(const double listOne[], double listTwo[], int listOneSize) {

for(int i=0; i<listOneSize; ++i) listTwo[i] = listOne[i];

}

int indexLargestElement(double list[], int listSize){

int maxIndex = 0;for (int i = 0; i < listSize; i++) if ( list[maxIndex] < list[i] ) maxIndex = i;

return maxIndex; }int main(){

double listA[arraySize]={0};double listB[arraySize];

fill_Array(listA, arraySize);print_Array(listA, arraySize);copy_Array(listA, listB, arraySize);cout<<“The position of the largest element in listA is: ” << indexLargestElement(listA, arraySize);

}

Page 6: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

6

Two-Dimensional Arrays Two-dimensional Array: a collection of a fixed number of

components arranged in two dimensions The syntax for declaring a two-dimensional array is:

dataType arrayName[intexp1][intexp2];where intexp1 and intexp2 are expressions yielding positive integer values

Example: double sales[10][5];

Page 7: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

7

Accessing Array Components The syntax to access a component of a two-dimensional array is:

arrayName[indexexp1][indexexp2]where indexexp1 and indexexp2 are expressions yielding nonnegative integer values

Example: sales[5][3]=25.75;

Page 8: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

8

Processing Two-Dimensional Arrays : Row processing and Column processing

We can process a particular row or column of a two-dimensional array as a one-dimensional array

use algorithms similar to processing one-dimensional arrays

For example:

the following for loop initializes row four to zero:row = 4;for(col = 0; col < columns; col++) matrix[row][col] = 0;

The following for loop inputs data in row 4 of matrix:row = 4;for(col = 0; col < columns; col++) cin>>matrix[row][col];

Page 9: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

9

Processing Two-Dimensional Arrays : Entire Array

We can process the entire array by using nested for loop

Example: The following nested for loop initialize the entire

matrix:

for(row = 0; row < rows; row++){

for(col = 0; col < columns; col++){ matrix[row][col] = 0;}

}

Page 10: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

10

#include <iostream>#include <iomanip>using namespace std;const int MAXI = 10;const int MAXJ = 15;

void init_matrix(double matrix[MAXI][MAXJ]) {

for(int i=0; i<MAXI; ++i) for(int j=0; j<MAXJ; ++j)

matrix[i][j]=i+j;}

void print_matrix(double matrix[MAXI][MAXJ]){

for(int i=0; i<MAXI; ++i){ for(int j=0; j<MAXJ; ++j)

cout<<setw(5)<<matrix[i][j]; cout<<endl;}

}

double sum_matrix(double matrix[MAXI][MAXJ]){

double sum=0.0;for(int i=0; i<MAXI; ++i) for(int j=0; j<MAXJ; ++j)

sum = sum + matrix[i][j];return sum;

}

int main(){

double matrix[MAXI][MAXJ];double sum;

init_matrix(matrix);print_matrix(matrix);sum = sum_matrix(matrix);

cout<<"The sum of all the elements of matrix is "<<sum<<endl;}

Page 11: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

11

Contents

Review of Arrays Application of Arrays

sequential search algorithm bubble sort algorithm selection sort algorithm binary search algorithm

Page 12: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

12

List Processing

List: a set of values of the same type

Basic list operations:

1. Search for a given item

2. Sort the list

3. Insert an item in the list

4. Delete an item from the list

Page 13: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

13

Searching

To search a list, you need1. The list (array) containing the list2. List length3. Item to be found

After the search is completed4. If found,

Report “success” Location where the item was found

5. If not found, report “failure”

Page 14: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

14

Sequential Search

Sequential search: search a list for an item Compare search item with other elements until

either Item is found List has no more elements left

Average number of comparisons made by the sequential search equals half the list size

Good only for very short lists

Page 15: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

15

int seqSearch(const int list[], int listLength, int searchItem){ int loc;

for (loc = 0; loc < listLength; loc++) if (list[loc] == searchItem)

return loc;

return -1;}

Page 16: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

16

Sorting a List: Bubble Sort

Suppose list[0]...list[n - 1] is a list of n elements, indexed 0 to n – 1

Bubble sort algorithm: In a series of n - 1 iterations, compare successive

elements, list[index] and list[index + 1]

If list[index] is greater than list[index + 1], then swap them

Page 17: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

17

Page 18: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

18

Page 19: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

19

void bubbleSort(int list[], int length){ int temp; int i, j;

for (i = 0; i < length - 1; i++) { for (j = 0; j < length - 1 - i; j++) if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } }}

Bubble Sort Algorithm

for i = 0 to n-1 do        for j = 0 to n - i - 1 do        if list[j] > list[j+1] then         Swap(list[j] , list[j+1])

Page 20: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

20

#include <iostream>using namespace std;void bubbleSort(int list[], int length);

int main(){ int list[] = {2, 56, 34, 25, 73, 46, 89, 10, 5, 16}; //Line 1 int i; //Line 2

bubbleSort(list, 10); //Line 3

cout << "After sorting, the list elements are:" << endl; //Line 4

for (i = 0; i < 10; i++) //Line 5 cout << list[i] << " "; //Line 6

cout << endl; //Line 7

return 0; //Line 8}

void bubbleSort(int list[], int length){ int temp; int counter, index;

for (counter = 0; counter < length - 1; counter++) { for (index = 0; index < length - 1 - counter; index++) if (list[index] > list[index + 1]) { temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; } }}

Page 21: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

21

Sorting a List: Selection Sort

Selection sort: rearrange list by selecting an element and moving it to its proper position

Find the smallest (or largest) element and move it to the beginning (end) of the list

Page 22: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

22

Sorting a List: Selection Sort (continued)

On successive passes, locate the smallest item in the list starting from the next element

Page 23: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

23

void selectionSort(int list[], int length){ int index;

int smallestIndex;int minIndex;int temp;

for (index = 0; index < length - 1; index++) {

//Step asmallestIndex = index;

for (minIndex = index + 1; minIndex < length; minIndex++)

if (list[minIndex] < list[smallestIndex])smallestIndex = minIndex;

//Step b temp = list[smallestIndex];

list[smallestIndex] = list[index];list[index] = temp;

}}

Selection Sort Algorithm

Page 24: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

24

Sorting a List: Insertion Sort

The insertion sort algorithm sorts the list by moving each element to its proper place.

Page 25: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

25

Page 26: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

26

Page 27: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

27

Page 28: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

28

Page 29: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

29

Sequential Search on an Ordered List

General form of sequential search algorithm on a sorted list:

Page 30: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

30

int seqOrderedSearch(const int list[], int listLength, int searchItem){ int loc; //Line 1 bool found = false; //Line 2

for (loc = 0; loc < listLength; loc++) //Line 3 if (list[loc] >= searchItem) //Line 4 {

found = true; //Line 5 break; //Line 6

} if (found) //Line 7 if (list[loc] == searchItem) //Line 8

return loc; //Line 9 else //Line 10

return -1; //Line 11else //Line 12 return -1; //Line 13

}

Sequential Search on an Ordered List

Page 31: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

31

Binary Search

Binary search can be applied to sorted lists Uses the “divide and conquer” technique

Compare search item to middle element If search item is less than middle element, restrict

the search to the lower half of the list Otherwise search the upper half of the list

Page 32: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

32

int binarySearch(const int list[], int listLength, int searchItem){

int first = 0;int last = listLength - 1;int mid;

bool found = false;

while(first <= last && !found){

mid = (first + last) / 2;

if(list[mid] == searchItem)found = true;

else if(list[mid] > searchItem)

last = mid - 1; else

first = mid + 1;}

if(found) return mid;

else return -1;

}

Binary Search

Page 33: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

33

Binary Search (continued) Every iteration cuts size of search list in half If list L has 1000 items

At most 11 iterations needed to determine if an item x is in list

Every iteration makes 2 key (item) comparisons Binary search makes at most 22 key comparisons to

determine if x is in L Sequential search makes 500 key comparisons

(average) to if x is in L for the same size list

Page 34: 1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006

34

End of lecture 22

Thank you!