The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > Java


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-21-2007, 09:27 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon11 Tutorial: Stack & Queue Algorithm Analysis

Hello everybody, back again to HelloWorld's tutorial on Java
In this tutorial, we basically going to analyze Stack and Queue's algorithms that's going to be implemented differently using both Vectors and Arrays. As we learned before in Tutorial: Simple Algorithm Analysis

ArrayList and Vector are actually types of Data Structure that uses the fundamental Arrays. Then you probably may be able to guess what's the underline Data Structure for Stacks and Queues too!!! Guess what, Stack and Queues are actually use VECTOR as their underline data structure!!! Why Vector not ArrayList?? The answer is I don't know, and should probably ask Sun for that

In this tutorial, we're going to analyze different run-time when we implement our Stack and Queues using Vector, and Arrays. How long does it take time to POP, PEEK, and PUSH? How long does it take to ENQUEUE and DEQUEUE? as well as the SORTED version too

P.S we're going to create our own version of Stacks and Queues. If you don't know about this, below is a little intro:

Stacks and Queues are actually just other type of Data Structures that we have in Java. Here are the difference between them:

Stacks
You're washing dishes and have 10 plates (doesn't have to be 10, whatever number you have for your plates) and you stack them together once you're done washing them. IF you are going to use the plate, then of course you're not going to bother to get it from the bottom, so you will get the plate that's on the top. So is the same with Stacks, and in Computer Science, we call Stacks as LIFO (Last In First Out) Data Structure

Queues
You're about to watch movie, I hope that you don't cut the people in front of you because they will get mad!!! So, you have to wait in line (it's called queue in UK if I'm not wrong) if you get to be the first person in line, you will be the first one that gets the ticket and get out of the line. So, it is the same with Queues, and in Computer Science, we call Queues as FIFO (First In First Out) Data Structure

Hopefully that little introduction gives you some little more background about Stacks and Queues. I see that those two stories work best for me to understand Stacks and Queues, so that's the best that I can come up with. However, feel free to add and ask questions.

Also, hope that my code below is self explanatory. It's really, straight forward. You can see the way I implement different things there using Arrays and Vector. I believe that programmers are reading codes better than bunch of paragraphs

Stack.java - ARRAYS IMPLEMENTATION
PHP Code:
public class Stack {
    
private int size 10;
    
private Object o[] = new Object[size];
    
private Object temp[] = new Object[size];
    
private int pos 0;
    
    
public void push(Object in) {
        if (
pos == size-1) {
            for (
int i 0o.lengthi++) {
                
temp[i] = o[i];
            }
            
= new Object[2*size];
            
size *= 2;
            for (
int i 0temp.lengthi++) {
                
o[i] = temp[i];
            }
        }
        
o[pos] = in;
        
pos++;
    }
    
    
public Object pop() {
        return 
o[pos-- - 1];
    }
    
    
public Object peek() {
        return 
o[pos];
    }

Queues.java - USING ARRAYS IMPLEMENTATION

PHP Code:

public 
class Queue {
    
private int size 10;
    
private Object o[] = new Object[size];
    
private Object temp[] = new Object[size];
    
private int pos 0;
    
private Object temp2[] = new Object[size];
    
    
public void queue(Object in) {
        if (
pos == size-1) {
            for (
int i 0o.lengthi++) {
                
temp[i] = o[i];
            }
            
= new Object[2*size];
            
size *= 2;
            for (
int i 0temp.lengthi++) {
                
o[i] = temp[i];
            }
        }
        
o[pos] = in;
        
pos++;
    }
    
    
public Object dequeue() {
        for (
int i 0temp2.lengthi++) {
            
temp2[i] = o[i];
        }
        
= new Object[size-1];
        for (
int i 1temp2.lengthi++) {
            
o[i-1] = temp2[i];
        }
        return 
temp2[0];
    }

VectorStack.java - USING VECTOR IMPLEMENTATION
PHP Code:
import java.util.Vector;

public class VectorStack {
    
private Vector<Object= new Vector<Object>();
    
    
public void push(Object i) {
        
v.add(i);
    }
    
    
public Object pop() {
        return 
v.remove(v.size() - 1);
    }
    
    
public Object peek() {
        return 
v.get(v.size() - 1);
    }

VectorQueue.java - USING VECTOR IMPLEMENTATION
PHP Code:
import java.util.Vector;

public class VectorQueue {
    
private Vector<Object= new Vector<Object>();
    
    
public void enqueue(Object i) {
        
v.add(i);
        
// insert at beginning
        // v.add(0, i);
    
}
    
    
public Object dequeue() {
        return 
v.remove(0);
        
// remove from end
        // return v.remove(v.size() - 1);
    
}

VectorSortedQueue.java - USING VECTOR IMPLEMENTATION
(Sorry, this is only for Integers)
PHP Code:
import java.util.Vector;

public class VectorSortedQueue {
    
private Vector<Integer= new Vector<Integer>();
    
    
public void enqueue(Integer i) {
        
v.add(i);
        
sort();
        
// insert at beginning
        // v.add(0, i);
    
}
    
    
public Object dequeue() {
        return 
v.remove(0);
        
// remove from end
        // return v.remove(v.size() - 1);
    
}
    
    
public void sort() {
        
VectorSelectionSort(v);
    }
    
    
// Selection Sort Vector
    
public void VectorSelectionSort(Vector<Integerv)
    {
      
int ij;
      
int mintemp;

      for (
0v.size()-1i++)
      {
        
min i;
        for (
i+1v.size(); j++)
        {
          if (
v.get(j) < v.get(min))
            
min j;
        }
        
temp v.get(i);
        
v.set(iv.get(min));
        
v.set(mintemp);
      }
    }

VectorSortedStack.java - USING VECTOR IMPLEMENTATION
(This one also only for Integers)
PHP Code:
import java.util.Vector;

public class VectorSortedStack {
    
private Vector<Integer= new Vector<Integer>();
    
    
public void push(Integer i) {
        
v.add(i);
        
sort();
    }
    
    
public Object pop() {
        return 
v.remove(v.size() - 1);
    }
    
    
public Object peek() {
        return 
v.get(v.size() - 1);
    }
    
    
public void sort() {
        
VectorSelectionSort(v);
    }
    
    
// Selection Sort Vector
    
public void VectorSelectionSort(Vector<Integerv)
    {
      
int ij;
      
int mintemp;

      for (
0v.size()-1i++)
      {
        
min i;
        for (
i+1v.size(); j++)
        {
          if (
v.get(j) < v.get(min))
            
min j;
        }
        
temp v.get(i);
        
v.set(iv.get(min));
        
v.set(mintemp);
      }
    }


__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 07-21-2007, 09:28 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Part 2 Continue

TestMain.java - MAIN METHOD (Get all of the Time)
PHP Code:
import java.util.Random;


public class TestMain {
    
public static void main(String[] args) {
        
        
int n 10;
        
        
Queue q = new Queue();
        
Stack s = new Stack();
        
VectorQueue vq = new VectorQueue();
        
VectorStack vs = new VectorStack();
        
        
double t1 0t2 00;
        
boolean passed false;
        
        
// ENQUEUE & PUSH
        
        
for (int i 0ni++) {
            if (!
passed) {
                
t1 System.nanoTime();
            }
                
q.queue(i);
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("ARRAY QUEUE - ENQUEUE: " r);
            }
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
s.push(i);
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("ARRAY STACK - PUSH: " r);
            }
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
vq.enqueue(i);
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("VECTOR QUEUE - ENQUEUE: " r);
            }
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
vs.push(i);
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("VECTOR STACK - PUSH: " r);
                
passed true;
            }
        }
        
        
System.out.println();
        
passed false;
        
// PEEK
        
        
for (int i 0ni++) {
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
s.peek();
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("ARRAY STACK - PEEK: " r);
            }
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
vs.peek();
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("VECTOR STACK - PEEK: " r);
                
passed true;
            }
        }
        
        
System.out.println();
        
passed false;
        
// DEQUEUE & POP
        
        
for (int i 0ni++) {
            if (!
passed) {
                
t1 System.nanoTime();
            }
                
q.dequeue();
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("ARRAY QUEUE - DEQUEUE: " r);
            }
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
s.pop();
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("ARRAY STACK - POP: " r);
            }
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
vq.dequeue();
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("VECTOR QUEUE - DEQUEUE: " r);
            }
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
vs.pop();
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("VECTOR STACK - POP: " r);
                
passed true;
            }
        }
        
        
System.out.println();
        
passed false;
        
// SORTED STACK
        
Random random = new Random();
        
int range 100;
        
VectorSortedStack vss = new VectorSortedStack();
        
VectorSortedQueue vsq = new VectorSortedQueue();
        for (
int i 0ni++) {
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
vss.push(random.nextInt(range));
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("VECTOR SORTED STACK: " r);
            }
            if (!
passed) {
                
t1 System.nanoTime();
            }
            
vsq.enqueue(random.nextInt(range));
            if (!
passed) {
                
t2 System.nanoTime();
                
t2 t1;
                
System.out.println("VECTOR SORTED QUEUE: " r);
                
passed true;
            }
        }
    }

My Result: (Will be various)
Code:
ARRAY QUEUE - ENQUEUE: 594577.0
ARRAY STACK - PUSH: 5483.0
VECTOR QUEUE - ENQUEUE: 10013.0
VECTOR STACK - PUSH: 6660.0

ARRAY STACK - PEEK: 2396.0
VECTOR STACK - PEEK: 6818.0

ARRAY QUEUE - DEQUEUE: 9540.0
ARRAY STACK - POP: 3202.0
VECTOR QUEUE - DEQUEUE: 9063.0
VECTOR STACK - POP: 5902.0

VECTOR SORTED STACK: 32148.0
VECTOR SORTED QUEUE: 29111.0
IMPORTANT:
To my class mates: Don't just copy and paste!!!
Don't blame and sue me if you get an F, getting fired, or whatever problem that you may have by using my codes. Use it at your own risk

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:56 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50