Sequences, Summaries of Chemistry

The concept of sequences, including ranked sequences, positions, positional sequences, and general sequences. It provides an overview of the ranked sequence adt and its various methods, such as elematrank, replaceelematrank, insertelematrank, and removeelematrank. The document also covers the array-based implementation of the ranked sequence adt, including the time complexity of the methods. Additionally, it explores the implementation of sequences using a doubly linked list, including the java implementation and the comparison of sequence implementations. Finally, the document introduces the bubble sort algorithm and its implementation for an array-based sequence.

Typology: Summaries

2020/2021

Uploaded on 03/05/2024

sayna-azarfar
sayna-azarfar 🇨🇦

1 document

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1Sequences
SEQUENCES
Ranked Sequences
Positions
Positional Sequences
General Sequences
Bubble Sort Algorithm
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Sequences and more Summaries Chemistry in PDF only on Docsity!

SEQUENCES

  • Ranked Sequences
  • Positions
  • Positional Sequences
  • General Sequences
  • Bubble Sort Algorithm

The Ranked Sequence ADT

  • A ranked sequence S (with n elements) supports the

following methods:

  • elemAtRank( r ):

Return the element of S with rank r ; an

error occurs if r < 0 or r > n -

Input : Integer; Output : Object

  • replaceElemAtRank( r , e ):

Replace the element at rank r with e and

return the old element; an error condition

occurs if r < 0 or r > n - 1

Input : Integer r , Object e ; Output : Object

  • insertElemAtRank( r , e ):

Insert a new element into S which will

have rank r ; an error occurs if r < 0 or

r > n - 1

Input : Integer r , Object e ; Output : Object

  • removeElemAtRank( r ):

Remove from S the element at rank r ; an

error occurs if r < 0 or r > n - 1

Input : Integer; Output : Object

Array-Based Implementation

(contd.)

  • Time complexity of the various methods:

Method Time

size O (1)

isEmpty O (1)

elemAtRank O (1)

replaceElemAtRank O (1)

insertElemAtRank O ( n )

removeElemAtRank O ( n )

Implementation with a Doubly

Linked List

  • the list before insertion:
  • creating a new node for insertion:

header

Baltimore Paris Providence

trailer

header

Baltimore Paris Providence

trailer

New York

Java Implementation

public class NodeRankedSequence extends MyDeque implements Deque, RankedSequence{ public void insertElemAtRank ( int rank, Object element) throws BoundaryViolationException { if (rank != size()) //rank size() is OK for //insertion checkRank(rank); DLNode next = nodeAtRank(rank); // the new node //will be right before this DLNode prev = next.getPrev(); // the new node //will be right after this DLNode node = new DLNode(element, prev, next); next.setPrev(node); prev.setNext(node); size++; } public Object removeElemAtRank (int rank) throws BoundaryViolationException { checkRank(rank); DLNode node = nodeAtRank(rank); // node to //be removed DLNode next = node.getNext(); //node before it

Java Implementation (cont.)

DLNode prev = node.getPrev(); // node after it prev.setNext(next); next.setPrev(prev); size--; return node.getElement(); } private DLNode nodeAtRank (int rank) { // auxiliary method to find the node of the //element with the given rank DLNode node; if (rank <= size()/2) { //scan forward from head node = header.getNext(); for ( int i=0; i < rank; i++) node = node.getNext(); } else { // scan backward from the tail node = trailer.getPrev(); for ( int i=0; i < size()-rank-1 ; i++) node = node.getPrev(); } return node; }

Nodes and Positions (cont.)

  • Positions:
    • Inituitve notion of “place” of an element.
    • This concept allows us to enjoy doubly-linked list

without violating object-oriented design.

  • Positions have 2 methods:

element(): Return the element at the Position

Input: none; Output: Object

container():Return the sequence that contains

this position.

Input: none; Output: sequence

  • Positions are defined relatively.
  • Positions are not tied to an element or rank
  • A Sequence is a container of elements that are

each stored in a position

The Positional Sequence ADT

  • The methods are:
    • first()
    • last()
    • before()
    • after()
    • size()
    • isEmpty()
    • replace(p,e)
    • swap(p, q)
    • insertFirst(e)
    • insertLast(e)
    • insertBefore(p,e)
    • insertAfter(p,e)
    • remove(p)
    • isFirst(p)
    • isLast(p)

Doubly Linked List

Implementation(cont.)

return cont; } public Object element() throws InvalidPositionException { if (cont == null ) throw new InvalidPositionException (“Position has no container!”); return element; } // Accesor methods NSNode getNext() { return next; } NSNode getPrev() { return prev; } void setNext(NSNode newNext) { next = newNext; } // Update methods void setPrev(NSNode newPrev) { prev = newPrev; } void setElement(Object newElement) { element = newElement; } void setContainer(Container newCont) { cont = newCont; } }

Doubly Linked List

Implementation

  • Code for other methods of a Doubly Linked List:
    • checkPosition protected NSNode checkPosition(Position p) throws InvalidPositionException{
if (p==head)

throw new InvalidPositionException(“Head of the sequence is not a valid position”);

if (p==tail)

throw new InvalidPositionException (“Tail of the sequence is not a valid position”); }

  • first public Position first() throws EmptyContainerException { if (isEmpty()) throw new EmptyContainerException (“Sequence is empty”); return head.getNext(); }

Doubly Linked List

Implementation (cont.)

  • remove public Object remove(Position p) throws InvalidPositionException { NSNode n = checkPosition(p); numElts--; NSNode nPrev = n.getPrev(); NSNode nNext = n.getNext(); nPrev.setNext(nNext); nNext.setPrev(nPrev); Object nElem = n.element(); // unlink the position from the list //and make it invalid n.setNext( null ); n.setPrev( null ); n.setContainer( null ); return nElem; }

The Sequence ADT in Java

public interface PositionalSequence extends PositionalContainer {

/* *********** Accessor Methods *********** */

public Position first() throws EmptyContainerException;

public Position last() throws EmptyContainerException;

public Position before (Position p)

throws InvalidPositionException, BoundaryViolationException;

public Position after (Positionp)

throws InvalidPositionException, BoundaryViolationException;

The Sequence ADT in Java

(contd.)

/* *********** More Update Methods *********** */

public Position insertBefore (Positionp, Object
element)

throws InvalidPositionException;

public Position insertAfter (Positionp, Objectelement)

throws InvalidPositionException;

public Object remove (Positionp)

throws InvalidPositionException;

public Object replace (Positionp, Object element)

throws InvalidPositionException;

public void swap (Positionp, Positionq)

throws InvalidPositionException; }

Comparison of Sequence

Implementations

  • Is replaceElemAtRank O(1) in a list?????

Operations Array List

size, isEmpty O (1) O (1)

atRank, rankOf, elemAtRank O (1) O ( n )

first, last O (1) O (1)

before, after O (1) O (1)

replace, replaceElemAtRank, swap O (1) O (1)

insertElemAtRank, removeElemAtRank O ( n ) O ( n )

insertFirst, insertLast O (1) O (1)

insertAfter, insertBefore O ( n ) O (1)

remove O ( n ) O (1)