Chapter - Notes Applications of Arrays I. List Processing A. Definition: List - A set of values of the same data type. B. Lists and Arrays 1. A convenient way to store a list is in an array, probably a one-dimensional array. 2. The size of a list can increase and decrease, therefore, the list should be stored in an array large enough to hold the maximum size of the list. 3. Basic operations performed on a list are: a. Search the list for a given item. b. Sort the list. c. Insert an item into the list. d. Delete an item from the list. C. Searching 1. The most common operation on a list. 2. To search the list, you need three pieces of information: a. The list or the array containing the list. b. The length of the list. ( Not the length of the array. ) c. The item for which you are searching. 3. After the search is completed, you should either: a. Report "success" and the position where the item was found b. or Report "failure". -1-
4. Sequential Search or Linear Search Function a. A value returning function with the following parameters: i. The array containing the list. ii. The length of the list, listlength. (listlength <= arraysize) iii. The item for which you are searching, searchitem. b. The function s the searchitem with the elements of the list in the array starting at position zero (0) and continues until it has either found the item or come to the end of the list. c. Two conditions exist for a loop to search the list: i. Exit loop when the item is found... ii. or exit the loop when all elements of the list have been searched without success. d. Examples of Search Functions: int seqsearch ( const int list[ ], int listlength, int searchitem ) int loc ; bool found = false ; for ( loc = 0 ; loc < listlength ; loc++ ) if ( list[loc] = = searchitem ) found = true ; break ; if ( found ) return loc ; else return -1 ; OR - - - it can be written as follows: 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 ; -2-
e. Sample Program using SeqSearch function: #include <iostream> #include <cstdlib> #include <time.h> using namespace std; const int asize = 00 ; 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 ; int main ( ) int intlist [ asize ] ; int i, listsize, look4num, result ; srand( (unsigned) time( NULL ) ); cout << "Please enter an integer for the size of list you want: " ; cin >> listsize ; for ( i = 0 ; i < listsize ; i++ ) intlist [ i ] = rand( ) % 0 + 1 ; cout << endl << "Please enter an integer between 1 and 0 to search: "; cin >> look4num ; cout << endl; result = seqsearch ( intlist, listsize, look4num ) ; if ( result!= -1 ) cout << "Number " << look4num << " can be found at position " << result << endl << endl ; else cout << "Sorry, number " << look4num << " is not in the list." << endl << endl; system("pause") ; return 0 ; -3-
f. Inefficiency of Sequential Search i. On average, the time it will take to search a list for an element is half the size of the list. ii. iii. If the list is extremely large, this can cost valuable CPU time. Remember, the above sequential search is on an unsorted list.. Sorting a List : Bubble Sort a. Suppose we would like to sort a list of n integers so that they reside in the list/array in ascending order ( smallest to largest ). b. Given the following list :,,,,... We can demonstrate how the Bubble Sort works. list [0] [1] [2] [3] [4] & swap & swap & swap How the Elements of LIST get rearranged in the First Iteration -4-
& swap & swap How the Elements of LIST get rearranged in the Second Iteration & swap & swap How the Elements of LIST get rearranged in the Third Iteration How the Elements of LIST get rearranged in the Last Iteration --
c. The above illustrations show how the list is logically sorted. The following is an example of a program that uses a function that implements the Bubble Sort in code. #include <iostream> using namespace std; void bubblesort ( int list[ ], int length ) int temp, i, k ; for ( i = 0 ; i < length - 1 ; i++ ) for ( k = 0 ; k < length - 1 - i ; k++ ) if ( list[k] > list[k+1] ) temp = list[k] ; list[k] = list[k+1] ; list[k+1] = temp ; int main ( ) int list [ ] = 2,, 34, 2, 3, 4, 89,,, 1 ; int i ; bubblesort ( list, ) ; cout << "After sorting, the list elements are: " << endl ; for ( i = 0 ; i < ; i++ ) cout << list [ i ] << " " ; cout << endl ; system("pause") ; return 0 ; --
. Sorting a List : Selection Sort a. In the Selection Sort, we rearrange the list by selecting an element in the list and moving it to its proper position. b. In the first iteration, we locate the smallest item in the list and exchange it with the first position. In the second iteration, we locate the smallest item in the list, starting at the second position, and exchange it with the second position. And so on... c. Example [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 30 24 2 2 4 0 unsorted list swap [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 30 24 2 2 4 0 unsorted list swap [0] [1] [2] [3] [4] [] [] [] [8] [9] list 30 24 2 2 4 1 0 sorted unsorted list swap [0] [1] [2] [3] [4] [] [] [] [8] [9] list 24 30 2 2 4 1 0 sorted unsorted list --
swap [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 30 2 2 4 24 0 sorted unsorted list!!! swap [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 24 2 30 4 0 2 sorted unsorted list d. Example of a function that implements the Selection Sort. void selectionsort ( int list[ ], int length ) int index, smallestindex, minindex, temp ; for ( index = 0; index < length - 1 ; index++ ) smallestindex = index ; for ( minindex = index + 1 ; minindex < length ; minindex++ ) if ( list [ minindex ] < list [ smallestindex ] ) smallestindex = minindex ; temp = list [ smallestindex ] ; list [ smallestindex ] = list [ index ] ; list [ index ] = temp ; -8-
e. The following is an example of a program using the Selection Sort. # include < iostream > using namespace std; void selectionsort( int list[ ], int length ) int index, possmallest, inrindex, temp ; for ( index = 0 ; index < length - 1; index++ ) possmallest = index ; for ( inrindex = index + 1 ; inrindex < length ; inrindex++ ) if ( list[ inrindex ] < list [ possmallest ] ) possmallest = inrindex ; ; temp = list [ possmallest ] ; list [ possmallest ] = list [ index ] ; list [ index ] = temp ; int main ( ) int list [ ] = 2,, 34, 2, 3, 4, 89,,, 1 ; int i ; selectionsort ( list, ) ; cout << "After sorting, the list elements are: " << endl << endl ; for ( i = 0 ; i < length ; i++ ) cout << list [ i ] << " " : cout << endl << endl ; system("pause") ; return 0 ; -9-
. Sorting a List : Insertion Sort a. The insertion sort algorithm sorts the list by moving each element to its proper place. b. It does this by splitting the list up into two sublists; the sorted part and the unsorted part. As it cycles through the unsorted part of the sublist, it inserts the unsorted value into the proper position in the sorted part of the list. As the sorted part of the list grows, the unsorted part of the list gets smaller, until all values have been inserted in the sorted part of the list. c. Example [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 30 24 21 2 4 0 unsorted list d. After the first four values have been sorted. [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 24 30 21 2 4 0 sorted unsorted e. Now we can see that the next item in the unsorted part of the list is the value 21 at position list[4], which should be inserted at position list[2] in the sorted part of the list. [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 24 30 21 2 4 0 sorted unsorted --
f. To move the value in list[4] into the space list[2], we must first copy the value in list[4] into a variable we will call 'temp'. [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 24 30 21 2 4 0 sorted unsorted temp copy 21 g. Next, we copy the value in list[3] into the space list[4], and then we copy the value in list [2] into the space list[3]. [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 24 30 21 2 4 0 sorted unsorted temp 21 copy copy h. The list should now look like the following: [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 24 24 30 2 4 0 sorted unsorted temp 21-11-
i. The next obvious step is to copy the value in our 'temp' variable into the space list[2] and increase our sorted list by one while decreasing our unsorted list by one. [0] [1] [2] [3] [4] [] [] [] [8] [9] list 1 21 24 30 2 4 0 sorted unsorted temp 21 j. Now you can see that we repeat these steps until every value in the unsorted list is inserted into the sorted side. k. The following code is an example of what a function would look like to implement the insertion sort: void insertionsort( int list[ ], int listlength ) int firstoutoforder, location; int temp; for ( firstoutoforder = 1 ; firstoutoforder < listlength; firstoutoforder++ ) if (list[firstoutoforder] < list[firstoutoforder - 1]) temp = list[firstoutoforder]; location = firstoutoforder; do list[location] = list[location - 1]; location- - ; while ( location > 0 && list[location - 1] > temp); ; list[location] = temp; -12-
8. Sequential Search on an Ordered List a. For a sequential search of an ordered ( sorted ) list, you search until you arrive at an item that is greater than or equal to what you are searching for, or the list has no more elements to search. b. A Function Implementing a Sequential Ordered Search: int seqorderedsearch ( const int list [ ], int listlength, int searchitem ) int loc ; bool found = false ; for ( loc = 0 ; loc < listlength ; loc++ ) if ( list [ loc ] >= searchitem ) found = true ; break ; if ( found ) if ( list [ loc ] == searchitem ) return loc ; else return -1 ; else return -1 ; 9. Binary Search a. An efficient search on an already sorted list is the Binary Search. It uses the divide and conquer approach. b. First, the search item is d with the value in the middle position of the list. If the value in the middle is greater than the search item, the first half of the list is searched, else if the value in the middle is less than the search item, then the second half of the list is searched. -13-
c. This method is again repeated. The middle position value of the half that is to be searched is d with the search item. And the process is repeated. d. Example: Searching for the number [0] [1] [2] [3] [4] [] [] [] [8] [9] [] [11] list 4 8 2 34 39 4 48 89 9 search list mid [0] [1] [2] [3] [4] [] [] [] [8] [9] [] [11] list 4 8 2 34 39 4 48 89 9 search list mid [0] [1] [2] [3] [4] [] [] [] [8] [9] [] [11] list 4 8 2 34 39 4 48 89 9 search list [0] [1] [2] [3] [4] [] [] [] [8] [9] [] [11] list 4 8 2 34 39 4 48 89 9 mid search list found [0] [1] [2] [3] [4] [] [] [] [8] [9] [] [11] list 4 8 2 34 39 4 48 89 9-14-
e. The Binary Search Function 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 ;. Performance of Binary Search a. Suppose you sort a list of 00 elements with the Binary Search. b. Because 00 24 = 2, a while loop will have at most 11 iterations to find the searched item. c. Because every iteration of the while loop makes at most two comparisons, the Binary Search will make at most 22 comparisons to determine if the search item is in the list. d. For a list of 00, A Binary Search takes 22 comparisons while a Sequential Search averages 00 comparisons. e. Bottom Line: The larger the size of the list, the more efficient a Binary Search is over a Sequential Search. -1-