0% found this document useful (0 votes)
8 views41 pages

Data Structure PPT - Unit 1

The document provides an overview of data structures, focusing on arrays as a fundamental data storage mechanism. It covers array basics, terminology, operations such as searching and sorting, and various sorting algorithms including selection, insertion, and bubble sort. Additionally, it discusses the efficiency of these algorithms and introduces multidimensional arrays.

Uploaded by

gdrivee515
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views41 pages

Data Structure PPT - Unit 1

The document provides an overview of data structures, focusing on arrays as a fundamental data storage mechanism. It covers array basics, terminology, operations such as searching and sorting, and various sorting algorithms including selection, insertion, and bubble sort. Additionally, it discusses the efficiency of these algorithms and introduces multidimensional arrays.

Uploaded by

gdrivee515
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Data Structures

B.Tech – 2nd Sem


Unit – 1:
Introduction and Elementary Data
Structures

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.

• The operations can be:


– Storing new data
– Updating existing data
• Includes Searching, Sorting, applying mathematical, Logical Operations
etc.
– Deleting existing data
Data Structure Classification
Array Data Structure
• Array Basics
• Programming with Arrays
• Sorting Arrays
• Multidimensional Arrays
Overview
• An array
– a single name for a collection of data values
– all of the same data type
– subscript notation to identify one of the values
• Accessing each of the values in an array
– Usually a for loop or other looping construct
Array Terminology
Array name
temperature[n + 2]
Index - also called a subscript
- must be an int,
temperature[n + 2] - or an expression that evaluates to an int

Indexed variable - also called an element or


temperature[n + 2] subscripted variable
Value of the indexed variable
temperature[n + 2] = 32; - also called an element of the array

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

• The length attribute is established in the declaration and cannot be


changed unless the array is re-declared
Subscript Range

• Array subscripts use zero-numbering


– the first element has subscript 0
– the second element has subscript 1
– etc. - the nth element has subscript n-1
– the last element has subscript length-1
• For example: an int array with 4 elements

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

How many iterations are needed? Key:


smallest remaining value
sorted elements
Example: Selection Sort
• Notice the precondition: every array element has a value

• may have duplicate values

• broken down into smaller tasks


– "find the index of the smallest value"
– "interchange two elements"
– private because they are helper methods (users are not expected
to call them directly)
Insertion Sort
• Basic Idea:
– Keeping expanding the sorted portion by one
– Insert the next element into the right position in the sorted portion
• Algorithm:
1. Start with one element [is it sorted?] – sorted portion
2. While the sorted portion is not the entire array
1. Find the right position in the sorted portion for the next element
2. Insert the element
3. If necessary, move the other elements down
4. Expand the sorted portion by one
Insertion Sort: An example
• First iteration
– Before: [5], 3, 4, 9, 2
– After: [3, 5], 4, 9, 2
• Second iteration
– Before: [3, 5], 4, 9, 2
– After: [3, 4, 5], 9, 2
• Third iteration
– Before: [3, 4, 5], 9, 2
– After: [3, 4, 5, 9], 2
• Fourth iteration
– Before: [3, 4, 5, 9], 2
– After: [2, 3, 4, 5, 9]
Bubble Sort
• Basic Idea:
– Expand the sorted portion one by one
– “Sink” the largest element to the bottom after comparing adjacent elements
– The smaller items “bubble” up
• Algorithm:
– While the unsorted portion has more than one element
• Compare adjacent elements
• Swap elements if out of order
• Largest element at the bottom, reduce the unsorted portion by one
Bubble Sort: An example
• First Iteration:
– [5, 3], 4, 9, 2  [3, 5], 4, 9, 2
– 3, [5, 4], 9, 2  3, [4, 5], 9, 2
– 3, 4, [5, 9], 2  3, 4, [5, 9], 2
– 3, 4, 5, [9, 2]  3, 4, 5, [2, 9]
• Second Iteration:
– [3, 4], 5, 2, 9  [3, 4], 5, 2, 9
– 3, [4, 5], 2, 9  3, [4, 5], 2, 9
– 3, 4, [5, 2], 9  3, 4, [2, 5], 9
• Third Iteration:
– [3, 4], 2, 5, 9  [3, 4], 2, 5, 9
– 3, [4, 2], 5, 9  3, [2, 4], 5, 9
• Fourth Iteration:
– [3, 2], 4, 5, 9  [2, 3], 4, 5, 9
How to Compare Algorithms in Efficiency (speed)
• Empirical Analysis
– Wall-clock time
– CPU time
– Can you predict performance before implementing the algorithm?
• Theoretical Analysis
– Approximation by counting important operations
– Mathematical functions based on input size (N)
How Fast/Slow Can It Get?
(10G Hz, assume 1010 operations/sec)
N Nlog2N N2 2N

10 33 100 1,024

100 664 10,000 1.3 x 1030


(10-8 sec) (4 x1012 years)
1,000 9,966 1,000,000 Forever??

10,000 132,877 100,000,000 Eternity??


Theoretical Analysis (Sorting)
• Counting important operations
– Comparisons (array elements)
• >, <, …
– Swaps/moves (array elements)
• 1 swap has 3 moves
• Comparison is the more important operation—could be expensive
• Size of input (N) = Number of array elements
• Three cases for analysis
– Worst case (interesting, popular analysis)
– Best case (not so interesting)
Selection Sort
• Comparisons
– N – 1 iterations
– First iteration: how many comparisons?
– Second iteration: how many comparisons?
– (N – 1) + (N – 2) + … + 2 + 1 = N(N-1)/2 = (N2 – N)/2
• Moves (worst case: every element is in the wrong location)
– N – 1 iterations
– First iteration: how many swaps/moves?
– Second iteration: how many swaps/moves?
– (N – 1) x 3 = 3N - 3
Insertion Sort
• Comparisons (worst case: correct order)
– N – 1 iterations
– First iteration: how many comparisons?
– Second iteration: how many comparisons?
– 1 + 2 + … + (N – 2) + (N – 1) = N(N-1)/2 = (N2 – N)/2
• Moves (worst case: reverse order)
– N – 1 iterations
– First iteration: how many moves?
– Second iteration: how many moves?
– 3 + 4 + … + N + (N + 1) = (N + 4)(N - 1)/2 = (N2 + 3N - 4)/2
Bubble Sort
• Comparisons
– N – 1 iterations
– First iteration: how many comparisons?
– Second iteration: how many comparisons?
– (N – 1) + (N – 2) + … + 2 + 1 = N(N-1)/2 = (N2 – N)/2
• Moves (worst case: reverse order)
– N – 1 iterations
– First iteration: how many swaps/moves?
– Second iteration: how many swaps/moves?
– [(N – 1) + (N – 2) + … + 2 + 1] x 3 = 3N(N-1)/2 = (3N2 – 3N)/2
Summary of Worst-case Analysis
Comparisons Moves
(more important)
Selection (N2 – N)/2 3N - 3

Insertion (N2 – N)/2 (N2 + 3N - 4)/2

Bubble (N2 – N)/2 (3N2 – 3N)/2


Sorting Algorithm Tradeoffs
• Easy to understand algorithms
– not very efficient
– less likely to have mistakes
– require less time to code, test, and debug
– Selection, Insertion, Bubble Sorting algorithms
– Bubble Sort is the easiest to implement
• Complicated but more efficient
– useful when performance is a major issue
– programming project for Chapter 11 describes a more efficient sorting
algorithm

"Getting the wrong result is always inefficient."


Multidimensional Arrays
• Arrays with more than one index
– number of dimensions = number of indexes

• Arrays with more than two dimensions are a simple extension of two-dimensional (2-D)
arrays

• A 2-D array corresponds to a table or grid


– one dimension is the row
– the other dimension is the column
– cell: an intersection of a row and column
– an array element corresponds to a cell in the table
Table as a 2-Dimensional Array
• The table assumes a starting balance of $1000
• First dimension: row identifier - Year
• Second dimension: column identifier - percentage
• Cell contains balance for the year (row) and percentage (column)
• Balance for year 4, rate 7.00% = $1311

Balances for Various Interest Rates


Compounded Annually
(Rounded to Whole Dollar Amounts)
Year 5.00% 5.50% 6.00% 6.50% 7.00% 7.50%
1 $1050 $1055 $1060 $1065 $1070 $1075
2 $1103 $1113 $1124 $1134 $1145 $1156
3 $1158 $1174 $1191 $1208 $1225 $1242
4 $1216 $1239 $1262 $1286 $1311 $1335
5 $1276 $1307 $1338 $1370 $1403 $1436
… … … … … … …
Table as a 2-D Array
Column Index 4
(5th column)
Indexes 0 1 2 3 4 5
0 $1050 $1055 $1060 $1065 $1070 $1075
1 $1103 $1113 $1124 $1134 $1145 $1156
2 $1158 $1174 $1191 $1208 $1225 $1242
3 $1216 $1239 $1262 $1286 $1311 $1335
Row Index 3 4 $1276 $1307 $1338 $1370 $1403 $1436
(4th row) … … … … … … …

• Generalizing to two indexes: [row][column]


• First dimension: row index
• Second dimension: column index
• Cell contains balance for the year/row and percentage/column
• All indexes use zero-numbering
– Balance[3][4] = cell in 4th row (year = 4) and 5th column (7.50%)
– Balance[3][4] = $1311 (shown in yellow)
Summary (Part 1)
• An array may be thought of as a collection of variables,
all of the same type.
• An array also may be thought of as a single object with
a large composite value of all the elements of the
array.
Summary (Part 2)
• Array indexes use zero-numbering:
– they start at 0, so index i refers to the(i+1)th element;
– the index of the last element is (length-of-the-array - 1).
– Any index value outside the valid range of 0 to length-1 will cause an array index out
of bounds error when the program runs.
• A method may return an array.
• A "partially filled array" is one in which values are stored in an initial segment of the
array:
– use an int variable to keep track of how many variables are stored.
Summary (Part 3)
• An array element can be used as an argument to a method any place the base type
is allowed:
– if the base type is a primitive type, the method cannot change the array
element;
– if the base type is a class, the method can change the array element.
• When you want to store two or more different values (possibly of different data
types) for each index of an array,
– parallel arrays (multiple arrays of the same length)
– use a class that have multiple types/values.
• An accessor method that returns an array corresponding to a private instance
variable of an array type should be careful to return a copy of the array, and not
return the private instance variable itself (like any object).
Summary (Part 3)
• Sorting algorithms
– Selection
– Insertion
– Bubble
• Analysis
– Empirical
– Theoretical
• Comparisons: Quadratic-time (N2 ) algorithms
Summary (Part 4)
• Arrays can have more than one index.
• Each index is called a dimension.
• Hence, multidimensional arrays have multiple indexes,
– e.g. an array with two indexes is a two-dimensional array.
• A two-dimensional array can be thought of as a grid or table with rows and columns:
– one index is for the row, the other for the column.
• Multidimensional arrays in Java are implemented as arrays of arrays,
– e.g. a two-dimensional array is a one-dimensional array of one-dimensional arrays.
Thank You

You might also like