Title: Lecture 22:Applications of Arrays
1Lecture 22Applications of Arrays
- Introduction to Computer Science
- Spring 2006
2Contents
- Review of Arrays
- Application of Arrays
3Contents
- Review of Arrays
- Application of Arrays
4Arrays
- 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 arrayNameintExp
- where intExp is any expression that
evaluates to a positive integer - Example int num5
5include ltiostreamgt include ltiomanipgt using
namespace std const int arraySize 10 void
fill_Array(double list, int listSize)
for(int i0 iltlistSize i)
cingtgtlisti void print_Array(double list,
int listSize) for(int i0 iltlistSize i)
coutltltsetw(5)ltltlisti void
copy_Array(const double listOne, double
listTwo, int listOneSize) for(int i0
iltlistOneSize i) listTwoi
listOnei int indexLargestElement(double
list, int listSize) int maxIndex 0 for
(int i 0 i lt listSize i) if
( listmaxIndex lt listi )
maxIndex i return maxIndex int
main() double listAarraySize0 double
listBarraySize fill_Array(listA,
arraySize) print_Array(listA,
arraySize) copy_Array(listA, listB,
arraySize) coutltltThe position of the largest
element in listA is ltlt indexLargestElement(list
A, arraySize)
6Two-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 arrayNameintexp1intexp2
- where intexp1 and intexp2 are expressions
yielding positive integer values
7Accessing Array Components
- The syntax to access a component of a
two-dimensional array is - arrayNameindexexp1indexexp2
- where indexexp1 and indexexp2 are expressions
yielding nonnegative integer values - Example sales5325.75
8Processing 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 lt columns col)
- matrixrowcol 0
- The following for loop inputs data in row 4 of
matrix - row 4
- for(col 0 col lt columns col)
- cingtgtmatrixrowcol
9Processing 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 lt rows row)
-
- for(col 0 col lt columns col)
-
- matrixrowcol 0
-
-
10include ltiostreamgt include ltiomanipgt using
namespace std const int MAXI 10 const int
MAXJ 15 void init_matrix(double
matrixMAXIMAXJ) for(int i0 iltMAXI
i) for(int j0 jltMAXJ
j) matrixijij void
print_matrix(double matrixMAXIMAXJ) for(int
i0 iltMAXI i) for(int
j0 jltMAXJ j) coutltltsetw(5)ltltmatrixij
coutltltendl double
sum_matrix(double matrixMAXIMAXJ) double
sum0.0 for(int i0 iltMAXI i)
for(int j0 jltMAXJ j) sum sum
matrixij return sum int
main() double matrixMAXIMAXJ double
sum init_matrix(matrix) print_matrix(matrix)
sum sum_matrix(matrix) coutltlt"The sum of
all the elements of matrix is "ltltsumltltendl
11Contents
- Review of Arrays
- Application of Arrays
- sequential search algorithm
- bubble sort algorithm
- selection sort algorithm
- binary search algorithm
12List Processing
- List a set of values of the same type
- Basic list operations
- Search for a given item
- Sort the list
- Insert an item in the list
- Delete an item from the list
13Searching
- To search a list, you need
- The list (array) containing the list
- List length
- Item to be found
- After the search is completed
- If found,
- Report success
- Location where the item was found
- If not found, report failure
14Sequential 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
15int seqSearch(const int list, int listLength,
int searchItem) int loc for (loc 0 loc
lt listLength loc) if (listloc
searchItem) return loc return -1
16Sorting a List Bubble Sort
- Suppose list0...listn - 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, listindex and listindex
1 - If listindex is greater than listindex 1,
then swap them
17(No Transcript)
18(No Transcript)
19Bubble Sort Algorithm
for i 0 to n-1 do for j 0 to n - i -
1 do if listj gt listj1 then
Swap(listj , listj1)
void bubbleSort(int list, int length) int
temp int i, j for (i 0 i lt length -
1 i) for (j 0 j lt length -
1 - i j) if (listj gt listj
1) temp
listj listj listj 1
listj 1 temp
20include ltiostreamgt 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 ltlt
"After sorting, the list elements are" ltlt endl
//Line 4 for (i 0 i
lt 10 i) //Line 5
cout ltlt listi ltlt " "
//Line 6 cout ltlt endl
//Line 7 return
0
//Line 8 void bubbleSort(int list, int
length) int temp int counter, index
for (counter 0 counter lt length - 1
counter) for (index 0 index
lt length - 1 - counter index) if
(listindex gt listindex 1)
temp listindex
listindex listindex 1
listindex 1 temp
21Sorting 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
22Sorting a List Selection Sort (continued)
- On successive passes, locate the smallest item in
the list starting from the next element
23Selection Sort Algorithm
void selectionSort(int list, int length)
int index int smallestIndex int
minIndex int temp for (index 0 index lt
length - 1 index) //Step
a smallestIndex index for (minIndex
index 1 minIndex lt length minIndex) if
(listminIndex lt listsmallestIndex) smalles
tIndex minIndex //Step b temp
listsmallestIndex listsmallestIndex
listindex listindex temp
24Sorting a List Insertion Sort
The insertion sort algorithm sorts the list by
moving each element to its proper place.
25(No Transcript)
26(No Transcript)
27(No Transcript)
28(No Transcript)
29Sequential Search on an Ordered List
- General form of sequential search algorithm on a
sorted list
30Sequential Search on an Ordered List
int seqOrderedSearch(const int list, int
listLength, int searchItem) int
loc //Line 1 bool found false //Line
2 for (loc 0 loc lt listLength
loc) //Line 3 if (listloc gt
searchItem) //Line 4 found
true //Line 5 break //Line 6
if (found) //Line 7 if
(listloc searchItem) //Line 8 return
loc //Line 9 else //Line 10
return -1 //Line 11 else //Line 12
return -1 //Line 13
31Binary 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
32Binary Search
int binarySearch(const int list, int
listLength, int searchItem) int first
0 int last listLength - 1 int mid bool
found false while(first lt last
!found) mid (first last) /
2 if(listmid searchItem) found
true else if(listmid gt searchItem)
last mid - 1 else first mid
1 if(found) return mid else
return -1
33Binary 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
34End of lecture 22