Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
-
Upload
diane-audrey-stephens -
Category
Documents
-
view
222 -
download
4
Transcript of Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
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
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++;
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.
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).
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.
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.
#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;
}}
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?
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.
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;
}}
#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;
}}
#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;
}}
#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;
}}
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;}
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.
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;
Vectors - Syntax• You must include:
#include <vector>• Vector Declaration:
vector<type> name;• Example:
vector<int> scores;
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.
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.
Example
vector<double> sample;sample.push_back(0.0);sample.push_back(1.1);sample.push_back(2.2);
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();
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;
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.
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.
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
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
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();