Vous êtes sur la page 1sur 41

IU-VNU-HCM

LOGO

Data Structures and Algorithms


Robert Lafore, Data structures and Algorithms in Java, Waite Group Press, 2002

Lecture 2-3. Array & Simple Sort

Trong Hai Duong, PhD


Email: haiduongtrong@gmail.com
Mobile phone: 0945090908
Homepage: http://co-intelligence.vn

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

Data Structures and Algorithms


Robert Lafore, Data structures and Algorithms in Java, Waite Group Press, 2002

Lecture 2-Array

Trong Hai Duong, PhD


Email: haiduongtrong@gmail.com
Mobile phone: 0945090908
Homepage: http://co-intelligence.vn

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.

Each item in an array is called an element, and each element is accessed


by its numerical index.

Declaration, Creation and


Process
Declaring Array Variables

Creating Arrays

Company Logo

Declaration, Creation and


Process
Do following problem
Create the array
Print all the array elements
Summing all elements
Finding the largest element

Company Logo

Declaration, Creation and


Process

Company Logo

public class TestArray {

Process

public static void main(String[] args) {


double[] myList = {5.6, 4.5, 3.3, 13.3, 4.0, 34.33,
34.0, 45.45, 99.993, 1112.3};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max);
}
}

Declaration, Creation and


Process
java.util.Arrays
public static int binarySearch(Object[] a, Object key)
public static boolean equals(long[] a1, long[] a2)
public static void fill(int[] a, int val)
public static void sort(Object[] a)

Company Logo

Company Logo

Insert, Delete and Search


a[nElems] = value;
// insert it
// note the public interface.
nElems++;
// increment size // Note that the object controls all indexing, looping, etc!!
//----------------------------------------------------------searchKey = 66;
// find item with key 66
for(j=0; j<nElems; j++)
// for each element,
// any difference here j++ or ++j ??
if(arr[j] == searchKey)
// found item?
break;
// yes, exit before end
// What does break do?
if(j == nElems)
// at the end?
System.out.println("Can't find " + searchKey);
// yes Will this always work???
else
System.out.println("Found " + searchKey);
//-------------------------------------------------------------searchKey = 55;
// delete item with key 55
for(j=0; j<nElems; j++)
// look for it
if(arr[j] == searchKey)
break;
for(int k=j; k<nElems; k++)
// move higher ones down // what if k = nElems? Will this work?
arr[k] = arr[k+1];
nElems--;
// decrement size // check: either we get a hit or not. Check both!
// What is wrong with this little routine?

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.

class ArrayApp Heres the Application!


{
Company Logo
public static void main(String[] args)
{
int maxSize = 100;
// array size
Array arr;
// reference to array - creates a reference to an object.
arr = new HighArray(maxSize); // create the array creates and initializes the object to 100 elements.
arr.insert(77);
// insert 10 items Note: we only supply value not
arr.insert(99);
// its the job of the object to insert the element into the array.
arr.insert(44);
// We have sent a message to arr to provide this service to us.
arr.insert(55);
arr.insert(22);
Note: we dont handle indices
arr.insert(88);
Note: we dont loop
arr.insert(11);
Note: we dont control the type of search!!!!
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
// display items
We simply request the object to provide those services!!
(assuming it can via its public interface that is,
assuming it is welldesigned for its clients).
int searchKey = 35;
// search for item
if( arr.find(searchKey) )
System.out.println("Found " + searchKey);
else
Still more: give parameter and request Search! No more!
System.out.println("Can't find " + searchKey);
We are abstracting out the details of these operations!!
Let the object that has responsibility for the data
arr.delete(00);
// delete 3 items
do the searching, manipulating, etc.
arr.delete(55);
Let client (HighArrayApp) merely request this service!
arr.delete(99);
arr.display();
// display items again
} // end main()
} // end class ArrayApp // Note how short main (driver) really is. It drives the app.

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

Binary Search Performance


So, what does all this mean?
These are easy to understand and essential to know.
Consider the table size discussion in book:
Binary Search
Table Size

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.)

Sequential (Linear) Search


performance
Sequential (Linear) Search
Average Nbr:
Table Size

Maximum Number
Comparisons Needed

10

10

100

50

100

1000

500

1000

10,000

5000

10,000

etc.

Potentially, every element might need to be looked at!

Average number of probes to find would be half the size


of the structure, such as 5, 50, 500 assuming elements
are equally-likely to be anywhere in the array
Maximum number of probes to find is the table size!

Recall::
Linear (sequential) search

Binary Search

n
n

Search times for two search algorithms and their cross over point

More on Ordered Arrays


Advantages:
If array is of sufficient size AND is ordered, search times are much
faster than with linear searches.
Ordered arrays super when look-ups (that is searches) are
frequent and array is of sufficient size.
But in ordered arrays adds/deletes are terrible.
But if maintenance (insertions, deletions) is not done a
lot, then this may be the best deal!)
Good examples:
Database of company employees and similar applications when adds
and deletes are few. (May batch up adds and deletes and run
overnight or off-line)
Large Tax tables relative stable. Inventories, Large / long lists of all
kinds that are not updated very frequently.

More on Ordered Arrays


Disadvantages:
Insert() horribly long.
If new item to be inserted belongs in a lower position of array,
movement of data items to insert in place can be horrific!
Delete() horribly long in BOTH ordered and unordered because
element to be deleted must be:
First: found, (easier, if there, in ordered array)
Then, remaining elements must be moved to cover up the
deleted elements location.

To start with: You must know


some integer powers of 2!

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

As unreasonable as this might appear to, perhaps,


a few of you, you need to be able to come up with
these without a calculator. They are basic!!!

Equations log r = s
2
Is equivalent to: r = 2s

So,

which I might write:

r = 2**s

1024 = 2**10

And log 1024 = 10


2

(2 raised to what power = 1024? 10)

Write relationship as a logarithm or as integer power of 2.

Insertions into an Unordered


Array = Constant
This is an easy one.
Unordered array can only be searched sequentially.
Insert algorithm does NOT depend on array size.
Item is merely placed in next available slot.
Same amount of time is always required, regardless of
size.
To insert: merely add the element to the end of the array.
Thus, the time is a constant, K.
So, T = K.
K is important and is used as a multiplier to account for the speed
of
the processor, and
other hardware features.

Insertions into an Ordered Array


= Terrible
Recall, insertions into an ordered array requires that all
of the items be moved up to provide room for the new
entry to maintain the order.
Performance is impacted by size, of course
and other factors influence the decision
to proceed this way
size of array
frequency of insertions / deletions ...

Big O Notation

We need a shorthand way to indicate


performance differences (efficiency)
of algorithms that search, sort, etc.
Enter the Big O Notation!

Bottom Lines Big O Notation


Big O Notation is read Order of is used
to indicate relative efficiencies.
Linear Search is said to be an O(n)
search:
Read, Order n search.

This says search time is directly proportional to the


number of elements being searched.
Think sequential search of an integer array.

Binary Search is an O (log2 n) search.


Says binary search time is proportional to log base
2 n.
Think: binary search of an ordered array.

Run Times using Big O Know


this Table
Algorithm
Linear Search
Binary Search

Running Time in Big O


(is proportional to) O(n)

O(log2 n)

Insertion unordered array


(immediate)
Insertion ordered array

O(1)
O(n)

Deletion unordered array

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.

Additional Relationships using


Big O Notation

Arrays for the World


Advantages and Disadvantages
Unordered Arrays:
A: insertions quick O(1)
D: search times can be quite slow O(n)
especially for large arrays. But if small n??
D: deletions very slow also (find and compress space)
O(n)
Ordered Array: (assumes array is sorted)
A: search times very quick (O(log2 n)) (if sufficient size)
D: insertion times: O(n) equally slow as unordered.
D: deletions: very slow also (find and compress space)
O(n)

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

Data Structures and Algorithms


Robert Lafore, Data structures and Algorithms in Java, Waite Group Press, 2002

Lecture 3-Simple Sort

Trong Hai Duong, PhD


Email: haiduongtrong@gmail.com
Mobile phone: 0945090908
Homepage: http://co-intelligence.vn

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

Vous aimerez peut-être aussi