Google
 
Web This Web site

Sunday, September 02, 2007

First Steps in the long Java road

In this first tutorial I just want to Help many of you (and myself) to do the first step in Java Programming.

We will not create the traditional "Hello Java" program, we start by a Java implementation of the Insertion Sort Algorithm:

Code Listing :

import java.util.Scanner;

public class First {
public static void main(String[] args) throws InterruptedException
{
int Tab[]= new int[10];
Scanner sc=new Scanner(System.in);
for (int i=0;i<10;i++) {
Tab[i]=sc.nextInt();
System.out.println(Tab[i]);
}

long start = System.currentTimeMillis();
Thread.sleep(20);
int buff=0;
for (int i=0;i<10;i++) {
for (int j=i+1;j<10;j++){
if (Tab[j]buff=Tab[i];
Tab[i]=Tab[j];
Tab[j]=buff;
}
}
}
long stop = System.currentTimeMillis();
System.out.println();
for (int i=0;i<10;i++) {
System.out.println(Tab[i]);
}
System.out.println( "Total Execution time " + stop-start );}
}



The first loop is to read the 10 numbers to be sorted, the User input will be 10 numbers separeted by spaces.

To be continued ...

Labels:

0 Comments:

Post a Comment

<< Home