Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003."— Presentation transcript:

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

2 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++;

3 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.

4 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).

5 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.

6 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.

7

8 #include 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 smallest for(int j=i +1; j<size; j++) if(array[j] < temp) { temp = array[j]; Index = j; } // swap elements temp = array[i]; array[i] = array[Index]; array[Index] = temp; }

9 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?

10 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.

11 Template Sort Function template 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 smallest for(int j=i +1; j<size; j++) if(array[j] < temp) { temp = array[j]; Index = j; } // swap elements temp = array[i]; array[i] = array[Index]; array[Index] = temp; }

12 #include using namespace std; template 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 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 smallest for(int j=i +1; j<size; j++) if(array[j] < temp) { temp = array[j]; Index = j; } // swap elements temp = array[i]; array[i] = array[Index]; array[Index] = temp; }

13 #include using namespace std; template 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 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 smallest for(int j=i +1; j<size; j++) if(array[j] < temp) { temp = array[j]; Index = j; } // swap elements temp = array[i]; array[i] = array[Index]; array[Index] = temp; }

14 #include using namespace std; template 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 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 smallest for(int j=i +1; j<size; j++) if(array[j] < temp) { temp = array[j]; Index = j; } // swap elements temp = array[i]; array[i] = array[Index]; array[Index] = temp; }

15 Break into 2 functions template 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 smallest NextSmallestIndex = findMin(array, i, size); // swap elements temp = array[i]; array[i] = array[NextSmallestIndex]; array[NextSmallestIndex] = temp; } template 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; }

16 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.

17 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 Declaration syntax: vector name; example – vector scores;

18 Vectors - Syntax You must include: #include Vector Declaration: vector name; Example: vector scores;

19 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.

20 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.

21 Example vector sample; sample.push_back(0.0); sample.push_back(1.1); sample.push_back(2.2);

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

23 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;

24 Special Declarations You can initialize a primitive vector to a particular size with default elements using this notation: vector 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.

25 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.

26 Removing Vector Elements To remove vector elements use the member function: –pop_back( ); vector sample; sample.push_back(3); sample.push_back(5); sample.push_back(4); 354 sample.size( ); 3

27 Removing Vector Elements Removing an element from the back of the vector. vector sample; sample.push_back(3); sample.push_back(5); sample.push_back(4); sample.pop_back( ); 35 sample.size( ); 2

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


Download ppt "Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003."

Similar presentations


Ads by Google