Data Structures and Algorithms

Total Page:16

File Type:pdf, Size:1020Kb

Data Structures and Algorithms

Cho, Joonmyun 18-4-5

Data Structures and Algorithms

ALGORITHMS AND EFFICIENCY

ALGORITMS EFFICIENCY DATA STRUCTURES WITH C++

STACKS QUEUES TREES

1/6 Cho, Joonmyun 18-4-5

Algorithms and Efficiency

Algoritms

Efficiency

In order for you to make an intelligent choice of which algorithm to use in a given situation, you need to have some idea of how the efficiencies of algorithms are calculated. There are three main measures of efficiency that can be applied to most algorithms: the best, worst, average case

The main point of working out the efficiency of an algorithm is to know how it behaves for large amounts of data; in particular, how fast the amount of work increases as the amount of data increases. linear, logarithmic, … Once you get used to the idea of classifying algorithms by the general way they depend on the amount of data, you will find a shorthand notation for efficiency to be quite handy; linear: O(n), which is read “order n” logarithmic: O(log n) (where n is a quantity that measures the amount of data being processed by the algorithm; n was equal to L when we were discussing list length above, for example)

Common orders of algorithms 1 Constant: O(1) 2 Logarithmic: O(log n) 3 Linear: O(n) 4 Log-linear: O(n log n) 5 Quadratic: O(n^2)

There are two types of operations involved in sorting a list of data; comparison and assignment. Since these two operations require different resources in terns of computing, we need to evaluate them separately. Assignment are almost more time-consuming than comparisons. In general, algorithms that minimize the number of assignments are to be preferred.

We find that there are L(L-1)(L-1)….. = L! ways of ordering the entire list. Now suppose that, with each swap, the number of final ordering of the list that needed to be considered was reduced by a factor 2. If the total number of possible ordering is a power of 2 (say, L! = 2^n), then the number of steps we need to completely sort the list is n  n = ln(L!) ~= L ln L where n = needed steps  efficiency ~= O(L ln L)  maximum efficiency

2/6 Cho, Joonmyun 18-4-5

Data Structures with C++

Stacks

The Main Features of a Stack  Access is allowed at one end (the top of the stack). Data are added and removed at this end  The action of adding a data element to a stack is called pushing  The action of removing a data element from a stack is called popping  Stacks handle data in a last-in-first-out, or LIFO fashion; the last item pushed onto the stack is always the first item popped off the stack

Implement push algorithm: 1 if stack is not full then 2 add 1 to the stack pointer 3 store item at stack pointer location pop algorithm: 1 if stack is not empty then 2 read item at stack pointer location I 3 subtract 1 from the stack pointer

C++ Code class charstack { protected: int StackSize; char* Element; int StackPointer;

public: BOOL Empty() const; BOOL Full() const; charstack(int StackSize = 10); virtual ~charstack(); virtual BOOL Pop(char& TopElem); virtual BOOL Push(const char& NewElem); }

3/6 Cho, Joonmyun 18-4-5

Queues

Features From the point of view of data structures, a queue is similar to a stack, in that data are stored in a linear fashion, and access to the data is allowed only of the ends of the queue The actions allowed on a queue:  creating an empty queue  testing if a queue is empty  adding data to the tail of the queue  removing data from the head of the queue

Implement Algorithms for dealing with queues in a circular array

Creating an empty queue: set head = tail = 0

Testing if a queue is empty: is head == tail ?

Testing if a queue is full: is (tail + 1) % QSize == head ?

Adding an item to a queue: if queue is not full, add item at location tail and set tail = ( tail + 1) % QSize

Removing an item from a queue: if queue is not empty, remove item from location head and set head = (head + 1) % QSize

C++ Code class intqueue { protected: int QSize; int* Element; int Head, Tail;

public: BOOL Empty() const; BOOL Full() const; intqueue(int queuesize = 10); virtual ~intqueue();

4/6 Cho, Joonmyun 18-4-5

virtual BOOL Remove(int& TopElem); virtual BOOL Add(const int& NewElem); }

5/6 Cho, Joonmyun 18-4-5

Trees

Features A tree is a datastructure consisting of data nodes connected to each other with pointers. Each node in a tree may be connected to two or more other nodes. Basic types of operation:  inserting a new node  deleting a node  listing or visiting the nodes of the tree

Implement Inserting the integers into a binary tree: if the current pointer is 0. create a new node, store the data, and return the address of the new node. otherwise, compare the integer to the data stored at the current node if the new integer is less than the integer at current node, insert the new integer into the left child of the current node

Inorder traversal: if the current pointer is not 0, then traverse the left child of the current pointer visit(or print) the data at the current pointer traverse the right child of the current pointer

C++ Code Because many of the binary tree functions are recursive, we often need to pass pointers to individual binary tree nodes to and from functions. So we are to encapsulate the data node class as a nested public class within the binary tree class

6/6

Recommended publications