|
Data
Structure Sample Questions |
void sort(int * array, int length) {
for(int j = lenght-2; j >= 0; j --) {
for(int i = 0; i < j; i ++) {
if(array[i+1] < array[i])
swap(i,i+1)
}
}
}
The input to sort are an array of length length and the length of
the array. swap is a function that swaps two elements in the array.
sort(int * array, int array_length)
{
for( int i = 0; i < array_length -2; i++) {
int indexMax = i;
for(int j=i+1; j < array_length - 1; j++)
{
if(array[indexMax] < array[j])
{
indexMax = j;
}
}
swap(array, i, max);
}
Give the number of comparisons of array elements. Prove your result
by induction on n, the number of elements in the array. (Here, assume
that the swap function swaps the two array elements given by the next two
parameters.)| © 2007 Thomas Schwarz, S.J., COEN, SCU | SCU | COEN | COEN350 | T. Schwarz |