Uploaded by Dan De Dosan

data structure

advertisement
Data Structures and Algorithms
Data Structures & Algorithms
Acknowledgement
International college of Business and Technology
2
Data Structures & Algorithms
Table of Content
Executive Summery…………………………………………………………………………………..4
Introduction…………………………………………………………………………………………...5
Message queue …………..……………………………………………………………………...….6-9
Calculator (Stack)..………………………………………………………………..………….……9-12
Calculator(Link list)..……….………………………………………….………………….....….12-13
Sort Search…..…….…………………………….…………….………….………………….…14-16
Real world application(Stack,Queue)…………………………………………..……..………….17
General purpose data structures………………………………………….……………………..….18
Big O Notation…………………………………………………………..….……..…….18-19
Comparison of various sorting algorithms…………………………….……..……….…………..20-21
Conclusion……………………………………………………………………..……….…………...22
Bibliography……………………………………………………………..………………………...29
Appendix I:- Gantt Chart………..…………………………………………………………….…..30
International college of Business and Technology
3
Data Structures & Algorithms
Executive Summery
Introduction
Data structures are generally based on the ability of a computer to fetch and store data at any place in
its memory, specified by an address—a bit string that can be itself stored in memory and manipulated
by the program. Thus the record and array data structures are based on computing the addresses of
data items with arithmetic operations; while the linked data structures are based on storing addresses
of data items within the structure itself. Many data structures use both principles, sometimes
combined in non-trivial ways (as in XOR linking)
The implementation of a data structure usually requires writing a set of procedures that create and
manipulate instances of that structure. The efficiency of a data structure cannot be analyzed separately
from those operations. This observation motivates the theoretical concept of an abstract data type, a
International college of Business and Technology
4
Data Structures & Algorithms
data structure that is defined indirectly by the operations that may be performed on it, and the
mathematical properties of those operations (including their space and time cost).
Common data structures include: array, linked list, hash-table, graph,heap, Tree (Binary Tree, Btree, red-black tree, trie), stack, and queue.
International college of Business and Technology
5
Data Structures & Algorithms
Message Queue
Queues
A queue is a particular kind of collection in which the entities in the collection are kept in order and
the principal (or only) operations on the collection are the addition of entities to the rear terminal
position and removal of entities from the front terminal position. This makes the queue a First-InFirst-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be
the first one to be removed. This is equivalent to the requirement that once an element is added, all
elements that were added before have to be removed before the new element can be invoked. A queue
is an example of a linear data structure.
Representation of a FIFO Queue
International college of Business and Technology
6
Data Structures & Algorithms
I created super class called person for Stu class(Student)
Example code of person class
abstract class person {
public int stuid;
public String stuName;
public String stuMarks;
public person(int ID,String Name,String Marks)
{
stuid=ID;
stuName=Name;
stuMarks=Marks;
}
Example code of student class
public class STU extends person{
/** Creates a new instance of STU */
public STU(int ID,String Name,String Marks) {
super (ID,Name,Marks);
}
public int getID(){
return stuid;
}
public String getName(){
return stuName;
}
public String getMarks(){
return stuMarks;
International college of Business and Technology
7
Data Structures & Algorithms
Example code of queue
public class STUQE {
private int Maxsi;
private STU[] QueArr;
private int front;
private int rear;
private int nItems;
public STUQE(int s) {
Maxsi=s;
QueArr=new STU[Maxsi];
front=0;
rear=-1;
nItems=0;
}
Inserting and Removing datas in queue
public void insert(int ID,String name,String marks){
if (nItems !=Maxsi-1)
{
if(rear==Maxsi-1)
rear=-1;
STU Stuobj=new STU(ID,name,marks);
QueArr[++rear]=Stuobj;
nItems++;
}
}
public STU remove(){
STU temp=QueArr[front++];
if(front==Maxsi)
{
front=0;
}
nItems--;
return temp;
}
International college of Business and Technology
8
Data Structures & Algorithms
Use of Queue
compare queue with basic arrays , queue has numerous advantages over basic arrays. The most
important is that queue and arrays can simplify the process of interacting with data that are frequently
being added and removed from memory and for which the size is difficult to predict in advance.
queue can be interacted by using only two functions: pop (remove/retrieve) and push (add).
International college of Business and Technology
9
Data Structures & Algorithms
Stack Calculator
In computer science, a stack is a last in, first out (LIFO) abstract data type and data structure. A stack
can have any abstract data type as anelement, but is characterized by only three fundamental
operations: push, pop and stack top. The push operation adds a new item to the top of the stack, or
initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the
given item, the stack is then considered to be in an overflow state. The pop operation removes an item
from the top of the stack. A pop either reveals previously concealed items, or results in an empty
stack, but if the stack is empty then it goes into underflow state (It means no items are present in stack
to be removed). The stack top operation gets the data from the top-most position and returns it to the
user without deleting it. The same underflow state can also occur in stack top operation if stack is
empty.
A stack is a restricted data structure, because only a small number of operations are performed on it.
The nature of the pop and push operations also means that stack elements have a natural order.
Elements are removed from the stack in the reverse order to the order of their addition: therefore, the
lower elements are those that have been on the stack the longest
Simple representation of a stack
(source Wikipedia)
International college of Business and Technology
10
Data Structures & Algorithms
Example code of stack calculator
public class Stackcalculateone {
private int MaxSize;
private double[] StkArr;
private int top;
public Stackcalculateone(int s) {
MaxSize = s;
StkArr = new double[MaxSize];
top = -1;
}
Stacks have some useful terminology associated with them:

Push To add an element to the stack

Pop To remove an element from the stock

Peek To look at elements in the stack without removing them

LIFO Refers to the last in, first out behavior of the stack

FILO Equivalent to LIFO
International college of Business and Technology
11
Data Structures & Algorithms
Use of Stack
Linked-lists and stacks/queues are not really analogous data structures; often if you looks at the source
code queues and stacks are simply special types of linked-lists. This is mainly because adding and
removing
items
from
a
linked-list
is
generally
much
faster
than
using
an
array.
Link list
In computer science, a linked list is a data structure consisting of a group of nodes which together
represent a sequence. Under the simplest form, each node is composed of a datum and a reference (in
other words, a link) to the next node in the sequence; more complex variants add additional links. This
structure allows for efficient insertion or removal of elements from any position in the sequence.
Linked lists are among the simplest and most common data structures. They can be used to implement
several
other
common abstract
data
structures,
including stacks, queues,associative
arrays,
and symbolic expressions, though it is not uncommon to implement the other data structures directly
without using a list as the basis of implementation.
The principal benefit of a linked list over a conventional array is that the list elements can easily be
inserted or removed without reallocation or reorganization of the entire structure because the data
items need not be stored contiguously in memory or on disk. Linked lists allow insertion and removal
of nodes at any point in the list, and can do so with a constant number of operations if the link
previous to the link being added or removed is maintained during list traversal.
On the other hand, simple linked lists by themselves do not allow random access to the data, or any
form of efficient indexing. Thus, many basic operations — such as obtaining the last node of the list
(assuming that the last node is not maintained as separate node reference in the list structure), or
finding a node that contains a given datum, or locating the place where a new node should be inserted
— may require scanning most or all of the list elements.
International college of Business and Technology
12
Data Structures & Algorithms
Example code of Link Stack java class
public class LinkStackcc {
private LinkListcc Linklistone;
public LinkStackcc()
{
Linklistone = new LinkListcc();
}
Example Link Java Class
class Linkcc {
public double LLddata;
public Linkcc next;
public Linkcc(double dd)
{
LLddata=dd;
}
Linked lists vs. dynamic arrays
A dynamic array is a data structure that allocates all elements contiguously in memory, and keeps a
count of the current number of elements. If the space reserved for the dynamic array is exceeded, it is
reallocated and (possibly) copied, an expensive operation.
Linked lists have several advantages over dynamic arrays. Insertion or deletion of an element at a
specific point of a list, assuming that we have a pointer to the node (before the one to be removed, or
before the insertion point) already, is a constant-time operation, whereas insertion in a dynamic array
at random locations will require moving half of the elements on average, and all the elements in the
worst case. While one can "delete" an element from an array in constant time by somehow marking its
slot as "vacant", this causes fragmentation that impedes the performance of iteration.
International college of Business and Technology
13
Data Structures & Algorithms
Sort Search algorithms
In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order.
The most-used orders are numerical order and lexicographical order. Efficientsorting is important for
optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists
to work correctly; it is also often useful for canonicalizingdata and for producing human-readable
output. More formally, the output must satisfy two conditions:
1. The output is in nondecreasing order (each element is no smaller than the previous element
according to the desired total order);
2. The output is a permutation, or reordering, of the input.
Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due
to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble
sort was analyzed as early as 1956. Although many consider it a solved problem, useful new sorting
algorithms are still being invented .Sorting algorithms are prevalent in introductory computer science
classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety
of core algorithm concepts, such as big O notation, divide and conquer algorithms, data
structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and
lower bounds.
International college of Business and Technology
14
Data Structures & Algorithms
Sample code for Buble Sort
public void bubbleSort()
{
int out,in;
for(out=nElements-1;out>0;out--)
{
for(in=0;in<out;in++)
{
if (a[in]>a[in+1])
swap(in,in+1);
}
Example code for the selection sort
public void selectionSort()
{
int out,in,min;
for(out=0;out<nElements-1;out++)
{
min=out;
for(in=out+1;in<nElements;in++)
{
if (a[in]<a[min])
min=in;
}
swap(out,min);
Example code of LinearSearch
public int LinearSearch(double searchKey)
{
int j;
for(j=0;j<nElements;j++)
{
if (a[j]==searchKey)
break;
}
if(j==nElements)
return nElements;
else
return j;
International college of Business and Technology
15
Data Structures & Algorithms
BinarySearch
public int BinarySearch(double searchKey)
{
selectionSort();
int lowerBound=0;
int upperBound=nElements-1;
int curIn;
while(true)
{
curIn=(lowerBound+upperBound)/2;
if(a[curIn]==searchKey)
return curIn;
else if(lowerBound>upperBound)
return nElements;
else {
if(a[curIn]<searchKey)
lowerBound=curIn+1;
else
upperBound=curIn-1;
}
International college of Business and Technology
16
Data Structures & Algorithms
Real world applications
Stack
Most microprocessors use a stack-based architecture. When a method is called, its return address and
arguments are pushed onto a stack, and when it returns, they’re popped off. The stack operations are
built into the microprocessor.
Queues
There are various queues quietly doing their job in our computer’s (or the network’s) operating
system. There’s a printer queue where print jobs wait for the printer to be available. A queue also
stores keystroke data as you type at the keyboard. This way, if you’re using a word processor but the
computer is briefly doing something else when you hit a key, the keystroke won’t be lost; it waits in
the queue until the word processor has time to read it. Using a queue guarantees the keystrokes stay in
order until they can be processed.
International college of Business and Technology
17
Data Structures & Algorithms
General-Purpose Data Structures
If you need to store real-world data such as personnel records, inventories, contact lists, or sales data,
you need a general-purpose data structure. The structures of this type that we’ve discussed in this
book are arrays, linked lists, trees, and hash tables. We call these general-purpose data structures
because they are used to store and retrieve data using key values. This works for general-purpose
database programs .
International college of Business and Technology
18
Data Structures & Algorithms
Big O Notation
In mathematics, computer science, and related fields, big-O notation (also known as big Oh
notation, big Omicron notation, Landau notation, Bachmann–Landau notation, and asymptotic
notation) (along with the closely related big-Omega notation, big-Theta notation, and little o
notation) describes the limiting behavior of a function when the argument tends towards a particular
value or infinity, usually in terms of simpler functions. Big O notation characterizes functions
according to their growth rates: different functions with the same growth rate may be represented
using the same O notation.
Although developed as a part of pure mathematics, this notation is now frequently also used in the
analysis of algorithms to describe an algorithm's usage of computational resources: the worst case or
average case running time or memory usage of an algorithm is often expressed as a function of the
length of its input using big O notation. This allows algorithm designers to predict the behavior of
their algorithms and to determine which of multiple algorithms to use, in a way that is independent of
computer architecture or clock rate. Because big O notation discards multiplicative constants on the
running time, and ignores efficiency for low input sizes, it does not always reveal the fastest algorithm
in practice or for practically-sized data sets, but the approach is still very effective for comparing the
scalability of various algorithms as input sizes become large.
International college of Business and Technology
19
Data Structures & Algorithms
Source: - Wikipedia
Comparison of various sorting algorithms
International college of Business and Technology
20
Data Structures & Algorithms
The sorting algorithms
Insertion sort:
Insertion sort is good only for sorting small arrays (usually less than 100 items). In fact, the
smaller the array, the faster insertion sort is compared to any other sorting algorithm.
However, being an O(n2) algorithm, it becomes very slow very quick when the size of the
array increases. It was used in the tests with arrays of size 100.
Shell sort:
Shell sort is a rather curious algorithm, quite different from other fast sorting algorithms. It's
actually so different that it even isn't an O(nlogn) algorithm like the others, but instead it's
something between O(nlog2n) and O(n1.5) depending on implementation details. Given that it's
an in-place non-recursive algorithm and it compares very well to the other algorithms, shell
sort is a very good alternative to consider.
Heap sort:
Heap sort is the other (and by far the most popular) in-place non-recursive sorting algorithm
used in this test. Heap sort is not the fastest possible in all (nor in most) cases, but it's the defacto sorting algorithm when one wants to make sure that the sorting will not take longer than
O(nlogn). Heap sort is generally appreciated because it is trustworthy: There aren't any
"pathological" cases which would cause it to be unacceptably slow. Besides that, sorting inplace and in a non-recursive way also makes sure that it will not take extra memory, which is
often a nice feature.
Merge sort
The virtue of merge sort is that it's a truely O(nlogn) algorithm (like heap sort) and that it's
stable (iow. it doesn't change the order of equal items like eg. heap sort often does). Its main
problem is that it requires a second array with the same size as the array to be sorted, thus
doubling the memory requirements.
In this test I helped merge sort a bit by giving it the second array as parameter so that it
wouldn't have to allocate and de-allocate it each time it was called (which would have
probably slowed it down somewhat, especially with arrays of the bigger items). Also, instead
of doing the basic "merge to the second array, copy the second array to the main array"
procedure like the basic algorithm description suggests, I simply merged from one array to the
other alternatively.
Quicksort:
Quicksort is the most popular sorting algorithm. Its virtue is that it sorts in-place (even though
it's a recursive algorithm) and that it usually is very fast. On reason for its speed is that its
inner loop is very short and can be optimized very well.
International college of Business and Technology
21
Data Structures & Algorithms
The main problem with quicksort is that it's not trustworthy: Its worse-case scenario is O(n2)
(in the worst case it's as slow, if not even a bit slower than insertion sort) and the pathological
cases tend to appear too unexpectedly and without warning, even if an optimized version of
quicksort is used (as I noticed myself in this project).
Algorithm
Time
Notes
insertion-sort
O(n2) or
O(n+k)
excellent for small inputs
fast for 'almost' sorted inputs
merge-sort
O(n log n)
excels in sequential access
for huge data sets
quick-sort
heap-sort
O(n log n)
expected
in-place, randomized
O(n log n)
in-place
excellent generalized
fastest for in-memory
bucket-sort
radix-sort
O(d(n+N))
if integer keys & keys known
faster than quick-sort
International college of Business and Technology
22
Data Structures & Algorithms
Conclusion
I develop various data structures & algorithems using Java for this assignment. Here i develop all
programs using Netbeans IDE. It helps me to design good project.
I have used several sub classes and abstract classes in this project to overcome all the assignment
requirements. Stack calculator and Link list calculator have developed according to the stack and link
data structures.
I learned from this assignments

Developing a calculator using stack and stack list,Bubble sort,Selection sort,Binery
search,Sequential search,Real world usage,Implementing with netbeans
Finally I had to complete all programs well. There are some problems in calculators but finally I solve
all problems.
International college of Business and Technology
23
Data Structures & Algorithms
Bibliography
http://en.wikipedia.org/wiki/Big_O_notation
viewed : 15 September 2011
last modified on 15 September 2011
copyrights Wikimedia Foundation, Inc.,
http://en.wikipedia.org/wiki/Sorting_algorithm
viewed : 15 September 2011
last modified on 15 September 2011
copyrights Wikimedia Foundation, Inc.,
http://en.wikipedia.org/wiki/Linked_list
viewed : 15 September 2011
last modified on 15 September 2011
copyrights Wikimedia Foundation, Inc.,
http://en.wikipedia.org/wiki/Stack_(data_structure)
viewed : 15 September 2011
last modified on 15 September 2011
copyrights Wikimedia Foundation, Inc.,
International college of Business and Technology
24
Data Structures & Algorithms
APPENDIX I
Gantt chart
International college of Business and Technology
25
Download