Week 3 Lectures - The Array Data Structure
Reference: Watt & Brown chapter 3. The Array
Java Arrays
ExampleBasic Array AlgorithmsInsert Component
Insert
Note that the original value Note also that the order of performing the copy operations in step 2 is important. Delete Component
Delete the value at position
Note that to ensure that no vital information is lost we would choose
Note also that the order of performing the copy operations in step 2 is important. Linear Search
Search for
Note that there is no 'correct' way to show that the target is not found. Sometimes an index value of -1 is used. Sorted Arrays
In a sorted array a[i] is 'less than or equal to' a[i+1] for i = left..right-1
The meaning of 'less than or equal to' will be determined by the type of component. An array can be inverse-sorted in a similar way.
Java provides comparison operators for comparing primitive types such
as char, integer or float. For reference types methods have to be
written to perform comparisons. Java 2 provides a
Linear Search - Sorted Array
Search for
Note that we will exit, on average, half way through the
traversal between Binary Search (recursive)See Watt & Brown 3.4.2 for a non-recursive version Search(target,a,left,right)- search for target in a[left..right]
This is an O(log n) algorithm.
Note that we could include a test for Sorting ExampleThe InsertionSort program makes use of the non-recursive Binary Search algorithm. See also the html file produced by javadoc. You will see from this html document that the InsertionSort program makes use of two other classes for reading from and writing to a text file. These classes are TextReader and TextWriter They are available for you to download and use if you wish. Note that on these pages they are stored as ".txt" files, so that the browser will display them. If you download them then you should save them in the directory that contains the Java class that will use them, and you should save them as ".java" files. Comparing Objects in Java
Java types char, int, short, long, float and double can all be compared using the
logical comparison operators (e.g. < and ==). However, more generally Java objects
have to use methods to perform comparisons - the comparison operators either will not
work, or will give a misleading result. For example, if variables
All objects have an
The Java String type can also make use of a This The Merge AlgorithmSee Watt & Brown 3.5 for the algorithm. Here is a sample Java method, which uses compareTo when comparing objects. Note the use of the Comparable class, which ensures that the Merge method will work for arrays of any Java object that implements the Comparable interface. Merge SortThe Merge Algorithm can be used to complete a complete sort of a range of values in an array. The Merge Sort algorithm can be described recursively as follows. Sort(a,left,right)
The array b should be declared outside the Sort method. If it is declared (or instantiated using "new") within the Sort method then there will be significant memory overheads caused by recursion. If declared outside the sort method it can be safely passed to the sort method as a parameter. |
You can obtain a text only version of this page by following the link.
CS2005 | Lectures | Practicals | Tutorials | Information |
![]() |