Insertion Sort Algorithm using Generics in Java

Today, we are going to show the implementation of the Insertion Sort, which is the second algorithm of our series of tutorials on sorting algorithms. If you haven’t read the first tutorial on BubbleSort, I strongly recommend that you go there and read it, because we will reuse code that was explained there.

The application is really simple, it will just get an instance of a Sortable algorithm, which in this case will be an InsertionSort object and pass some Integer and String arrays to it, but any object that implements Comparable can be passed as a parameter as well, then it will print the elements from the arrays.

public class SorterApp {
  public static void main(String[] args) {
    SorterApp app = new SorterApp();
    app.run();
  }

  private void run() {
    sort(new Integer[] { 1, 2, 3, 4, 5 });
    sort(new Integer[] { 3, 1, 5, 4, 2 });
    sort(new Integer[] { 5, 4, 3, 2, 1 });

    System.out.println();

    sort(new String[] { "a", "b", "c", "d", "e" });
    sort(new String[] { "c", "a", "e", "d", "b" });
    sort(new String[] { "e", "d", "c", "b", "a" });
  }

  private <T extends Comparable<T>> void sort(T[] values) {
    Sortable sorter = newSortable(2);
    sorter.sortAscending(values);
    // sorter.sortDescending(values);
    Utils.printArray(values);
  }

  private Sortable newSortable(int sortableAlgorithm) {
    switch (sortableAlgorithm) {
      case 1:
        return new BubbleSort();
      case 2:
        return new InsertionSort();
      default:
        return new BubbleSort();
    }
  }
}

Continue reading Insertion Sort Algorithm using Generics in Java

Bubble Sort Algorithm using Generics in Java

This is our first tutorial about sorting algorithms and we are going to start by showing the Bubble Sort. It gets its name from the way smaller elements “bubble” to the top of the list.

We will create other sorting algorithms, so the first thing we need to do is prepare the structure of classes and packages that we are going to use.

Every sorting algorithm that we are going to create will have at least 2 public methods, which are: sortAscending and sortDescending. To make sure this will always be true, we are going to create an Interface called Sortable.

public interface Sortable {
  <T> extends Comparable<T> void sortAscending(T[] values);
  <T> extends Comparable<T> void sortDescending(T[] values);
}
Continue reading Bubble Sort Algorithm using Generics in Java

How to sync folders on Mac OS X with Dropbox using Symbolic Links

I decided to write this post because yesterday I was talking with some of my friends and we were discussing how amazing it is to have all (pretty much all) files stored in the cloud, thus, not having to worry about losing them because of a stolen laptop or a problem with the hard drive. Well, but some of them were complaining that the thing they hate the most is the fact that they have to put (move or copy) all the files and folders into the folder of the application they are using, i.e Dropbox, where you have to copy (in other words duplicate) all the files you want to share into the dropbox folder.

But I believe that the user should not have to adapt in order to use an application, it is the application that should be able to work for any type of user. I don’t know about you guys, but I have all my files and folders in a very well organized hierarchy that I don’t want to change it just because I want to share or have my files backed up.

Continue reading How to sync folders on Mac OS X with Dropbox using Symbolic Links

Sharing Eclipse preferences with your teammates

If you write Java code and works with Eclipse, you probably know that Eclipse allows you to change many of its default preferences, but once you’ve done that you will not want to do it again and again every time you change a computer or when there is a new teammate on your team, right?

Well, Eclipse allows you to export and import these preferences helping you not waste time.

To export you need to do the following:

Continue reading Sharing Eclipse preferences with your teammates

SQL Injection: The ultimate solution to prevent this from happening in your application

On this video, I show you the ultimate solution to prevent SQL Injection from happening in your application.

You can download all the files here: SQL_Injection.

Thank you for your time and feel free to leave any comments or questions.

Security vulnerabilities: should they be early detected?

Introduction

Whether you are a developer and/or just a normal user, if I ask you: What is a good software? What are the first thoughts that come to your mind? “Easy to use”, “Fast”, “Easy to update”. Am I right? Well, you probably thought about these ones because they are easy to spot, if a software is slow to perform a task or the “send” button is nowhere to be found, anyone can noticed that, but what about security? Can you tell if a software is secure and no one can hack it?

According to the OWASP, these are The Ten Most Critical Web Application Security Risks.

  1. (SQL/Command) Injection;
  2. Broken Authentication and Session Management;
  3. Cross-Site Scripting (XSS);
  4. Insecure Direct Object References;
  5. Security Misconfiguration;
  6. Sensitive Data Exposure;
  7. Missing Function Level Access Control;
  8. Cross-Site Request Forgery (CSRF);
  9. Using Known Vulnerable Components;
  10. Unvalidated Redirects and Forwards;

Continue reading Security vulnerabilities: should they be early detected?

How to show the contents of “Password” fields with JavaScript

Do you share a computer with someone? If the answer is “YES”, you should be careful on how you handle your passwords. Some browsers offer you a way to store your passwords for future usage so you can sign in without the need to type your information again.

Before you choose this option you should know that there is a way to retrieve your password from the browsers, even if that person is not you. If you know how to create a web page, you are probably asking yourself “But the type of the field is “password”, how is it possible?”. Well, that’s what I am going to show you.

If you know JavaScript, you should be familiar with the code below:
Continue reading How to show the contents of “Password” fields with JavaScript

How to Simulate Threads in JavaScript

Hello, Today I’m going to explain how you can simulate threads in JavaScript. Because JavaScript doesn’t have an explicit thread object, like C# or Java does. But it does have 2(two) methods (setTimeout and setInterval) that you can use in order to get at least some of the basic behaviors of threads.

You don’t have all the features like: sleep, suspend, resume and some others, but you can specify when to start and end it, with just these 2(two) I guaranty you can do a lot. Continue reading How to Simulate Threads in JavaScript