// Vector and median
// December 3rd 2009
#include <iostream>
#include <vector>
#include <fstream>
usingnamespace std;
//template <typename T>
//double median(const vector<T> & v) ;
template <typename T>
void insertionSort(vector<T>& a_values);
template <typename T>
void fill_vector(ifstream& in_file, vector<T> & v)
{
T a_value;
while (!in_file.eof())
{
in_file >> a_value;
v.push_back(a_value);
}
}
int main() // begin main
{
vector<double> v;
char filename[16];
//double m = median(v);
cout << "Enter a file name: ";
cin >> filename;
cout << endl;
//double is = insertionSort(v);
ifstream fin(filename);
if (!fin)
{
cerr << "Could not open the file" << endl;
return 1;
}
fill_vector(fin,v);
cout << "Values entered: ";
vector<double>::iterator p;
for (p = v.begin(); p != v.end(); p++)
{
cout << *p << ",";
}
cout << endl;
insertionSort(v);
cout << "Sorted values: ";
vector<double>::iterator k;
for (k = v.begin(); k != v.end(); k++)
{
cout << *k << ",";
}
cout << endl;
//cout << "Median = " << m << endl;
} //end main
template <typename T>
void insertionSort(ifstream& in_file, vector<T>& a_value) //vector is used as an implementation of a List
{
int i, j, n = v.size(); //i is the pseudo-Code Index and j is the scanIndex
T temp;
// Select List[Index] as element to be inserted into sorted sublist.
// place v[i] into the sublistv[0] ... v[i-1], 1 <= i < n,
// so it is in the correct position
for (i = 1; i < n; i++)
{
// index j scans down list from v[i] looking for correct position to locate temp assigns it to v[j]
j = i;
temp = a_value[ i];
// find where to insert in the sorted sublistand shift elements to right while scanning
//from right-to-left
while ( j > 0 && target < a_value[ j-1 ])
{
// shift elements up list to make room for insertion
a_value[ j ] = a_value[ j-1];
j--;
}
// the location is found; insert temp
a_value[j] = temp;
}
}
//template <typename T>
//double median(const vector<T> & v)
//{
// double product = 0;
/*
if( v.size() % 2 )
{
product = v * (v.size() / 2);
}
else
{
product = (product = v * (v.size() / 2) + (v.size() / 2) ) / 2;
}
return product;
}*/
lol i thought that would be great. but yea an insertion sort takes for example a vector.
Vectors start out empty. and it gets the name insertionSort because as you insert a number via a file or just inserting it from main the function will place that number in a sorted fashion automatically.
i think you can change the function back to use the file as a paramater because i think that is what you are supposed to do. read in a number from the file and put the numbers into a vector. but make sure you put the number in the correct spot etc.
its really up to you though if you wanna read out of the file and sort them as you go or you can already have the vector and sort it. but either way take a look at the youtube video i posted. it is a very good explanation and it helps to visualize it. after that look at the link 2 posts up it has an example of an array. then after that make yours do the same but just for a vector.
template <typenameT>
void bubbleSort(vector<T>& v)
{
inti,j, n = v.size(); // index of last exchange
boolexchangeOccurs= true;
T temp;
// iis the index of last element in the current sublist
i= n-1;
// continue the process until we make no exchanges or
// we have made n-1 passes
while (i> 0 && exchangeOccurs)
{
// assume no exchange occurs
exchangeOccurs= false;
// scan the sublistv[0] to v[i]
for (j = 0; j < i; j++)
// exchange a pair and assign true to exchangeOccurs
if (v[j+1] < v[j])
{
temp = v[j];
v[j] = v[j+1];
v[j+1] = temp;
exchangeOccurs= true;
}
// move idownward one element
i--;
}
}