Data Structure PPT - Unit 1
Data Structure PPT - Unit 1
Prepared by:
Neeraj Sharma (A.P, SOET)
Data Structures
• Data Structure is a data storage mechanism. We study the
specific storage and organization of data so as to improve
the efficiency of performing operations on data.
Note that "element" may refer to either a single indexed variable in the
array or the value of a single indexed variable.
Array Length
• Specified by the number in brackets when created with new
– maximum number of elements the array can hold
– storage is allocated whether or not the elements are assigned values
Subscript: 0 1 2 3
Value: 97 86 92 71
Subscript out of Range Error
• Using a subscript larger than length-1 causes a run time (not a compiler) error
– an ArrayOutOfBoundsException is thrown
• you do not need to catch it
• you need to fix the problem and recompile your code
• Other programming languages, e.g. C and C++, do not even cause a run time
error!
– one of the most dangerous characteristics of these languages is that they
allow out of bounds array indices.
Programming Tip: Use Singular Array Names
• Using singular rather than plural names for arrays improves readability
• Although the array contains many elements the most common use of the name
will be with a subscript, which references a single value.
• It is easier to read:
– score[3] than
– scores[3]
Initializing an Array's Values in Its Declaration
• Can be initialized by putting a comma-separated list in braces
• Uninitialized elements will be assigned some default value, e.g. 0 for int arrays
(explicit initialization is recommended)
• The length of an array is automatically determined when the values are explicitly
initialized in the declaration
Initializing Array Elements in a Loop
• A for loop is commonly used to initialize array elements
• For example:
int a[10],i;
for(i = 0; i < 10; i++)
a[i] = 0;
– note that the loop counter/array index goes from 0 to length – 1
Programming Tip:
Do not count on default initial values for array elements
– explicitly initialize elements in the declaration or in a loop
Arrays and Array Elements as Method Arguments
• Arrays and array elements can be
– used with functions/methods just like other objects
– be an argument in a function/method
– returned by methods/method
When Can a Method Change an
Indexed Variable Argument?
• primitive types are “call-by-value”
– only a copy of the value is passed as an argument
– method cannot change the value of the indexed variable
• class types are reference types (“call by reference”)
– pass the address of the object
– the corresponding parameter in the method definition becomes
an alias of the object
– the method has access to the actual object
– so the method can change the value of the indexed variable if it is
a class (and not a primitive) type
Behavior of Three Operations
Primitive Class Entire Array
Type Type Array Element
Assignment (=) Copy content Copy address Copy address Depends on
primitive/
class type
Equality (==) Compare Compare Compare Depends on
content address address primitive/
class type
Parameter Pass by value Pass by Pass by Depends on
Passing (content) reference reference primitive/
(address) (address) class type
Searching an Array
• There are many techniques for searching an array for a particular value
• Sequential search:
– start at the beginning of the array and proceed in sequence until either the value
is found or the end of the array is reached*
• if the array is only partially filled, the search stops when the last meaningful
value has been checked
– it is not the most efficient way
– but it works and is easy to program
* Or, just as easy, start at the end and work backwards toward the beginning
Sorting an Array
• Sorting a list of elements is another very common problem (along with searching a
list)
– sort numbers in ascending order
– sort numbers in descending order
– sort strings in alphabetic order
– etc.
• There are many ways to sort a list, just as there are many ways to search a list
• Selection sort
– one of the easiest
– not the most efficient, but easy to understand and program
Selection Sort Algorithm
for an Array of Integers
To sort an array on integers in ascending order:
1. Find the smallest number and record its index
2. swap (interchange) the smallest number with the first element of
the array
– the sorted part of the array is now the first element
– the unsorted part of the array is the remaining elements
3. repeat Steps 2 and 3 until all elements have been placed
– each iteration increases the length of the sorted part by one
Selection Sort Example
Problem: sort this 10-element array of integers in ascending order:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
7 6 11 17 3 15 5 19 30 14
1st iteration: smallest value is 3, its index is 4, swap a[0] with a[4]
before: 7 6 11 17 3 15 5 19 30 14
after: 3 6 11 17 7 15 5 19 30 14
2nd iteration: smallest value in remaining list is 5, its index is 6, swap a[1] with a[6]
3 6 11 17 7 15 5 19 30 14
3 5 11 17 7 15 6 19 30 14
10 33 100 1,024
• Arrays with more than two dimensions are a simple extension of two-dimensional (2-D)
arrays