Data Structures and Algorithms in Java Second Year Software Engineering
Introduction Computer: is a programmable machine that can store, retrieve and process data. Data: The representation of information in a manner suitable for communication or analysis by humans or machines. Data is classified into data types. e.g. char, float, int, etc. Data type: is a domain of allowed values and a set of operations on these values. Data Type Domain Operations boolean True, False &&,, =, etc. char ASCII =, >, <, etc. byte -2 7 to +2 7-1 +, -, =, = =,!, <, etc. short -2 15 to +2 15-1 +, -, =, = =,!, <, etc. 2
Data Types What is the output of the following code? Why? byte b=128; System.out.println( +b); Primitive data types: these data types also known as simple or atomic types, in which elements are single, non-decomposable data items. Primitive data types for representing basic information ( int age, double balance). Java primitive data types are: boolean, char, byte, short, int, long, float and double. 3
Data Types Cont. More complex structure can be constructed by combining primitive data types. A point in x-y plane by two integers. A list of scores for students on an assignment. Composite type(non-primitive): a data type whose elements are composed of multiple data items. Primitive types are partially defined by describing their domain of values, while composite types are partially defined by the relationship among their constituent values. Primitive types are defined by describing permitted operations, while composite types, the main operation of interest is accessing the elements. 4
Data Types Cont. Typically, there are structured and unstructured composite types. A structured data type is one in which the components are organized with respect to each other. The organization determines the method used to access individual components. An array is a structured data type whose components are accessed by their position. An unstructured data type is one in which the components are not organized with respect to each other. Classes and interfaces are examples of unstructured data type. 5
Data Types Cont. Non-primitive data types also known as objects, they can be constructed in Java language by a mechanism which is called reference variable, since they reference a memory location. From a defined class new object is created by new operator and returns a reference to that object. A new object of a certain type must be created by using of the new operator followed by a constructor for that type of object. A constructor is a special kind of method that is used to initialize newly created objects. 6
Data Types Cont. Point p; // declare the variable p to be of type of Point. p=new Point (); //create a new Point object and returns a reference to it, then then assigns the reference to the new object to the variable p. Number classes in Java provide to store numbers as objects. Byte b=new Byte(8); Short s=10; A sequence of characters forms a String object. String s=new String( Data Structure ); String s= Data Structure ; The use of new operator to: Allocate the object s memory space. Using the object's constructor to initialize this space. Assign the address of this space to reference variable. 7
Data Types Cont. Reference variable can be viewed as a pointer to some object. The reference variable like a holder for a remote control, and contains the object reference. 8
Data Types Cont. class TV { } int Brightness; int Contrast; int Color; // The Default Constructor of the TV Object TV() { } Brightness=0; Contrast=0; Color=0; Many references can point to the same object. i.e. many remote controls work on the same device. TV remote1=new TV(); TV remote2=remote1; //declare the variable remote2 to be of type of TV, then assigns remote2 to reference the same object as remote1. 9
Data Types Cont. Dot operator (".") is used to access and/or change the state of object. Changing the state of object by one object reference variable, the state changes to all references to it. System.out.println(""+remote1.Color); // 0 System.out.println(""+remote2.Color); // 0 remote1.color=10; System.out.println(""+remote1.Color); // 10 System.out.println(""+remote2.Color); // 0 or 10? remote2.color=25; System.out.println(""+remote2.Color); // 25 System.out.println(""+remote1.Color); // 10 or 25? 10
Concepts of Data Structure In a general sense, any data representation is a data structure. Example: An integer. More typically, a data structure is meant to be an organization for a collection of data items. Data structures include arrays, linked lists, stacks, binary trees, etc. Any organization for a collection of records can be manipulated by algorithms in various ways. The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. Rarely is one data structure better than another in all situations. 11
Data Structure Selection and Requirements 1. Analyze the problem to determine the resource constraints a solution must meet. 2. Determine the basic operations that must be supported. 3. Select the data structure that best meets these requirements. A data structure requires: n n n space for each data item it stores. time to perform each basic operation. programming effort. 12
Data Structures and Algorithms Usability Data structures and algorithms are good for real-world data storage, programmer s tools and modeling. In real world data storage, suppose you want to create a computer program for an index-card then: How would you store the data in your computer s memory? Would your method works for a hundred file cards? A thousand? A million? Would your method permit quick insertion of new cards and deletion of old ones? Would it allow for fast searching for a specified card? Suppose you wanted to arrange the cards in alphabetical order. How would you sort them? Some times data storage structures are accessed by program itself. 13
Data Structures and Algorithms Usability Cont. Programmer uses such structures as tools to facilitate some other operation. Stacks, queues, and priority queues are often used in this way. The graph is the most important data structure that is used in real world modeling. Airline routes representation between cities. 14
Data Structures and Algorithms Usability Cont. Tasks in a project. Connections in an electric circuit. 15
Data Structure Classification 16
Characteristics of Data Structures Data Structure Avantages Disadvantages Array Ordered array Quick insertion, very fast access if index known. Quicker search than unsorted array. Slow search, slow deletion, fixed size. Slow insertion and deletion, fixed size. Stack Provides last-in, first-out access. Slow access to other items. Queue Provides first-in, first-out access. Slow access to other items. Linked list Binary tree Quick insertion, quick deletion. Quick search, insertion, deletion (if tree remains balanced). Slow search. Deletion algorithm is complex. 17
Algorithms Problem: is a task to be performed Algorithm: is a way or a method in which the problem is solved. The algorithm gives us a recipe for solving the problem by performing a series of steps, where each step is completely understood and doable. Many algorithms can be used for solving a specific problem, i.e. the problem of sorting can be solved by many algorithms such as: Insertion sort Bubble sort Selection sort Shellsort Mergesort Others 18
Example Problem: find y = x to the power of 2 üalgorithm1: Multiply X by X üalgorithm2: add X to itself, X times ØProgram1: y=x*x; ØProgram1: for (int i = 0; i<x; i++) y +=x; Which one is better? Why? 19
Algorithm Properties An algorithm possesses the following properties: It must be correct. It must be composed of a series of concrete steps. There can be no ambiguity as to which step will be performed next. It must be composed of a finite number of steps. It must be terminated. 20
To Summarize: Data Structure is a particular way of storing and organizing data in a computer. So that it can be used efficiently. An algorithm is a recipe for solving a problem whose steps are concrete and clear. A program is an instantiation of an algorithm in a computer programming language. 21
22