Académique Documents
Professionnel Documents
Culture Documents
LOGO
IU-VNU-HCM
Contents
Array
Declaration, Creation and Process
Search
Big O notion
Simple Sort
Bubble Sort
Selection Sort
Insertion Sort
IU-VNU-HCM
LOGO
Lecture 2-Array
Company Logo
Data Stucture
W.R.T Organization
Sequential
By changing sequence, philosophy of data structure changes.
e.g. Array, Stack, Queue.
Hierarchical
To access data, we need to follow some hierarchy.
e.g. Trees.
General
Have no specified rules
e.g. Graphs
Company Logo
Definition
An array is a container object that holds a fixed number of values of a single
type. The length of an array is established when the array is created. After
creation, its length is fixed.
Creating Arrays
Company Logo
Company Logo
Company Logo
Process
Company Logo
Company Logo
class
class Array
Array
{
{
//
// have
have taken
taken some
some spacing
spacing liberties
liberties to
to fit
fit this
this on
on one
one page
page
private
private long[]
long[] a;
a;
//
// ref
ref to
to array
array a
a
private
private int
int nElems;
nElems;
public
public Array(int
Array(int max)
max)
Company Logo
//
// number
number of
of data
data items
items
//
// constructor
constructor
a
a =
= new
new long
long [max];
[max];
{
{
//
// create
create the
the array
array
nElems
nElems =
= 0;
0;
//
// no
no items
items yet
yet
}
} //
// end
end ConstructorConstructorpublic
public boolean
boolean find
find (long
(long searchKey)
searchKey)
{
{
//
// find
find specified
specified value
value
int
int j;
j;
for
for (j=0;
(j=0; j<nElems;
j<nElems; j++)
j++)
//
// for
for each
each element,
element,
if
if (a[j]
(a[j] ==
== searchKey)
searchKey)
//
//
/note
/note that
that the
the class
class provides
provides the
the search
search capability!
capability!
//
// found
found item?
item?
break;
break;
//
// exit
exit loop
loop before
before end
end
if(j
if(j ==
== nElems)
nElems)
Nice
Nice service.
service.
//
// Client
Client does
does not
not have
have to
to be
be concerned
concerned over
over how
how the
the search
search is
is undertaken!
undertaken!
//
// Search
Search was
was done
done in
in the
the app
app last
last time
time and
and NOT
NOT in
in the
the object..
object..
//
// gone
gone to
to end?
end?
return
return false;
false;
//
// yes,
yes, can't
can't find
find it
it
else
else
return
return true;
true;
}
}
//
// no,
no, found
found it
it
//
// end
end find()
find()
//----------------------------------------------------------//----------------------------------------------------------public
public void
void insert(long
insert(long value)
value)
//
// put
put element
element into
into array
array
{
{
//note
//note the
the services
services provided
provided by
by the
the object
object
a[nElems]
a[nElems] =
= value;
value;
//
// insert
insert it
it
//
// note
note the
the public
public interface.
interface.
nElems++;
nElems++;
//
// increment
increment size
size
//
// Note
Note that
that the
the object
object controls
controls all
all indexing,
indexing, looping,
looping, etc!!
etc!!
}
}
//----------------------------------------------------------//----------------------------------------------------------public
public boolean
boolean delete(long
delete(long value)
value)
{
{
//
// Again,
Again, note
note the
the service:
service:
int
int j;
j;
//
//
for(j=0;
for(j=0; j<nElems;
j<nElems; j++)
j++)
//
// look
look for
for it
it
if(
if( value
value ==
== a[j]
a[j] )
)
//
// Client
Client really
really doesnt
doesnt care
care about
about how
how the
the delete
delete is
is done.
done.
break;
break;
//
//
if(j==nElems)
if(j==nElems)
only
only that
that it
it IS
IS done.
done.
//
// can't
can't find
find it
it
return
return false;
false;
else
else
{
{
//
// found
found it
it
for(int
for(int k=j;
k=j; k<nElems;
k<nElems; k++)
k++) //
// move
move higher
higher ones
ones down
down
a[k]
a[k] =
= a[k+1];
a[k+1];
nElems--;
nElems--;
//
// decrement
decrement size
size
return
return true;
true;
}
}
}
}
//
// end
end delete()
delete()
//----------------------------------------------------------//----------------------------------------------------------public
public void
void display()
display()
//
// displays
displays array
array contents
contents
for(int
for(int j=0;
j=0; j<nElems;
j<nElems; j++)
j++)
System.out.print(a[j]
System.out.print(a[j] +
+ "
" ");
");
System.out.println("");
System.out.println("");
}//
}// end
end display()
display()
}
}
//
// end
end class
class Array
Array
delete
delete is
is accommodated
accommodated within
within object!
object!
Client
Client just
just requests
requests a
a simple
simple delete
delete service!
service!
//
// for
for each
each element,
element,
//
// display
display it
it
{
{
This
This is
is MUCH
MUCH better.
better.
Company Logo
Linear Search
Company Logo
Linear Search
Ordered Arrays
Will use an Ordered Array class to encapsulate the array
itself (data) and its methods (algorithms).
Most essential part of this example is, of course, the find
algorithm, which uses a binary search to find / not-find a
specific item.
Lets look at the method first.
Company Logo
Ordered Arrays
Company Logo
Binary search
Company Logo
Binary search
Company Logo
Binary search
Company Logo
Binary search
Comparisons Needed
10
100
1000
10
10,000
14
1,000,000,000
30
Please note that given table sizes (on the left), the values
on the right represent maximum number of probes to
find / not-find. (Next integer power of 2 higher than table size.)
Maximum Number
Comparisons Needed
10
10
100
50
100
1000
500
1000
10,000
5000
10,000
etc.
Recall::
Linear (sequential) search
Binary Search
n
n
Search times for two search algorithms and their cross over point
2**0 = 1
2**1 = 2
2**2 = 4
2**3 = 8
2**4 = 16
2**5 = 32
2**6 = 64
2**7 = 128
2**8 = 256
2**9 = 512
2**10 = 1024
2**11 = 2048
2**12 = 4096
2**13 = 8192
2**14 = 16384
2**15 = 32768
2**16 = 65536
Equations log r = s
2
Is equivalent to: r = 2s
So,
r = 2**s
1024 = 2**10
Big O Notation
O(log2 n)
O(1)
O(n)
O(n)
must find and create space!
Deletion ordered array
O(n)
Says that deletions require an amount of time proportional to
the array size.
Storing Objects
Storing primitive variables simplifies
the program
But it is not representative of how
data storage structures are used in
the real world
Stored data items are combinations of
many fields
Will know how objects, rather than
variables of primitive types, can be
stored via an example
ClassDataArray.java
IU-VNU-HCM
LOGO
Company Logo
Bubble Sort
Bubble Sort
Company Logo
Bubble Sort
Company Logo
Selection Sort
Company Logo
Selection Sort
Company Logo
Selection Sort
Company Logo
Insertion Sort