Sort Function

closed account (23vMDjzh)
My instructor gave us a program that consists of a <list> of doubles. Our assignment is to create a sort function that utilizes selection sort, insertion sort, or recursive quicksort algorithms. This is a University 200 level course, we can't simply use the sort function for lists and vectors. My problem is that the function's parameters that we must use are L.begin() and L.end(). These are bidirectional iterators and all examples I've seen either online, in the course, or in our text do not use these types of parameters.

Here is the sample program we have to edit:
http://gdansk.bradley.edu/olekmali/courses/bb-ee221/homework/B_sort_iterator.cpp.HTML

How can I adapt a function that uses an array or list container as a parameter to this assignment? I just learned that bidirectional iterators cannot use any boolean operators other than == and !=. Please help, thank you.

i.e.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void InsertionSort(int V[], int len) {
    for (int i=1; i<len; i++) {
        int elem=V[i]; // next element to be inserted
        int    j=i-1;
        while (j>=0 && elem<V[j]) {
            // move sorted elements until the right place
            // to insert elem or begin of array is reached
            V[j+1]=V[j];
            j--;
        }
        V[j+1]=elem;
    }
}
if the function should sort just a list (a vector if i see it right) u can make a function that accepts pointers (list.begin(), list.end()), which show to the first element and behinde the last one. when u send such ur parameters to such a function u need to add & like
InsertionSort(&l.begin(), &l.end() ); the function sorts the elements betwean the two pointers and u don't need a return.

hope it helps
closed account (23vMDjzh)
That did help me understand where the iterators/pointers are pointing to. Thank you.

The function is actually implemented as a template.
We're not allowed to edit the function's declaration so it looks like this:

1
2
3
4
template <typename IT> 
void MySort (IT beg, IT end){
     //sort algorithm
}


Does this use pass by reference even though it doesn't show the & sign?
No it uses pass by value. the iterators are objects that are being passed by value. An iterator could be a pointer to an element of an array but it could be more complex than that depending on the kind of iterator.

How can I adapt a function that uses an array or list container as a parameter to this assignment? I just learned that bidirectional iterators cannot use any boolean operators other than == and !=. Please help, thank you.


I don't really understand that question nor do I understand why a teacher would ask you to write a sort for a template class that has a member function that sorts. You'll be glad to know that once you are finished with the class you can begin your real education and you should mostly be educating yourself. Unfortunately we all need that piece of paper so we have to put up with these so called teachers for many years.
Topic archived. No new replies allowed.