1 / 42

CHAPTER 10 ARRAYS II Applications and Extensions

CHAPTER 10 ARRAYS II Applications and Extensions. In this chapter, you will: Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement the binary search algorithm

jena-sykes
Download Presentation

CHAPTER 10 ARRAYS II Applications and Extensions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 10ARRAYS IIApplications and Extensions

  2. In this chapter, you will: • Learn how to implement the sequential search algorithm • Explore how to sort an array using the selection sort algorithm • Learn how to implement the binary search algorithm • Discover how to manipulate data in a two-dimensional array • Learn about multidimensional arrays

  3. LIST PROCESSING • A list is defined as a set of values of the same type. • Some of the basic operations that are performed on a list are: 1. Search the list for a given item. 2. Sort the list. 3. Insert an item in the list. 4. Delete an item from the list.

  4. Sequential Search found is set to false; for(loc = 0; loc < length; loc++) if(list[loc] is equal to searchItem) { found is set to true exit loop } if(found) return loc; else return –1; If the function seqSearch returns a value greater than or equal to 0, it is a successful search; otherwise, it is an unsuccessful search.

  5. int seqSearch(constint list[], int listLength, int searchItem) { int loc; boolfound = false; for(loc = 0; loc < listLength; loc++) if(list[loc] == searchItem) { found = true; break; } if(found) return loc; else return -1; }

  6. Sorting a List: Selection Sort • This algorithm a. Finds the location of the smallest element b. Move the smallest element to the beginning of the unsorted list. for(index = 0; index < length – 1; index++) { a. Find the location, smallestIndex, of the smallest element in list[index]...list[length]. b. Swap the smallest element with list[index]. That is, swap list[smallestIndex] with list[index]. }

  7. Step a: smallestIndex = index; //assume first element is the //smallest for(minIndex = index + 1; minIndex < length; minIndex++) if(list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; //current element in //the list is smaller than //the smallest so far, so //update smallestIndex Step b: temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp;

  8. Before writing the selection sort function, let us apply the above algorithm to sort the following list. The length of list is 6

  9. Iteration 1: Sortlist[0]...list[5]. Step a: Find the index of the smallest element in list[0]...list[5]. Step b: Swap list[smallestIndex] with list[index], that is, list[3] with list[0]. The resulting array is:

  10. Iteration 2: Sort list[1]...list[5]. Here index = 1. Step a: Find the index of the smallest element in list[1]...list[5]. Step b: Swap list[smallestIndex] with list[index]. That is, swap list[1] with list[1].

  11. Iteration 3: Sort list[2]...list[5]. Step a: Find the index of the smallest element in list[2]...list[5]. Step b: Swap list[smallestIndex] with list[index]. That is, swap list[4] with list[2].

  12. Iteration 4: Sort list[3]...list[5]. Step a: Find the index of the smallest element in list[3]...list[5]. Step b: Swap list[smallestIndex] with list[index]. That is, swap list[3] with list[3].

  13. Iteration 5: Sort list[4]...list[5]. Step a: Find the index of the smallest element in list[4]...list[5]. Step b: Swap list[smallestIndex] with list[index]. That is, swap list[5] with list[4].

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

  15. Sequential search on a sorted list found is set to false; for(loc = 0; loc < listLength; loc++) if (list[loc] is greater than or equal to searchItem) { found is set to true exit loop } if(found) if(list[loc] == searchItem) return loc; else return –1; else return –1;

  16. int seqOrderdSearch(constint list[], int listLength, int searchItem) { int loc; //Line 1 boolfound = 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 }

  17. length = 8; searchItem = 35; loc = 0, found = false. Iteration locfoundlist[loc]list[loc]>= searchItem 1 0 false 4 4 >= 35 isfalse; loc = 1 2 1 false 18 18 >= 35 isfalse;loc = 2 3 2 false 29 29 >= 35 isfalse;loc = 3 4 3 false 35 35 >= 35 istrue; found = true • found is true, the if statement at Line 8 executes. • list[loc]= 35 and searchItem = 35, the expression • list[loc] == searchItem • evaluates to true and the return statement at Line 9 returns the value of loc, which is 3. This is a successful search.

  18. searchItem = 40; loc = 0; found = false. Iteration locfoundlist[loc] list[loc]>= searchItem 1 0 false 4 4 >= 40 isfalse; loc = 1 2 1 false 18 18 >= 40 isfalse; loc = 2 3 2 false 29 29 >= 40 isfalse; loc = 3 4 3 false 35 35 >= 40 isfalse; loc = 4 5 4 false 44 44 >= 40 istrue; found = true found is true. The if statement at Line 8 executes. list[loc] = 44 and searchItem = 40. The expression list[loc] == searchItem evaluates to false, the statement at Line 9 is skipped, and the return statement at Line 11 executes, which returns –1. This is an unsuccessful search.

  19. Binary Search • A binary search is similar to a dictionary search. • A binary search uses the “divide and conquer” technique to search the list. • First, the search item is compared with the middle element of the list. • If the search item is less than the middle element of the list, we restrict the search to the upper half of the list; otherwise, we search the lower half of the list. Consider the following sorted list of length = 12

  20. Determine whether 75 is in the list • Compare 75 with the middle element in the list, list[5] (which is 39). • Because 75list[5] and 75>list[5], we then restrict our search to the list list[6]...list[11]

  21. int binarySearch(constint 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; }//end binarySearch

  22. searchItem = 89; first = 0, last = 11, and found = false Iterationfirstlastmidlist[mid]Number of Key Comp. 1 0 11 5 39 2 2 6 11 8 66 2 3 9 11 10 89 1(found istrue) • searchItem = 34; first = 0, last = 11, and found = false Iterationfirstlastmidlist[mid]Number of Key Comp. 1 0 11 5 39 2 2 0 4 2 19 2 3 3 4 3 25 2 4 4 4 4 34 1(found istrue)

  23. searchItem = 22; first = 0, last = 11, and found = false Iterationfirstlastmidlist[mid]Number of Key Comp. 1 0 11 5 39 2 2 0 4 2 19 2 3 3 4 3 25 2 4 3 2 The loop stops (because first > last) unsuccessful search

  24. Two-dimensional Array: A two-dimensional array is a collection of a fixed number of components arranged in two dimensions, and all components are of the same type. The syntax for declaring a two-dimensional array is: dataType arrayName[# of Rows][# of Column]; double sales[10][5]; declares a two-dimensional array sales of 10 rows and 5 columns and every component is of the type float.

  25. Two-Dimensional Array Initialization During Declaration Like one-dimensional arrays, two-dimensional arrays can be initialized when they are declared. int board[4][3] = {{2, 3, 1}, {15, 25, 13}, {20,4,7}, {11,18,14}};

  26. Processing Two-dimensional Arrays const int rows = 7; const int columns = 6; int matrix[rows][columns]; int row; int col; int sum; int largest; int temp;

  27. Initialize the 6th row to zero row = 5; for(col = 0; col < columns; col++) matrix[row][col] = 0;

  28. Print out the 3rd column to screen output col = 2; for(row = 0; row < rows; row++) cout << matrix[row][col] << endl;

  29. Print The following nested for loops print the components of matrix, one row per line. for(row = 0; row < rows; row++) { for(col = 0; col < columns; col++) cout<<setw(5)<<matrix[row][col]<<" "; cout<<endl; }

  30. Sum of each individual row for(row = 0; row < rows; row++) { sum = 0; for(col = 0; col < columns; col++) sum = sum + matrix[row][col]; cout<<"Sum of row "<<row+1<<" = " <<sum<<endl; } Sum of each individual column for(col = 0; col < columns; col++) { sum = 0; for(row = 0; row < rows; row++) sum = sum + matrix[row][col]; cout<<"Sum of column "<<col+1<<" = " <<sum<<endl; }

  31. //Largest element in each row for(row = 0; row < rows; row++) { largest = matrix[row][0]; for(col = 1; col < columns; col++) if(largest < matrix[row][col]) largest = matrix[row][col]; cout<<"Largest element of row "<<row+1 <<" = "<<largest<<endl; }

  32. //Largest element in each column for(col = 0; col < columns; col++) { largest = matrix[0][col]; for(row = 1; row < rows; row++) if(largest < matrix[row][col]) largest = matrix[row][col]; cout<<"Largest element of col "<<col+1 <<" = "<<largest<<endl; }

  33. Passing Two-dimensional Arrays as Parameters to Functions • Two-dimensional arrays can be passed as parameters to a function and they are passed by reference. • The base address, that is, the address of the first component of the actual parameter is passed to the formal parameter. • When storing a two-dimensional array in the computer’s memory, C++ uses the row order form. That is, first the first row is stored, followed by the second row, which is followed by the third row and so on. void example(int table[][5], int rowsize) { . . . }

  34. Arrays of Strings • Arrays of Stings and the string Type string list[100]; • Arrays of Strings and C- Strings (Character Arrays) char list[100][16];

  35. strcpy(list[1], "Snow White"); for(j = 0; j < 100; j++) cout<<list[j]<<endl;

  36. Other form of Declaring Two-dimensional Arrays const int numberOfRows = 20; const int numberOfColumns = 10; typedef int tableType[numberOfRows][numberOfColumns]; We can declare variables of the type tableType. tableType matrix; void initialize(tableType table) { int row; int col; for(row = 0; row < numberOfRows; row++) for(col = 0; col < numberOfColumns; col++) table[row][col] = 0; }

  37. Multi-dimensional Arrays • Array: An array is a collection of a fixed number of elements (called components) arranged in n dimensions (n >= 1), called an n-dimensional array. The general syntax of declaring an n-dimensional array is: dataType arrayName[intExp1][intExp2]...[intExpn]; where intExp1, intExp2, … ,and intExpn areconstant expressions yielding positive integer values. The syntax of accessing a component of an n-dimensional array is: arrayName[indexExp1][indexExp2]...[indexExpn] where indexExp1,indexExp2,..., and indexExpn are expressions yielding nonnegative integer values. indexExpi gives the position of the array component in the ith dimension.

  38. The statement declares carDealers to be a three-dimensional array. double carDealers[10][5][7]; • Size of the first dimension is 10 and it ranges from 0 to 9. • The size of the second dimension is 5 and it ranges from 0 to 4. • The size of the third dimension is 7 and it ranges from 0 to 6. • The base address of the array carDealers is the address of the first array component, that is the address of carDealers[0][0][0]. • The total number of components in the array carDealers is 10*5*7 = 350. carDealers[5][3][2] = 15564.75; for(i = 0; i < 10; i++) for(j = 0; j < 5; j++) for(k = 0; k < 7; k++) carDealers[i][j][k] = 0.0; initializes the entire array to 0.0.

More Related