How to handin.
Points : 45
Weight : 3%
Due : Tuesday Jun 15, 2004 @ 11:00 PM
Note : Late assignments will be accepted only with the instructor's pre-approval.
int sum1 (int n) { return n * (n-1) / 2; }
int sum2 (int n) {
int total = 0;
for (int i = 1; i <= n; i++)
total = total + i;
return total;
}
void print1(int n) {
for (int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
cout << i+j << ' ';
cout << endl;
}
}
void print2(int n) {
for (int i = 0; i < n; i++) {
for ( int j = i; j < n; j++)
cout << i+j << ' ';
cout << endl;
}
}
void print3(int n) {
for (int i = 0; i < n; i++) {
for ( int j = n; j > 0; j--)
cout << i+j << ' ';
cout << endl;
}
}
void print4(int n)
for (int i = 0; i < n; i++) {
for ( int j = 0; j < 10; j++)
cout << i+j << ' ';
cout << endl;
}
}
void print5(int n) {
for (int i = 0; i < n; i++) {
int j = n;
while (j > 0) {
cout << i+j << ' ';
j = j/2;
}
cout << endl;
}
}
template <typename T>
void sort1(vector<T>& vec) {
int length = vec.size();
for (int last = length-1; last > 1; last--)
for (int currPos = 0; currPos < last; currPos++)
if (p[currPos+1] < p[currPos]) {
T temp = v[currPos];
v[currPos] = v[currPos+1];
v[currPos+1] = temp;
}
}
//******************************************************************
// function to sort a list using a smarter bubble sort.
//Post-condition: elements of list are sorted in nonincreasing order
//******************************************************************
template <typename T>
void bubble(vector<T> list) {
int currPos;
for (int last = list.size()-1; last > 1; last--)
for (int currPos = 0; currPos < last; currPos++)
if (list[currPos+1] < list[currPos])
swap(list[currPos],list[currPos+1]);
}
Assume the list is a vector which contains the following values:
48 23 37 5 34 17 11 41Trace the bubble sort function for the call
bubble(list);Show the contents of the list after each pass.
//*********************************************************************
// function to sort a list using the shell sort. Insertion version.
// Gap sequence used y: ... 31, 15, 7, 3, 1
//*********************************************************************
template <typename T>
void shell(vector<T> list) {
int length = list.size();
int gap; // select an initial gap between elements to be compared.
for (gap=1; gap < length; gap *=2); // smallest 2**k greater than length
gap/=2; gap--; // largest 2**k less than length then decrement
// now proceed to sort
while (gap > 0) {
for (int seqStart = gap; seqStart < length; seqStart++) {
T key = list[seqStart];
int currPos = seqStart;
for (;;) {
currPos -= gap;
if (currPos < 0 || list[currPos] < key) break;
list[currPos+gap] = list[currPos];
}
list[currPos+gap] = key;
}
gap /= 2;
}
}
Just show the contents of the list after each pass
University of Lethbridge Home Page