Fundamental Algorithms, Spring 1998
Assignment 2. Running time estimates for simple algorithms and sorting.
Given January 28, due February 10.
p = 1
for i = 1, ... , n {
for j = 2, ... , sqrt(i) {
if ( ( i / j ) * j == i ) break; // i is not prime
p_list(p) = i;
p = p + 1;
}
}
for k = 1, ... , n {
for j = 1, ... , k {
a(j) = rand();
}
mergesort(a,k,temp)0
sum = 0;
for j = 1, ... , k-1 {
sum = sum + ( a(j+1) - a(j) ) * ( a(j+1) - a(j) );
}
}
cout << " the sum is " << sum << endl; // Never mind this line.
The routine rand() produces a different random number in between 0
and 1 each time it is called.