Saturday 8 February 2014

Do you know how simple is programming? Try it yourself. AutoIT

You don't need to be IT expert to write a program, you can write your own programs with this script language:

 

AutoIT



There are a lot of complicated programming languages​​, but there are also simple language that everyone can use, one of them is AutoIT script language.

He is very simple and easy to learn. You can write simple and complicated programs for whatever you need.

How to start with AutoIT? You need to do 1. and 3. step on this link. After that you are ready to write your own programs.

I will show you how to make program to open your CD drive and close it in a loop every 1 minute. :D
We all wanted in some point to be a hackers, this is a closest you can be a hacker without knowing anything about programing.

So how to start? You can do like this, go to your desktop or folder you want to make programs and click right click, from menu select "New" then select AutoIT script. Name it like "CD Drive" or something like that. After you name it, you need to right click on it and click on Edit Script, by default if you just open it i will run script and nothing will be executed because you don't have any code on script.

So we click on Edit Script and now you see Script Editor, all you need to do now is to write your code and use your imagination. :)
We are building CD script, so what do we need? Script gonna be in a loop because we want that CD open's every 1 minute. So how to make a loop?

You can do a loop very easy and intuitive like this:
While 1=1

Wait! What? :D Yes this is a loop, if you think about it loop will do code while condition is correct, in our example 1 is always equal to 1 so our loop will do this always! :)

So how will the loop know where is the end of our code? We need to add this:
WEnd

W - represents while loop and End - end of the loop so every simple for now. :D

Hmm we want to do this loop every 60 second so we need to pause or "sleep" our program for 1 minute, how we gonna do that? Just add this:    sleep(time in millisecond)
So we need to our program sleeps for 60 second, 1 second has 1000 millisecond so we just add sleep method:
while 1=1

sleep(60000)
WEnd

We have our loop who will execute code in every minute in just 3 line of code's :D How cool is that? :)
Now we need to open a CD drive, you can say hmm that is hard, hmm no! Programing is simple because all you need to do is to call method who will do something that we need, so we need method to get CD Drive.

$var = DriveGetDrive( "CDROM" ) 

In this variable called "$var" we get all CD drives on computer, if you think what is this "$" sign means, that is AutoIT thing to say what is variable, you don't need to know anything about that...

So we have all drives in one variable, now we need to open it, so we need to iterate through all cd drive's and open it. We will do it like this:
If not @error Then
For $i = 1 to $var[0]
    CDTray($var[$i] , "open")
Next
EndIf

We first ask is there some kind of error in calling CD Drive's? If not we use for loop to iterate through all CD drives, so if we have 3 drives, with for we go from 1 to 3, we now use 1drive call method to open it: CDTray($var[$i] , "open")When we are finished with first drive we call method Next it will give us next drive, so for loop will now do same thing with second drive, after second is finished we call Next and call it for third drive. So with that 5 lines of codes we open all CD drives on computer. Our code look like this now:

While 1=1

$var = DriveGetDrive( "CDROM" )
If not @error Then
For $i = 1 to $var[0]
    CDTray($var[$i] , "open")
Next
EndIf

sleep(60000)
WEnd

And we finish it :D If you want to close it and after a few second you can use that same code but change "open" to "close" for operation and add sleep between open and close about 20-30 second and you are good. :D

Now we need to create executive program that we can send someone to make a joke with him. :)
In Script Editor menu you have Tools tab, open it and click "Build". That will call AutoIT function to make our program become executive or .exe file. If you don't have any mistakes with your code in your folder or desktop where did you create this script will be file NameOfTheScript.exe and all you need to do now is to zip it or rar it and send it to someone you want to go insane. :P

You can run it on your computer as well, if you want to stop it just use Ctrl+Alt+Delete and kill the program... :) 

This is a tutorial how to make CD Room open's with 10 lines of code and you don't need to be programer to do that. 
If you want to learn more try to read AutoIT documentation or just leave comment bellow and we will make new tutorial how to do similar crazy stuff with AutoIT... :)

If you like this post please like us on Facebook, subscribe, share it, do whatever you want with it... :)
Have a good one. M.L.


Sunday 2 February 2014

Best algorithms for sorting arrays - Java

Bubble sort, Insertion sort and Selection sort



Which one you will use depends on what do you need. Bubble sort is a simplest algorithm, it will do the job but he will be slow, if you need something faster try Selection Sort and Insertion Sort.

They use different methods of sorting if you want to know how they works read it on this links:

My example of implementation this 3 algorithm's, I add speed test so you will see results which one is the best.
Notice: If you are using algorithms for different things, test it in all 3 algorithms it can give different results.


public class Example {

    public static void insertion_sort (int array[]) {
        long start = System.nanoTime();
        for (int i = 1; i < array.length; i++) {
            int j = i;
            int B = array[i];
            while ((j > 0) && (array[j-1] > B)) {
                array[j] = array[j-1];
                j--;
            }
            array[j] = B;
        }
        long total = System.nanoTime() - start;
        System.out.println("Time needed for sorting: "
                + (total / 1000000.0));
    }

    public static void selection_sort (int array[]) {
        long start = System.nanoTime();
        for (int x = 0; x < array.length; x++) {
            int index_of_min = x;
            for (int y = x; y < array.length; y++) {
                if (array[index_of_min] < array[y]) {
                    index_of_min = y;
                }
            }
            int temp = array[x];
            array[x] = array[index_of_min];
            array[index_of_min] = temp;
        }
        long total = System.nanoTime() - start;
        System.out.println("
Time needed for sorting: "
                + (total / 1000000.0));
    }


    public static void bubble_sort( int a[] ) {
        long start = System.nanoTime();
        int i, j,t=0;
        for (i = 0; i < a.length; i++) {
            for (j = 1; j < (a.length-i); j++) {
                if (a[j-1] > a[j]){
                    t = a[j-1];
                    a[j-1] = a[j];
                    a[j] = t;
                }
            }
        }
        long total = System.nanoTime() - start;
        System.out.println("
Time needed for sorting: "
                + (total / 1000000.0));
    }
   
   
    public static void main (String [] args) {
        int i;
        int array[] = {12,9,3,5,7,9,11,17,22,15,4,99,120,1,3,10};
        System.out.println();
        bubble_sort(array);
        System.out.print("After bubble sort
algorithm:\n");
        for(i = 0; i <array.length; i++)
        System.out.print(array[i]+"  ");
        System.out.println();
       
        int p;
        int array2[] = {12,9,3,5,7,9,11,17,22,15,4,99,120,1,3,10};
        System.out.println();
        selection_sort(array2);
        System.out.print("After selection sort algorithm:\n");
        for(p = 0; p < array2.length; p++)
        System.out.print(array2[p]+"  ");
        System.out.println();
       
        int c;
        int array3[] = {12,9,3,5,7,9,11,17,22,15,4,99,120,1,3,10};
        System.out.println();
        insertion_sort(array3);
        System.out.print("After insertion sort
algorithm:\n");
        for(c = 0; c < array3.length; c++)
        System.out.print(array3[c]+"  ");
        System.out.println();

    }
}


If you see this as useful please subscribe, like it, share it do whatever you want with it.
Have a good one. M.L.