Download presentation
Presentation is loading. Please wait.
1
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006
2
2 Contents Review of Arrays Application of Arrays
3
3 Contents Review of Arrays Application of Arrays
4
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
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
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
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
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
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
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
11 Contents Review of Arrays Application of Arrays sequential search algorithm bubble sort algorithm selection sort algorithm binary search algorithm
12
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
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
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
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
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
17
18
18
19
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])
20
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 - 1 - counter; index++) if (list[index] > list[index + 1]) { temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; }
21
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
22 Sorting a List: Selection Sort (continued) On successive passes, locate the smallest item in the list starting from the next element
23
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
24 Sorting a List: Insertion Sort The insertion sort algorithm sorts the list by moving each element to its proper place.
25
25
26
26
27
27
28
28
29
29 Sequential Search on an Ordered List General form of sequential search algorithm on a sorted list:
30
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
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
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
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
34 End of lecture 22 Thank you!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.