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-24-2007, 09:36 PM
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: Palendrome Algorithm Runtime Comparison

In this tutorial, we will be creating a program that reads from a file named input.txt that contains the following:

Code:
10 11 12 13 14 14 13 12 11 10
10 11 12 13 12 11 10
10 11 12 10 11 12
We want to determine whether this is a PALENDROME or not with different type of Data Structures Algorithm and see the actual running time for each algorithm Sounds fun?

You may also want to refer to the previous tutorial that I made for Tutorial: How to read from ASCII files

Hopefully the code is self explanatory, and as usual, you will take your own risk by using my code
However, if you do have any questions regarding this program, feel free to throw me some questions

Data Structures Used in this Program:
1. Stack/Queue
2. Vector
3. Array
4. Doubly Linked List

PHP Code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;

import sun.misc.Queue;

public class Palendrome {
    
    
public Palendrome(String filename) {
        
        
double stack_t1stack_t2stack_r;
        
double vector_t1vector_t2vector_r;
        
double array_t1array_t2array_r;
        
double ll_t1ll_t2ll_r;
        
        
try {
            
BufferedReader br = new BufferedReader(new FileReader(filename));
            
String line br.readLine();
            
Stack s = new Stack();
            
Queue q = new Queue();
            
Vector v = new Vector();
            
String[] array = new String[99];
            
LinkedList ll = new LinkedList();
            
            
System.out.println("Result\tStack/Queue\tVector\tArray\tDoubly LL");
            
System.out.println("------\t-----------\t------\t-----\t---------");
            
            while (
line != null) {
                
String result null;
                if (
line.length() == 0) {
                    
// ignores blank line
                
} else {
                    
= new Stack();
                    
= new Queue();
                    
= new Vector();
                    array = new 
String[99];
                    
ll = new LinkedList();
                    
                    
StringTokenizer st = new StringTokenizer(line.trim(), " ");
                    
int cnt 0;
                    
int length 0;
                    while (
st.hasMoreTokens()) {
                        
String num st.nextToken();
                        
s.push(num);
                        
q.enqueue(num);
                        
v.add(num);
                        array[
cnt] = num;
                        
ll.add(num);
                        
cnt++;
                        
length++;
                    }
                    
// STACK/QUEUE
                    
stack_t1 System.nanoTime();
                    for (
int i 0lengthi++) {
                        if (!
s.pop().equals(q.dequeue())) {
                            
result "FALSE";
                        } else {
                            
result "TRUE";
                        }
                    }
                    
stack_t2 System.nanoTime();
                    
stack_r stack_t2 stack_t1;
                    
// VECTOR
                    
vector_t1 System.nanoTime();
                    for (
int i 0lengthi++) {
                        if (!
v.get(i).equals(v.get(v.size()-(i+1)))) {
                            
result "FALSE";
                        } else {
                            
result "TRUE";
                        }
                    }
                    
vector_t2 System.nanoTime();
                    
vector_r vector_t2 vector_t1;
                    
// ARRAY
                    
array_t1 System.nanoTime();
                    for (
int i 0cnti++) {
                        if (!array[
i].equals(array[cnt-(i+1)])) {
                            
result "FALSE";
                        } else {
                            
result "TRUE";
                        }
                    }
                    
array_t2 System.nanoTime();
                    
array_r array_t2 array_t1;
                    
// DOUBLY LINKED LIST
                    
ll_t1 System.nanoTime();
                    for (
int i 0lengthi++) {
                        if (!
ll.get(i).equals(ll.get(ll.size()-(i+1)))) {
                            
result "FALSE";
                        } else {
                            
result "TRUE";
                        }
                    }
                    
ll_t2 System.nanoTime();
                    
ll_r ll_t2 ll_t1;
                    
System.out.println(result "\t" stack_r "\t\t" vector_r "\t" array_r "\t" ll_r);
                }
                
line br.readLine();
            }
        } 
catch (FileNotFoundException fnfe) {
            
System.out.println("FileNotFoundException: " fnfe.getMessage());
        } 
catch (IOException ioe) {
            
System.out.println("IOException: " ioe.getMessage());
        } 
catch (InterruptedException ie) {
            
System.out.println("InterruptedException: " ie.getMessage());
        }
    }
    
    
public static void main(String[] args) {
        if (
args.length != 1) {
            
System.out.println("USAGE: java Palendrome [filename]");
        } else {
            new 
Palendrome(args[0]);
        }
    }

RESULT


Code:
Result    Stack/Queue    Vector    Array    Doubly LL
------    -----------    ------    -----    ---------
TRUE    37938.0        12117.0    5977.0    16898.0
TRUE    13526.0        5929.0    2171.0    7748.0
FALSE    14921.0        5396.0    2344.0    6596.0
Pretty neat program eh? TRUE means that it is a "PALENDROME"
The numbers are the time it takes to determine whether it's a palendrome or not for each Data Structures

__________________
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!

Last edited by HelloWorld : 07-25-2007 at 07:29 AM.
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:37 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