Skip to document

Dsa using java-1 - Java concepts on DSA

Java concepts on DSA
Course

Programming fundamentals (CO102)

62 Documents
Students shared 62 documents in this course
Academic year: 2022/2023
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
Delhi Technological University

Comments

Please sign in or register to post comments.

Preview text

DSA using Java - Quick Guide

What is a Data Structure?

####### Data Structure is a way to organized data in such a way that istructure. t can be used efficiently. Following terms are foundation terms of a data

• Interface only provides the list of supported operations, type of parame − Each data strucure has an interface. Interface represents the set of operations that a dters they can accept and return type of these operations. atastructure supports interface

• Implementation of the alogrithms used in the opreations of the data stru − Implementation provides the internal representation of a data structure. Implementacture. tion also provides the defination

Characteristics of a Data Structure

• • CorrectnessTime Complexity − Data Structure implementation should implement − Running time or execution time of operations of data stits interface correctly. ructure must be as small as possible.

• Space Complexity − Memory usage of a data structure operation should be as little as possible.

Need for Data Structure

####### As applications are getting complex and data rich, there are three common problems applications face now-a-days.

• Data Search million(10 6 ) items every time slowing down the search. As data grows, search wil − Consider an inventory of 1 million(10 6 ) items of a store. If application is to search an item. It l become slower. has to search item in 1

• • Processor speedMultiple requests − Processor speed although being very high, falls limited if da − As thousands of users can search data simultaneously on a web server,even very fast server fails while ta grows to billon records.

####### searching the data.

####### To solve above problems, data structures come to rescue. Data canrequired to be search and required data can be searched almost instantly. be organized in a data structure in such a way that all items may not be

Execution Time Cases

####### There are three cases which are usual used to compare various data structure's execution time in relative manner.

• Worst Case case time is ƒ(n) then this operation will not take time more t − This is the scenario where a particular data structure han ƒ(n) time where ƒ(n) represents function of n takes maximum time it can take. If a operation's wo rst

• Average Case time in execution then m operations will take mƒ(n) time. − This is the scenario depicting the average execution time of an operation of a data structure. If a operation takes ƒ(n)

• Best Case ƒ(n) time in execution then actual operation may take time as rand − This is the scenario depicting the least possible executionom number which would be maximum as ƒ(n). time of an operation of a data structure. If a operation takes

DSA using Java - Environment Setup

Local Environment Setup

####### If you are still willing to setup your environment for Java pJava on your machine. Please follow the following steps to serogramming language, then this section guides you on how to t up the environment. download and set up

####### Java SE is freely available from the link Download Java. So you download a version based on your operating system.

####### Follow the instructions to download java and run the would need to set environment variables to point to correct in .exe to install Java on your machine. Once you installed Java on youstallation directories: r machine, you

Setting up the path for windows 2000/XP

####### Assuming you have installed Java in c:\Program Files\java\jdk directory:

• • Right-click on 'My Computer' and select 'Properties'. Click on the 'Environment variables' button under the 'Advanced' tab.

• Now alter the 'Path' variable so that it also contains the 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINpath to the Java executable. Example, if the path is currently DOWS\SYSTEM32;c:\Program Files\java\jdk\bin'. set to

Setting up the path for windows 95/98/ME

####### Assuming you have installed Java in c:\Program Files\java\jdk directory −

  • Edit the 'C:\autoexec' file and add the following 'SET PATH=%PATH%;C:\Program Files\java\jdk\bin' line at the end:
Setting up the path for Linux, UNIX, Solaris, FreeBSD:

####### Environment variable PATH should be set to point to where thehave trouble doing this. java binaries have been installed. Refer to your shell documentation if you

####### Example, if you use bash as your shell, then you would add the following line to the end of your '.bashrc: export PATH=/path/to/java:$PATH'

Popular Java Editors

####### To write your java programs you will need a text editor. Thereconsider one of the following: are even more sophisticated IDE available in the market. But for now, you can

  • Notepad −Netbeans − On Windows machine you can use any simple text editor like Nis a Java IDE that is open source and free which can be downloaded from otepad (Recommended for this tutorial), TextPad. netbeans/index.html.
  • Eclipse − is also a java IDE developed by the eclipse open source community and can be downloaded from eclipse/.
What is Next?

####### Next chapter will teach you how to write and run your first jadeveloping applications. va program and some of the important basic syntaxes in java needed for

DSA using Java - Algorithms

Algorithm concept

####### Algorithm is a step by step procedure, which defines a set ofdata structures, following are the categories of algorithms. instructions to be executed in certain order to get the desired output. In term of

  • SearchSort − Algorithms to sort items in certain order − Algorithms to search an item in a datastrucure.
  • InsertUpdate − Algorithm to insert item in a − Algorithm to update an existing item in a data structuredatastructure
  • Delete − Algorithm to delete an existing item from a data structure
Algorithm analysis

####### Algorithm analysis deals with the execution time or running timebe defined as no. of computer instructions executed per operati of various operations of a data structure. Running time of on. As exact running time of any operation varies from one com puan operation can ter to

####### another computer, we usually analyze the running time of anoperation in a datastructure. y operation as some function of n, where n is the no. of items processed in that

Asymptotic analysis

####### Asymptotic analysis refers to computing the running time of any opeone operation is computed as f (n) and of another operation as g (n 2 ). Which means first operation running time will increase linration in mathematical units of computation. For example, runnearly with the ing time of

####### increase in n and running time of second operation will inoperations will be nearly same if n is significantly small. crease exponentially when n increases. Similarly the running time of both

Asymptotic Notations

####### Following are commonly used asymptotic notations used in calculating running time complexity of an algorithm.

  • • Ο NotationΩ Notation
Big Oh Notation, Ο• θ Notation

The Ο(n) is the formal way to express the upper bound oftime an algorithm can possibly take to complete. For example, for a function an algorithm's running time. It measures the worst case f (n) time complexity or longest amount of

Ο( f (n)) = { g (n) : there exists c > 0 and n 0 such that g (n) f c. f (n) for all n > n 0. }

####### Big Oh notation is used to simplify functions. For example, wConsider the scenario as follows: e can replace a specific functional equation 7nlogn + n - 1 with Ο( f (nlogn)).

DSA using Java - Arrays

Array Basics

####### Array is a container which can hold fix number of items and these to implement their algorithms. Following are important terms to understand the concepts of Array items should be of same type. Most of the datastructure make use of array

  • ElementIndex − Each location of an element in an array has a numerical ind − Each item stored in an array is called an element. ex which is used to identify the element.
Array Representation

####### As per above shown illustration, following are the important points to be considered.

  • • Index starts with 0. Array length is 8 which means it can store 8 elements.
  • Each element can be accessed via its index. For example, we can fetch element at index 6 as 9.
Basic Operations

####### Following are the basic operations supported by an array.

  • InsertionDeletion − delete an element at given index.− add an element at given index.
  • SearchUpdate − search an element using given index or by value.− update an element at given index.

####### In java, when an array is initialized with size, then it assigns defaults values to its elements in following order.

Data Type Default Value byte 0 short 0 int 0 long 0L float 0 double 0

char '\u0000' boolean false Object null

Demo package com.tutorialspoint;

public class ArrayDemo { public static void main(String[] args){ // Declare an array int intArray[]; // Initialize an array of 8 int intArray // set aside memory of 8 int = new int[ 8 ]; System.out("Array before adding data."); // Display elements of an array. display (intArray); // Operation : Insertion // Add elements in the array for{ (int i = 0 ; i< intArray; i++) // place value of i at index i. System.out("Adding " +i+" at index "+i); intArray } [i] = i; System.out(); displaySystem.(outintArray); ("Array after adding data."); // Operation : Insertion // Element at any location can be updated directly int index = 5 ; intArray [index] = 10 ; displaySystem.(outintArray); ("Array after updating element at index " + index); // Operation : Search using index // Search an element using index.System.out("Data at index " + index + ": "+ intArray[index]); // Operation : Search using value // Search an element using value value = 4 ; for{ (int i = 0 ; i< intArray; i++) if(SystemintArray[i] .==println value(value ){ + " Found at index "+i); } break; }System .out("Data at index " + index + ": "+ intArray[index]); } privateSystem static .voidprint display("Array : ["(int[]); intArray ){ for{ (int i = 0 ; i< intArray; i++) // display value of element at index i. System.out(" "+intArray[i]); }System .out(" ]"); } System.out(); }

  • InsertionDeletion − delete an element at the beginning of the list.− add an element at the beginning of the list.
  • DisplaySearch − search an element using given key.− displaying complete list.
  • Delete − delete an element using given key.
Insertion Operation

####### Insertion is a three step process:

####### 1. Create a new Link with provided data.

####### 2. Point New Link to old First Link.

####### 3. Point First Link to this New Link.

//insert link at the first locationpublic void insertFirst(int key, int data){ //create a linkLink link = new Link(key,data); link//point it to old first node = first; first //point first to new first node= link; }

Was this document helpful?

Dsa using java-1 - Java concepts on DSA

Course: Programming fundamentals (CO102)

62 Documents
Students shared 62 documents in this course
Was this document helpful?
DSA using Java - Quick Guide
What is a Data Structure?
Data Structure is a way to organized data in such a way that it can be used efficiently. Following terms are foundation terms of a data
structure.
Interface Each data strucure has an interface. Interface represents the set of operations that a datastructure supports.An interface
only provides the list of supported operations, type of parameters they can accept and return type of these operations.
Implementation − Implementation provides the internal representation of a data structure. Implementation also provides the defination
of the alogrithms used in the opreations of the data structure.
Characteristics of a Data Structure
Correctness − Data Structure implementation should implement its interface correctly.
Time Complexity − Running time or execution time of operations of data structure must be as small as possible.
Space Complexity − Memory usage of a data structure operation should be as little as possible.
Need for Data Structure
As applications are getting complex and data rich, there are three common problems applications face now-a-days.
Data Search Consider an inventory of 1 million(106) items of a store. If application is to search an item. It has to search item in 1
million(106) items every time slowing down the search. As data grows, search will become slower.
Processor speed − Processor speed although being very high, falls limited if data grows to billon records.
Multiple requests As thousands of users can search data simultaneously on a web server,even very fast server fails while
searching the data.
To solve above problems, data structures come to rescue. Data can be organized in a data structure in such a way that all items may not be
required to be search and required data can be searched almost instantly.
Execution Time Cases
There are three cases which are usual used to compare various data structure's execution time in relative manner.
Worst Case This is the scenario where a particular data structure operation takes maximum time it can take. If a operation's worst
case time is ƒ(n) then this operation will not take time more than ƒ(n) time where ƒ(n) represents function of n.
Average Case − This is the scenario depicting the average execution time of an operation of a data structure. If a operation takes ƒ(n)
time in execution then m operations will take mƒ(n) time.
Best Case This is the scenario depicting the least possible execution time of an operation of a data structure. If a operation takes
ƒ(n) time in execution then actual operation may take time as random number which would be maximum as ƒ(n).
DSA using Java - Environment Setup
Local Environment Setup
If you are still willing to setup your environment for Java programming language, then this section guides you on how to download and set up
Java on your machine. Please follow the following steps to set up the environment.
Java SE is freely available from the link Download Java. So you download a version based on your operating system.
Follow the instructions to download java and run the .exe to install Java on your machine. Once you installed Java on your machine, you
would need to set environment variables to point to correct installation directories:
Setting up the path for windows 2000/XP
Assuming you have installed Java in c:\Program Files\java\jdk directory:
Right-click on 'My Computer' and select 'Properties'.
Click on the 'Environment variables' button under the 'Advanced' tab.
Now alter the 'Path' variable so that it also contains the path to the Java executable. Example, if the path is currently set to
'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
Setting up the path for windows 95/98/ME
Assuming you have installed Java in c:\Program Files\java\jdk directory −