{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture_20_revised

lecture_20_revised - i scanf"%d" list i return 0...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

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

View Full Document Right Arrow Icon
Background image of page 2
Background image of page 3

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

View Full Document Right Arrow Icon
1 · insert.cc · 2010-10-28 08:31 · David Nicol #include <stdio.h> int GetArray( int [], int *); void InsertionSort( int [], int ); void PrintArray( const char *, int [], int ); int main() { int glist[10]; int glistlen=0; if (GetArray(glist, &glistlen) < 0 ) { return -1; } PrintArray( "------ unsorted list --------\n" , glist, glistlen); InsertionSort(glist,glistlen); PrintArray( "------ sorted list --------\n" , glist, glistlen); } int GetArray( int list[], int *n) { int i; printf( "enter list length between 1 and 10 elements: " ); scanf( "%d" , n); if ( *n<1 || *n > 10 ) return -1; for (i=0; i<*n; i++) { printf( "enter list[%d] : "
Background image of page 4
Background image of page 5

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

View Full Document Right Arrow Icon
Background image of page 6
Background image of page 7

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

View Full Document Right Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: , i ); scanf( "%d" , list+i); } return 0; } void InsertionSort( int list, int n) { int unsorted, sorted; int unsortedItem; for (unsorted=1; unsorted<n; unsorted++) { unsortedItem = list[unsorted]; for (sorted=unsorted-1; (sorted>=0) && (list[sorted]>unsortedItem); sorted--) { list[sorted+1] = list[sorted]; } list[sorted+1] = unsortedItem; } } void PrintArray( const char *msg, int list, int n) { int i; printf( "%s" , msg); for (i=0; i<n; i++) { printf( "%d%c" , list[i], ((i+1)%4? '\t' : '\n' )); } printf( "\n" ); }...
View Full Document

{[ snackBarMessage ]}