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-17-2007, 12:18 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,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon11 Tutorial: Simple Algorithm Analysis

** I just got pissed off because there was Database error for 1s and then got back again (by the time I clicked submit) ** And yeah, didn't save anything that I just wrote... yeah, really long... anyways.. haha... I'm bored anyways!!

Hello all~!
Come back to HelloWorld's Tutorial again!!! It's been a long time that I haven't wrote tutorial, and yeah here it is again, hopefully this tutorial would be helpful for everyone. Please don't hate, blame, or sue me (if you're in the U.S) if you got an F on your homework, test, or whatever you do... (got fired? lol..) Use this at your own risk. However, this tutorial has been tested and works on JVM 1.6 on Eclipse 3.2 Europa

I have had fun for these past days analyzing some algorithm and see the difference at their running time. I choose 3 algorithm that built on each other for this tutorial purpose: Arrays, ArrayList, and Vector

These Data Structures are built on each other:
Array is the foundation
ArrayList is built from Arrays
Vector is built from ArrayList as well as Arrays

However, please note that Vector and ArrayList are just exactly the same thing if you don't use it in the multithreading application. The only difference between Vector and ArrayList is that Vector is SYNCHRONIZED while ArrayList is NOT. So, it is make sense that Vector is running slower on the application that is not multi-threaded. You will see it in this program that you'll be creating as the part of this tutorial. If you're confused on what are all of this means, stop for a while and read the tutorial that I created about multithreading: http://www.programmerstalk.net/thread845.html

Hopefully the code and the comments are self explanatory. There's nothing that's too amazing to explain. However, feel free to throw me any questions regarding this tutorial in this thread

** CODE IS TESTED AND WORK ON JVM 1.6 **

PHP Code:
[left]import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

public class AlgorithmTest {

private static int n1 10000;
private static int n2 100000;
private static int n3 1000000;

// Selection Sort Array
public void ArraySelectionSort(int numbers[], int array_size)
{
int ij;
int mintemp;

for (
0array_size-1i++)
{
min i;
for (
i+1array_sizej++)
{
if (
numbers[j] < numbers[min])
min j;
}
temp numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}

// Selection Sort ArrayList
public void ArrayListSelectionSort(ArrayList<Integeral)
{
int ij;
int mintemp;

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

// 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);
}
}

public static void main(String[] args) {
Random r = new Random();

double t1t2;
double res;

int arr[] = new int[n1];
ArrayList<Integeral = new ArrayList<Integer>();
Vector<Integer= new Vector<Integer>();

// populate
for (int i 0arr.lengthi++) {
arr[i] = r.nextInt(1000000);
al.add(r.nextInt(1000000));
v.add(r.nextInt(1000000));
}
// end populate

// Arrays
// instantiate this class
AlgorithmTest at = new AlgorithmTest();
t1 System.nanoTime();
at.ArraySelectionSort(arrn1); // add populated Arrays
t2 System.nanoTime();
res t2 t1;
System.out.println("Array: " res);

// ArrayList
t1 System.nanoTime();
at.ArrayListSelectionSort(al); // add populated ArrayList
t2 System.nanoTime();
res t2 t1;
System.out.println("ArrayList: " res);

// Vector
t1 System.nanoTime();
at.VectorSelectionSort(v); // add populated vector
t2 System.nanoTime();
res t2 t1;
System.out.println("Vector: " res);
}
}
[/
left
The result that I get: (may vary)

Code:
Array: 1.6671426E8 ArrayList: 1.40868954E9 Vector: 8.764331389E9
Pay attention that it's E8, E9, and E9.. I assume that you know what are those means

Note that Vector must be the slowest from all 3 because of the explanation that I made earlier.

Thanx to whoever that helped me: Java API and my brain..

FOR ALL OF MY CLASSMATES: Better not just copy and paste! but well, you're lucky for coming to this site

__________________
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-17-2007 at 12:25 AM.
Reply With Quote
The Following User Says Thank You to HelloWorld For This Useful Post:
Leech (07-17-2007)
  #2 (permalink)  
Old 07-17-2007, 11:21 PM
Leech
Posts: n/a
Very nice, however, your code seems to work only with n1. When I replace n1 with either n2 or n3, it throws an ArrayIndexOutOfBoundsException.

The error points to the line if (numbers[j] < numbers[min]) in the ArraySelectionSort
and the line where we take in n1, n2 or n3.

__________________

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 Leech : 07-17-2007 at 11:23 PM.
Reply With Quote
The Following User Says Thank You to For This Useful Post:
HelloWorld (07-17-2007)
  #3 (permalink)  
Old 07-17-2007, 11:25 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,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Hehe, thanx for reminding me to test them over again at least
But when I tested it, it works perfectly fine. That's probably you have not changed both n2 on:

Code:
int arr[] = new int[n2];
Code:
        at.ArraySelectionSort(arr, n2); // add populated Arrays
It takes a while for me to complete the task though

__________________
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 05:24 PM. 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