hw6-soln

# hw6-soln - ECE 209 Homework 6 Fall 2007 Homework 6 Due NAME...

This preview shows pages 1–3. Sign up to view the full content.

ECE 209 Fall 2007 Homework 6 Homework 6 Due: Oct 30, 2007 NAME: __________________________________________________________ ROW NUMBER: _______________ Print this file and answer in the space provided. Staple in the top left corner. Do not fold. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ECE 209 Fall 2007 Homework 6 1. (20 points) Write the selection sort algorithm (Sedgewick, Program 6.2) using pointer notation instead of array notation. void selection(Item *a, int l, int r) { int *iPtr, *jPtr; for (iPtr = a+l; iPtr < a+r; iPtr++) { int *minPtr = iPtr; for (jPtr = iPtr+1; jPtr <= a+r; jPtr++) { if (less(*jPtr, *minPtr)) minPtr = jPtr; } exch(*iPtr, *minPtr); } } /* or a version with fewer pointers */ void selectionAlt(Item *a, int l, int r) { int i, j; for (i=l; i < r; i++) { int min = i; for (j = i+1; j <= r; j++) { if (less(*(a+j), *(a+min)) min = j; } exch(*(a+i), *(a+min)); } } 2. (20 points) (a) What sort of array would maximize the number of exchanges performed in a bubble sort? An array which was already sorted in reverse order.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

hw6-soln - ECE 209 Homework 6 Fall 2007 Homework 6 Due NAME...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online