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

Slides:



Advertisements
Similar presentations
CHAPTER 10 ARRAYS II Applications and Extensions.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 19: Searching and Sorting Algorithms
TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914,
Data Structures & Algorithms CHAPTER 3 Sorting Ms. Manal Al-Asmari.
1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
1 Lecture 25:Pointers Introduction to Computer Science Spring 2006.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Sorting Algorithms: Selection, Insertion and Bubble.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Arrays Multi-dimensional initialize & display Sample programs Sorting Searching Part II.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Data Structures and Algorithms.
Chapter 8 Arrays and Strings
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
Computer Science Searching & Sorting.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Lecture 16: Searching and Sorting Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 14: Searching and Sorting
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Sorting Algorithms: Selection, Insertion and Bubble.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CHAPTER 10 ARRAYS II Applications and Extensions.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 16: Searching, Sorting, and the vector Type.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
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.
Chapter 16: Searching, Sorting, and the vector Type
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Basic Array Definition
Sorting Algorithms: Selection, Insertion and Bubble
Lecture 9 Objectives Learn about arrays.
Data Structures (CS212D) Week # 2: Arrays.
Applications of Arrays
Presentation transcript:

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

2 Contents Review of Arrays Application of Arrays

3 Contents Review of Arrays Application of Arrays

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];

5 #include 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); }

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];

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;

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];

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; }

10 #include 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; }

11 Contents Review of Arrays Application of Arrays sequential search algorithm bubble sort algorithm selection sort algorithm binary search algorithm

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

13 Searching To search a list, you need 1. The list (array) containing the list 2. List length 3. Item to be found After the search is completed 4. If found, Report “ success ” Location where the item was found 5. If not found, report “ failure ”

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

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; }

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

17

18

19 void bubbleSort(int list[], int length) { int temp; int i, j; for (i = 0; i < length - 1; i++) { for (j = 0; j < length 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])

20 #include 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 counter; index++) if (list[index] > list[index + 1]) { temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; }

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

22 Sorting a List: Selection Sort (continued) On successive passes, locate the smallest item in the list starting from the next element

23 void selectionSort(int list[], int length) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < length - 1; index++) { //Step a smallestIndex = 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

24 Sorting a List: Insertion Sort The insertion sort algorithm sorts the list by moving each element to its proper place.

25

26

27

28

29 Sequential Search on an Ordered List General form of sequential search algorithm on a sorted list:

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 11 else//Line 12 return -1;//Line 13 } Sequential Search on an Ordered List

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

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

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

34 End of lecture 22 Thank you!