dsIntro

dsIntro - Algorithms and Data Structures Objectives: To...

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

View Full Document Right Arrow Icon
    Algorithms and Data Structures Objectives:  To review the fundamental  algorithms and data structures that are  commonly used in programs.  To see how to  use and implement these algorithms and data  structures in different languages and to see  what language and library support exists for  them.
Background image of page 1

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

View Full DocumentRight Arrow Icon
    Topics Arrays and Vectors Lists Linear/Binary Search Quicksort Hash Tables - Dictionaries C, C++, Java, Perl
Background image of page 2
    Arrays Sequence of items Indexable (can refer to any element  immediately, by its index number) (Conceptually) contiguous chunks of  memory 0 5 1 2 3 4 6
Background image of page 3

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

View Full DocumentRight Arrow Icon
    Arrays Access: constant-time ( θ (1)) Searching: Sorted array -  θ (lg(n)) Unsorted -  θ (n) Inserting/removing Unordered -  θ (1) (Add to end, or swap last guy w/deleted guy) Ordered -  θ (n) Need to make (or fill in) a hole, move n/2 elements  (on average) to maintain relative order
Background image of page 4
    Resizing Arrays If the number of elements in an array is not  known ahead of time it may be necessary  to resize the array. Involves dynamic memory allocation and  copying To minimize the cost it is best to resize in  chunks (some factor of its current size)
Background image of page 5

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

View Full DocumentRight Arrow Icon
    Libraries - Arrays Arrays (lists) in Perl, Bash, Python, Awk,  resize automatically, as do C++ vectors,  and the Array in Java This does  not  mean that it is free.   Depending on the task, what goes on  underneath the hood may be important We shall create our own machinery in C
Background image of page 6
    Some C memory things… void* malloc(int n)  – allocates  n  contiguous bytes from heap,  returns address of first byte ( NULL  if it failed) free( void* p )  – returns memory addressed by  p  to the heap  (does  nothing  to  p  itself) void* memmove( void* d, void* s, size_t n )  – moves  n   bytes from  s  to (possibly overlaping region at)  d void* memcpy( void* d, void* s, size_t n )  – copies  n   bytes from  s  to ( non- overlaping region at)  d sizeof() – actually an operator, returns size, in bytes, of given  object or type void* realloc( void* src, size_t n )  – attempts to resize  array for you, and copy the stuff over.  Returns ptr to new location  (might be the same),  NULL  if it fails.
Background image of page 7

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

View Full DocumentRight Arrow Icon
    Growing Arrays in C enum { INIT_SIZE=1, GROW_FACTOR=2 }; int curr_size = INIT_SIZE; int nr_elems = 0; /* # of useful elements */ int *a = (int*)malloc( INIT_SIZE * sizeof( int )); … /* some stuff here */ /* attempt to insert 24 */ if( nr_elems >= curr_size ) { /* need to grow */ int *t = realloc( a, curr_size*GROW_FACTOR*sizeof( int )) if( t != NULL ) { /* success! */ curr_size *= GROW_FACTOR; a = t; a[nr_elems++] = 24; } else /* FAILURE! */ }
Background image of page 8
    Lists A sequence of elements Not  indexable (immediately) Space is allocate for each new element 
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/12/2008 for the course CS 265 taught by Professor ... during the Spring '07 term at Drexel.

Page1 / 54

dsIntro - Algorithms and Data Structures Objectives: To...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online