Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
-
Upload
diane-audrey-stephens -
Category
Documents
-
view
223 -
download
4
Transcript of Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
![Page 1: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/1.jpg)
Searching and Searching and Sorting, Sorting,
Template Functions,Template Functions,and Vectors and Vectors
Searching and Searching and Sorting, Sorting,
Template Functions,Template Functions,and Vectors and Vectors
ITK 169ITK 169Fall 2003Fall 2003
![Page 2: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/2.jpg)
Searching an Array• We used a simple linear search in program
1. while(aTrans.partNum != invArr[i].get_partNum)
i++;-or-
while(!found) if(aTrans.partNum == invArr[i].get_partNum)
found = true; else
i++;
![Page 3: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/3.jpg)
A Better Search• A linear search in necessary if
the array is in no particular order.
• A better search could be performed if the array were known to be in a specific order.
• Therefore a sorting function is necessary.
![Page 4: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/4.jpg)
Sorting an Array• Numeric arrays can easily be
sorted, either lowest to highest (ascending) or highest to lowest (descending).
• String arrays can also be sorted alphabetically (ascending or descending).
![Page 5: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/5.jpg)
Selection Sort
• If we are sorting the array in ascending order, we find the smallest element and place that in index 0, then the next smallest is placed in index 1, etc.
• Consider what necessary steps you would need to sort an array.
![Page 6: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/6.jpg)
Steps for Selection Sort• Look for smallest element in the array.
– Compare two elements to find the smaller of the two and hold its index.
– If array holds class objects – need member function, friend function, or overloaded operator. (Should know how to code these.)
• Swap two elements when out of order.
![Page 7: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/7.jpg)
![Page 8: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/8.jpg)
#include <iostream>using namespace std;
void sort(int array[ ], int size);
int main(){
int array[]={7,3,9,18,15};int i, size = 5;for (i = 0; i<size; i++)
cout<<array[i]<<" ";sort(array, size);cout<<endl;for (i = 0; i<size; i++)
cout<<array[i]<<" ";return(0);
}
void sort(int array[ ], int size){
int temp;int Index;for(int i=0; i<size-1; i++){// place correct value in array[i]
temp = array[i];Index = i;// find smallestfor(int j=i +1; j<size; j++)
if(array[j] < temp){ temp = array[j]; Index = j;}
// swap elementstemp = array[i];array[i] = array[Index];array[Index] = temp;
}}
![Page 9: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/9.jpg)
Swap Two Elements• Suppose the array holds integers…• Suppose the array holds doubles…• Suppose the array holds Parts…• What is the difference between the
three sets of code above?
![Page 10: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/10.jpg)
Template Functions• Write a template function when
the function can be used on a variety of different types with “virtually” no change in the code.
• The type is determined by the calling statement and can change with each call to the function.
![Page 11: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/11.jpg)
Template Sort Functiontemplate <typename T>void sort(T array[ ], int size){
T temp;int Index;for(int i=0; i<size-1; i++){// place correct value in array[i]
temp = array[i];Index = i;// find smallestfor(int j=i +1; j<size; j++)
if(array[j] < temp){ temp = array[j]; Index = j;}
// swap elementstemp = array[i];array[i] = array[Index];array[Index] = temp;
}}
![Page 12: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/12.jpg)
#include <iostream>using namespace std;
template <typename T>void sort(T array[ ], int size);
int main(){
int array[]={7,3,9,18,15};int i, size = 5;for (i = 0; i<size; i++)
cout<<array[i]<<" ";sort(array, size);cout<<endl;for (i = 0; i<size; i++)
cout<<array[i]<<" ";return(0);
}
template <typename T>void sort(T array[ ], int size){
T temp;int Index;for(int i=0; i<size-1; i++){// place correct value in array[i]
temp = array[i];Index = i;// find smallestfor(int j=i +1; j<size; j++)
if(array[j] < temp){ temp = array[j]; Index = j;}
// swap elementstemp = array[i];array[i] = array[Index];array[Index] = temp;
}}
![Page 13: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/13.jpg)
#include <iostream>using namespace std;
template <typename T>void sort(T array[ ], int size);
int main(){
double array[]={7.1,3.6,9.0,18.7,15.2};int i, size = 5;for (i = 0; i<size; i++)
cout<<array[i]<<" ";sort(array, size);cout<<endl;for (i = 0; i<size; i++)
cout<<array[i]<<" ";return(0);
}
template <typename T>void sort(T array[ ], int size){
T temp;int Index;for(int i=0; i<size-1; i++){// place correct value in array[i]
temp = array[i];Index = i;// find smallestfor(int j=i +1; j<size; j++)
if(array[j] < temp){ temp = array[j]; Index = j;}
// swap elementstemp = array[i];array[i] = array[Index];array[Index] = temp;
}}
![Page 14: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/14.jpg)
#include <iostream>using namespace std;
template <typename T>void sort(T array[ ], int size);
int main(){
char array[]={'f','a','7','B','c'};int i, size = 5;for (i = 0; i<size; i++)
cout<<array[i]<<" ";sort(array, size);cout<<endl;for (i = 0; i<size; i++)
cout<<array[i]<<" ";return(0);
}
template <typename T>void sort(T array[ ], int size){
T temp;int Index;for(int i=0; i<size-1; i++){// place correct value in array[i]
temp = array[i];Index = i;// find smallestfor(int j=i +1; j<size; j++)
if(array[j] < temp){ temp = array[j]; Index = j;}
// swap elementstemp = array[i];array[i] = array[Index];array[Index] = temp;
}}
![Page 15: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/15.jpg)
Break into 2 functionstemplate <typename T>void sort(T array[], int size){
T temp;int NextSmallestIndex;for(int i=0; i<size-1; i++){// place correct value in array[i]
// find smallestNextSmallestIndex =
findMin(array, i, size);// swap elementstemp = array[i];array[i] =
array[NextSmallestIndex];array[NextSmallestIndex] =
temp;}
}
template <typename T>int findMin(const T array[], int
start_index, int size){
T min = array[start_index];int minIndex = start_index;
for(int i=start_index +1; i<size; i++){
if(array[i] < min){
min = array[i];minIndex = i;
}}
return minIndex;}
![Page 16: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/16.jpg)
Vectors• When an array is created, it
must have a declared size and this size cannot change during the program.
• A vector can be thought of as an array that can grow (and shrink) in length while your program is running.
![Page 17: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/17.jpg)
Vector Basics• Like an array, a vector has a base
type and stores a collection of elements of this type.
• The vector class is a templated class.
• You must #include <vector>• Declaration syntax:
vector<type> name;example – vector<int>
scores;
![Page 18: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/18.jpg)
Vectors - Syntax• You must include:
#include <vector>• Vector Declaration:
vector<type> name;• Example:
vector<int> scores;
![Page 19: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/19.jpg)
Using Vectors
• Vector elements are indexed the same as arrays – (starting at zero).
• You can use the square brackets [ ] to change the value stored in a vector element.
![Page 20: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/20.jpg)
Adding Vectors Elements
• The first time you add an element to the vector, you must use a function called push_back( ).
•The function’s parameter is of the same type as the vector elements.
![Page 21: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/21.jpg)
Example
vector<double> sample;sample.push_back(0.0);sample.push_back(1.1);sample.push_back(2.2);
![Page 22: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/22.jpg)
Vector Size• The vector class has a function called size
that will return the number of elements currently held.
• The size function returns an unsigned integer. – You will receive a warning if you store size in
a regular integer.unsigned int x;x = sample.size();
![Page 23: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/23.jpg)
Printing a Vector
• Like arrays, vectors are printed using a for-loop:
for(unsigned int i=0; i< sample.size( ); i++)cout << sample[i] << endl;
![Page 24: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/24.jpg)
Special Declarations
• You can initialize a primitive vector to a particular size with default elements using this notation:
vector<int> sample(10);– This vector will have size=10 and
each of these 10 elements will equal zero.
– To add any additional elements you would have to use the push_back function.
![Page 25: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/25.jpg)
Over-running a Vector• Provided you always use the
push_back function, you will never need to worry about over-running a vector.– Note: You can over-run a vector
using the square brackets. – When accessing vector elements,
take care that the index inside the brackets never exceeds the size-1.
![Page 26: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/26.jpg)
Removing Vector Elements
• To remove vector elements use the member function:– pop_back( );
vector<int> sample;sample.push_back(3);sample.push_back(5);sample.push_back(4);
3 5 4
sample.size( );
3
![Page 27: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/27.jpg)
Removing Vector Elements
• Removing an element from the back of the vector.
vector<int> sample;sample.push_back(3);sample.push_back(5);sample.push_back(4);sample.pop_back( );
3 5
sample.size( );
2
![Page 28: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.](https://reader035.fdocuments.net/reader035/viewer/2022081516/56649ecf5503460f94bdcb91/html5/thumbnails/28.jpg)
Other Functions• Every vector has a size and a
capacity.• Capacity is the number of
elements which the vector is currently allocated to hold. – This is a memory control or
efficiency issue and not one we will concern ourselves with at this time.
– More vector member functions:.capacity();.empty();