tailieunhanh - Lecture Introduction to Computer Programming - Lecture 19
The contents of this chapter include all of the following: Pointer arithmetic – a program, sorting techniques – a program, passing an entire array to a function, array and pointer, 2 dimensional array. | CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides: 2003 Prentice Hall, Inc. Applications of Arrays Searching Arrays: Linear Search Search array for a key value Linear search Compare each element of array with key value Start at one end, go to other Useful for small and unsorted arrays Inefficient, if search key not present, examines every element // The function compares key to every element of array until location // is found or until end of array is reached; return subscript of // element key or -1 if key not found int linearSearch( const int array[], int key, int sizeOfArray ) { for ( int j = 0; j Applications of Arrays Searching Arrays: Linear Search Search array for a key value Linear search Compare each element of array with key value Start at one end, go to other Useful for small and unsorted arrays Inefficient, if search key not present, examines every element // The function compares key to every element of array until location // is found or until end of array is reached; return subscript of // element key or -1 if key not found int linearSearch( const int array[], int key, int sizeOfArray ) { for ( int j = 0; j < sizeOfArray; j++ ) if ( array[ j ] == key ) // if found, return j; // return location of key return -1; // key not found } // end function linearSearch Linear Search Function Sorting Arrays : .
đang nạp các trang xem trước