Docsity

Arrays and Linear Lists: Understanding Indexing and Data Structures, Slides of Aeronautical Engineering

Toggle sidebar
bg1
Linear lists
• Ordered collection of data
– data is arranged into some order (not
necessarily sorted)
– data is referenced by its position within the list
Operation Explanation
Initialize Initialize the internal structure of
the list, make sure it’s empty
Empty Returns true iff the list is empty
Insert Inserts a new element after the kth
element; if k is zero, then insert at
the beginning of the list
Delete Delete the kth item in the list
Arrays
• Data structure which groups related
items together
a day
6842757356937284
Marks
Marks(1) Marks(8)
about several different things
• the mark on a test for each student in a class
– related in that they record similar data
• the temperature on the hour, at each hour during
•etc
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Arrays and Linear Lists: Understanding Indexing and Data Structures and more Slides Aeronautical Engineering in PDF only on Docsity!

Linear lists

  • Ordered collection of data
    • data is arranged into some order (not necessarily sorted)
    • data is referenced by its position within the list

Operation Explanation Initialize Initialize the internal structure of the list, make sure it’s empty Empty Returns true iff the list is empty Insert Inserts a new element after the kth element; if k is zero, then insert at the beginning of the list Delete Delete the kth^ item in the list

Arrays

  • Data structure which groups related

items together

a day

Marks 84 72 93 56 73 75 42 68

Marks(1) Marks(8)

about several different things

  • the mark on a test for each student in a class
  • related in that they record similar data
  • the temperature on the hour, at each hour during
  • etc

Array index vs. array element

  • labels are going to be
    • index type range of values can the index take?
    • ENUMERATED TYPE

type of information can go into each box.

  • element type
  • any type

the array items

Example [1/3]

When designing an array, you need to decide what the

  • of value is the index?

the array index may be INTEGER, CHARACTER or any

the array element type can be

Courtesy of Chris Lokan. Used with permission.

the array what

  • what
  • what the array

the type of the array index is not related to the type of

Example [3/3]

constant := 8; constant := 'a'; constant := 'h';

subtype STRING8 is STRING (1 .. 8); type int_8_array (1 .. max_iarr) of INTEGER; type float_arrays (min_farr..max_farr) of FLOAT; type str_arrays (-5 .. 2) of STRING8; type small_arrays ('a' .. 'c') of FLOAT;

The declaration gives a name to the array type then can declare variables of that array type (^) arr1 : int_8_array; arr2 : float_arrays; arr3 : str_arrays;

Initializing Arrays

aggregate can be used to list

initial values for items in an array variable

coord1 : small_arrays := (1.2, 2.4, 3.6) ;

coord2 : small_arrays := ('c'=>3.6, 'b'=>2.4, 'a'=>1.2) ;

  • coord3 : small_arrays := ('b'=>5.2, others => 0.0 );

type small_arrays ('a' .. 'c') of FLOAT;

max_iarr : min_farr : max_farr :

is array is array is array is array

is array

-- various constants used in data types -- largest index in int array -- low index in float array -- high index in float array -- type declarations

  • An array
    • using positional notation
    • using explicit index references

-- init array coord1 using a positional list

-- init array coord1 using explicit index references

-- init array coord1 using others

Using Arrays

  • Referring to arrays

variable name.

  • Note: refer to the array variable , not the array type

specify the array variable name and the index value for the element you want.

PUT(coord1('b'));

total := coord1('a') + coord1('b') + coord1('c');

PUT(arr3(-2));

  • To refer to an entire array just use the array
  • To refer to an individual element in the array:

Array Attributes

  • Give info about the array type or array

variable

Attribute Meaning

‘ first The value of the smallest index ‘ last The value of the largest index ‘ range The entire range or index values ‘ length The number of items in the array

Array Attributes in Loops

setting the bounds of loop control

variables:

for t in count’

PUT(t);

PUT(count(t), width=>11); NEW_LINE;

for the array "count", regardless of the index type and range.

Operation on arrays

another of the same type

  • coord1 := coord2;

another of the same type

  • if (coord1 /= coord2) then PUT(“They are different”); ;

range loop

end loop

  • Assignment
    • Compares item by item

end if

  • A useful application of array attributes is
    • This causes "t" to take each index value in turn
      • You can assign one entire array variable to
  • Comparison
  • You can compare one array variable to

Operation on arrays

parameter to a procedure or function.

so depends on how the formal parameter was declared in the subprogram: unconstrained array type is used for the formal parameter, then any variable based on

parameter, then only variables of that type may

Unconstrained Arrays

constrained array types

so far

when the range of index values was specified

unconstrained array

types

not specified in type declaration

variable declarations

  • if an
  • if a constrained array type is used for the formal

be passed as an actual parameter

  • Arrays as Parameters
    • You can use an array variable as an actual
    • The amount of flexibility you have in doing

that type may be passed as an actual parameter.

  • We have only used
    • the size of array was specified in type declaration,
  • Ada also provides
    • element type is specified in type declaration
    • index type is specified in type declaration
    • range of index values (ie size) is
    • specify range of index values in

Insertion sort

  • Uses a fixed amount of storage beyond

what is needed for the data

  • InsertionSort(A)

-- A array of n numbers

for j in 2 to length of A loop

key := A(j)

i := j-

while i > 0 and A(i) > key

A(i+1) := A(i)

i := i-

A(i+1) := key

Insertion sort

Bubble Sort

last := length;

for I in 1 .. Last –1 loop

for J in I+1 .. Last loop

if List(I) < List(J) then

swap list(i) and list(j)

end if

end loop

end loop

Bubble Sort