searching and sorting, template functions, and vectors itk 169 fall 2003

28
Searching and Searching and Sorting, Sorting, Template Template Functions, Functions, and Vectors and Vectors ITK 169 ITK 169 Fall 2003 Fall 2003

Upload: diane-audrey-stephens

Post on 01-Jan-2016

223 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 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

Page 2: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 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++;

Page 3: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003

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

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

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

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
Page 8: Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003

#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

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

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

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

#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

#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

#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

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

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

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

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

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

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

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

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

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

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

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

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

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

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();