|
||||||
| C Tutorials All C Tutorials and Code |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
||||
|
Templates and Sorting
C++ Templates
Templates allow you to write a function once that works on any data type. The idea of templates is used in the container classes in the STL. Why would this be useful? Consider you create a function selectSort that sorts an array of items using the selection sort algorithm. When you define this function you would define the data type that it uses when you write it. Say you write it to sort an array of integers. Great, now you can sort an array of integers using the selection sort. What if you want to sort an array of doubles? You would have to define a second function called selectSort2 that sorts an array of floats. So you would have to define lots of sort methods. Now say your program doesn't run fast off with the 0(n^2) selection sort and you want to use the quick sort instead. You would have to modify all of those functions. This becomes impossible to maintain. Another problem, say you want to sort a vector? You would then have to write the function to sort a vector. This becomes imposssible to manage when you start creating array of objects. This is where templates are useful. You write the function once and let the compiler compile several different versions of the function that are needed. This makes modification so much easier. The Selection Sort Let us first take a look at the selection sort. This is a very basic algorithm. Input: a sequence of n items Output: a sequence of n items such that arr[0] < arr[1] < arr[2] ... arr[n-1] How does this work? The selection sort makes n-1 passes through the array. On each pass through the array it finds the next smallest item in the array and keeps track of the index of the smallest item. Once we get to the end of the inner loop, if iMin is equal to i, then we know that the smallest item is at the beginning of the loop, so we don't do any swapping of items. Otherwise we introduce a temporary variable that is used to store one of the previous items, so we can swap the items in the array. Here is the code for the selection sort for an array of integers. Code:
/**
@param arr
The array of data to be sorted in ascending order. It contains n items.
@param n
The number of items in the array.
*/
void selectSort(int arr[], int n) {
int iMin, nTemp;
for (int i=0;i<n-1;i++) {
iMin = i;
for (int j=i+1;j<n;j++) {
if (arr[j] < arr[iMin]) {
iMin = j;
}
}
if (iMin != i) {
// the smallest item is somewhere else in the array so move it to this position
nTemp = arr[i];
arr[i] = arr[iMin];
arr[iMin] = nTemp;
}
}
}
Code:
int nums[6] = {3,2,4,5,6,7};
selectSort(nums, 6);
for (int i=0;i<6;i++) {
cout << nums[i] << " ";
}
cout << endl;
Let us take a look at how this actually works. Consider this array: Quote:
Code:
int arr[6] = {3,2,5,7,-1,4};
selectSort(arr,6);
This gives us the array: {-1,2,5,7,3,4} On the second pass when i = 1 we look through the array ranges [i+1,n). I set iMin = to 1 and compare each item with the value at index iMin. If I find a smaller item, I move the index of the smaller item into iMin. On this comparison we find that iMin is 1. Since iMin is the same as the current i, the smallest item is already in the correct place. You can confirm this by looking at the array. Is 2 in the correct spot in the sorted array? Third pass: we identify that 3 (located at index 4) is the smallest item in the aray, and we swap it with the value at index 2. Our new array becomes: {-1,2,3,7,5,4} Pass four: Looking at the last 3 items we notice that 4 is the smallest item is at index 5 so we swap index 5 and index 3. This gives us this array: {-1,2,3,4,5,7} Pass five: We look at index 4 and find the smallest item in the subset {5,7}. We notice that 5 is already in the correct place, so we do not move it. Now we are done. Wait! We didn't look at the last item? Well think about why should we? If we looked at item at index 5, we would have a subset of {7}. There is only one number in the subset. Thus a subset of 1 is already sorted. This tutorial is not about analysis but if we did an analysis we would note that the running time is 0(n^2). Writing a generic function The problem with the above function was mentioned earlier. It only works on integer arrays. To get around this we are going to write a template function that works with any data type (provided it supports the < operator). All primitives should support this and if objects overload the operator < then we can use them also. The template function begins with a line Code:
template<typename T> Instead of declaring an array of type int, we will declare an array of type T. This is what allows the method to accept an array of any data type. So we get this line: Code:
void selectSort(T arr[], int n) {
So instead of: Code:
int nTemp; Code:
T temp; Code:
template<typename T>
void selectSort(T arr[], int n) {
int iMin;
// a temporary variable used to sort the array.
// Notice it uses the type T that was defined in the template parameters?
T temp;
for (int i=0;i<n-1;i++){
iMin = i;
for (int j=i+1;j<n;j++) {
if (arr[j] < arr[iMin]) {
iMin = j;
}
}
if (iMin != i) {
// the smallest item is somewhere else in the array so move it to this position
temp = arr[i];
arr[i] = arr[iMin];
arr[iMin] = temp;
}
}
}
Code:
int nums[6] = {3,2,4,5,6,7};
selectSort(nums, 6);
for (int i=0;i<6;i++) {
cout << nums[i] << " ";
}
cout << endl;
Code:
float nums[6] = {3.2,2.1,4.2,5.6,6.8,7.3};
selectSort(nums, 6);
for (int i=0;i<6;i++) {
cout << nums[i] << " ";
}
cout << endl;
Sorting Objects We will use this class to demonstrate how to sort array objects. Code:
#include <iostream>
using namespace std;
class rect {
public:
int nArea;
int nLength;
int nWidth;
rect::rect(int width = 0, int length = 0) {
nWidth = width;
nLength = length;
}
void calcArea() {
nArea = nLength * nWidth;
}
void output() {
cout << nArea << " ";
}
bool operator<(const rect& r1) const {
return nArea < r1.nArea;
}
};
Let us see that this function works for sorting objects as well. We will also briefly note operator overloading. Add this code to your file: Code:
// demonstrating sorting an array of objects rect shapes[6]; shapes[0] = rect(3,2); shapes[1] = rect(4,3); shapes[2] = rect(5,3); shapes[3] = rect(8,3); shapes[4] = rect(9,4); shapes[5] = rect(3,1); Try this code after the array is declared: Code:
selectSort(shapes,6);
for (int i=0;i<6;i++) {
cout << "Width: " << shapes[i].nWidth << " Length: " << shapes[i].nLength;
cout << " Area: " << shapes[i].nArea << endl;
}
cout << endl;
The topic of operator overloading is a topic for another day. This is more of a practical explanation of templates. You can read more about templates here. The idea of templates parallels generics in the Java programming language. Enjoy! My Dev C++ project is uploaded as a zip file since I can't upload .h files.
__________________
"Whenever you remember, I'll be there/ Remember how we reached that dream together" - Carrie Underwood Last edited by chili5; 07-20-2009 at 10:28 AM.. |
|
||||
|
Re: Templates and Sorting
Very informative! +rep
__________________
Questions and Answers | Online News and Social Bookmarking | Code and Text Collaboration General Chat Forum |
|
||||
|
Re: Templates and Sorting
Excellent job! +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
||||
|
Re: Templates and Sorting
Arrays can be passed by reference or by value. It does look you aren't getting the results passed back from your function.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
||||
|
Re: Templates and Sorting
Very nice tutorial mate, I have down templates yet so this should make it a lot easier when I do
![]() +rep
__________________
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 11:19 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc