# HW7A - p.257 7.4 Show the result of running Shellsort on...

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

p.257 7.4 Show the result of running Shellsort on the input 9, 8, 7, 6, 5, 4, 3, 2, 1 using the increments {1, 3, 7}. Answer Original 9 8 7 6 5 4 3 2 1 After 7-sort After 3-sort After 1-sort 2 2 1 1 1 2 7 4 3 6 3 4 5 5 5 4 7 6 3 6 7 9 9 8 8 8 9 p.258 7.14 How would you implement mergesort without using recursion? void merge( element list[ ], element sorted[ ], int i, int m, int n ) { /* merges the sorted lists ( list[ i ],…, list[ m ] ) and ( list[ m+1 ], …, list[ n ] ), into a single sorted list ( sorted[ i ],…, sorted[ n ] ) */ int j, k, t; j = m+1; /* index for the second sublist */ k = i; /* index for the sorted list */ while ( i <= m && j <= n) { /* while there are elements left in both lists */ if ( list[i].key <= list[j].key ) sorted[k++] = list[i++]; else sorted[k++] = list[j++]; } if ( i > m) /* merge whatever left to the rest of the list */ for ( t = j; t <= n; t++ ) sorted[ k+t-j ] = list[ t ]; else for ( t = i; t <= m; t++ ) sorted[ k+t-i ] = list[ t ]; } void merge_pass( element

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/16/2011 for the course CS 136 taught by Professor Yuechen during the Winter '08 term at Zhejiang University.

### Page1 / 3

HW7A - p.257 7.4 Show the result of running Shellsort on...

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

View Full Document
Ask a homework question - tutors are online