The Queue ADT - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

The Queue ADT

Description:

Here is a more detailed pseudo code description of the radix sort: Input: A queue Q of N items Output: Q sorted in ascending order Algorithm RadixSort (Q, N ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 12
Provided by: csCcsuEd4
Learn more at: https://cs.ccsu.edu
Category:
Tags: adt | queue | radix | sort

less

Transcript and Presenter's Notes

Title: The Queue ADT


1
The Queue ADT
  • Definition A queue is a restricted list, where
    all additions occur at one end,
  • the rear, and all removals occur at the other
    end, the front. This strategy is
  • known as first-in-first-out (FIFO) strategy.
  • Operations (methods) on queues
  • enqueue (item) Inserts item at the rear of
    the queue
  • dequeue () Removes the item from the
    front of the queue
  • size () Returns the number of
    items in the queue
  • empty () Returns true if the
    queue is empty
  • full () Returns true if the
    queue is full
  • front () Returns the item at
    the front of the queue without
  • removing it from
    the queue.

2
The Queue Interface in two versions
  • version 2
  • public interface QueueEx
  • public void enqueue (int item) throws

  • QueueFullException
  • public int dequeue() throws

  • QueueEmptyException
  • public int size()
  • public boolean empty()
  • public boolean full()
  • public int front() throws QueueEmptyException
  • version 1
  • public interface Queue
  • public void enqueue (int item)
  • public int dequeue()
  • public int size()
  • public boolean empty()
  • public boolean full()
  • public int front()

3
The Queue ADT -- an array implementation (version
1)
  • class QueueADT implements Queue
  • final int MAXSIZE 100
  • private int size
  • private int queueADT
  • private int front 0
  • private int rear -1
  • public QueueADT ()
  • size MAXSIZE
  • queueADT new intsize
  • public QueueADT (int inputsize)
  • size inputsize
  • queueADT new intsize
  • public boolean empty ()
  • public void enqueue (int number)
  • rear
  • queueADTrear number
  • public int dequeue ()
  • int i queueADTfront
  • front
  • return i
  • public int front ()
  • return queueADTfront
  • public int size ()
  • return (rear 1 - front)

4
The Queue ADT -- an array implementation (version
2)
  • class QueueEmptyException extends Exception
  • public QueueEmptyException (String message)
  • System.out.println (message)
  • class QueueFullException extends Exception
  • public QueueFullException (String message)
  • System.out.println (message)
  • class QueueADTEx implements QueueEx
  • final int MAXSIZE 100
  • private int size
  • private int queueADT
  • private int front 0
  • private int rear -1
  • public QueueADTEx ()
  • size MAXSIZE
  • queueADT new intsize
  • public void enqueue (int number) throws
    QueueFullException
  • if (full())
  • throw new QueueFullException ("The queue
    is full.")
  • rear
  • queueADTrear number
  • public int dequeue () throws
    QueueEmptyException
  • if (empty())
  • throw new QueueEmptyException ("The queue
    is empty.")
  • int i queueADTfront
  • front
  • return i
  • public int front () throws QueueEmptyException
  • if (empty())
  • throw new QueueEmptyException ("The queue
    is empty.")
  • return queueADTfront

5
Example application of the Queue ADT using
version 1
  • class QueueAppl
  • public static void main (String args) throws
    IOException
  • BufferedReader stdin new BufferedReader
    (new InputStreamReader(System.in))
  • System.out.print ("Enter queue size ")
  • System.out.flush()
  • int size Integer.parseInt(stdin.readLine())
  • QueueADT queue new QueueADT(size)
  • int i 2
  • while (!queue.full())
  • queue.enqueue(i)
  • System.out.println (queue.front() " is
    the front element.")
  • i i 2
  • System.out.println ("The current queue
    contains " queue.size() " elements.")

6
Example application of the Queue ADT using
version 2
  • class QueueApplEx
  • public static void main (String args) throws
    IOException
  • BufferedReader stdin new BufferedReader
    (new InputStreamReader(System.in))
  • System.out.print ("Enter queue size ")
  • System.out.flush()
  • int size Integer.parseInt(stdin.readLine()
    )
  • QueueADTEx queue new QueueADTEx (size)
  • int i 2
  • try
  • for (int j 1 j lt 7 j)
  • queue.enqueue(i)
  • System.out.println (queue.front() "
    is the front item.")
  • i i 2
  • catch (QueueFullException e)
  • System.out.println ("The queue is
    full.")
  • catch (QueueEmptyException e)

7
Radix sort another application of the Queue ADT
  • Sorting methods which utilize digital properties
    of the numbers (keys) in the
  • sorting process are called radix sorts.
  • Example. Consider the list 459 254 472
    534 649 239 432 654 477
  • Step 1 ordering wrt ones Step 2 ordering
    wrt tens Step 3 ordering wrt hundreds
  • 0 0
    0
  • 1 1
    1
  • 2 472 432 2
    2 239 254
  • 3 3
    432 534 239 3
  • 4 254 534 654 4 649
    4 432 459 472
    477
  • 5 5
    254 654 459 5 534
  • 6 6
    6 649
    654
  • 7 477 7
    472 477 7
  • 8 8
    8
  • 9 459 649 239 9
    9
  • After step 1 472 432 254 534 654 477
    459 649 239

8
Radix Sort the algorithm
  • Consider the following data structures
  • a queue for storing the original list and lists
    resulting from collecting piles at the end of
    each step (call these master lists)
  • ten queues for storing piles 0 to 9
  • Pseudo code description of radix sort at the
    idea level
  • start with the ones digit
  • while there is still a digit on which to
    classify data do
  • for each number in the master
    list do
  • add that number to
    the appropriate sublist
  • for each sublist do
  • for each number from
    the sublist do
  • remove the
    number from the sublist and append it
  • to a newly
    arranged master list

9
Radix Sort the algorithm (cont.)
  • Here is a more detailed pseudo code description
    of the radix sort
  • Input A queue Q of N items
  • Output Q sorted in ascending order
  • Algorithm RadixSort (Q, N)
  • digit 1
  • while StillNotZero (digit) do
  • for (i 1 to 10) do
  • create (sublisti)
  • while (! empty Q) do
  • dequeue (Q, item)
  • pile getPile (item, digit) 1
    O(N)
  • enqueue (sublistpile, item)
    swaps this outer loop will
    execute


  • digit times
  • reinitialize (Q)
  • for (j 1 to 10) do
  • while (! empty sublist(j)) do
  • dequeue (sublistj, item)
    O(N)
  • enqueue (Q, item)
    swaps

10
Efficiency of the Radix Sort
  • Operations that affects the efficiency of radix
    sort the most are dequeue-
  • enqueue swaps from and to the master list.
    Because the outer while-loop
  • executes C times, and each of the inner loops is
    O(N), the total efficiency
  • of radix sort is O(CN).
  • Notes
  • 1. If no duplicates are allowed in
    the list, we have log10 N lt C for non-negative
    integers.
  • 2. If there is a limit on the number
    of digits in the integers being sorted, we have C
    lt H log10 N.
  • Therefore, radix sort is O(N log N)
    algorithm if unique values are
  • sorted otherwise it is O(N) algorithm
    with a constant of proportionality,
  • C, which can be large enough to make C
    N gt N logN even for large N.
  • A disadvantage of radix sort is that it required
    a large amount of memory to
  • keep all of the sub-lists and the master list at
    the same time.

11
About the getPile method
  • The getPile method must return an appropriate
    isolated digit from the number
  • currently considered. That digit 1 yields the
    sublist, where the number is to
  • be enqueued.
  • A possible implementation of the getPile method
    is the following
  • int getPile (int number, int digit)
  • return (number (10 digit) / digit)
  • Examples number 1234
  • digit 100
  • (1234 1000) / 100 2
  • number 12345
  • digit 1
  • (12345 10) / 1 5
Write a Comment
User Comments (0)
About PowerShow.com