Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
.
  • Loading branch information
diegopacheco committed Aug 19, 2015
1 parent 19f40dc commit db34d0f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.diegopacheco.sandbox.java.bigonotations;

public class BigONotationOn {

private static int[] theArray = new int[1000];
private static int sizeOfArray = 0;

public static void main(String[] args) {

oNBigONotation(60,1);
oNBigONotation(60,100);
oNBigONotation(60,1000);
oNBigONotation(60,100000);
oNBigONotation(60,10000000);

}

//
// O(n) -> Linear Search -> the operation time will be proporsional of the number of elements of the array.
//
private static void oNBigONotation(int value,int size){
long init = System.currentTimeMillis();
theArray = new int[size];
randomValues(size);

boolean found = false;
String index = "";

for(int i=0;i < (sizeOfArray-1) ; i++){
if(theArray[i]==value){
found = true;
index += i + " ";
}
}
System.out.println("Result Result: " + found);
System.out.println("Linear Search in the Array : " + (System.currentTimeMillis()-init) + " ms" );

sizeOfArray = 0;
}

private static void randomValues(int n){
for(int i=0; i < (n-1) ;i++){
sizeOfArray++;
theArray[i] = new Double((Math.random() * 1000)).intValue();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.diegopacheco.sandbox.java.bigonotations;

public class BigONotationOone {

private static int[] theArray = new int[1000];
private static int itemsOfArray = 0;

public static void main(String[] args) {
oOneBigONotation(1,1);
oOneBigONotation(13,1000);
oOneBigONotation(60,10000);
}

//
// O(1) -> Add One Item to the Array -> No matter the size of the array the operation time is constant.
//
private static void oOneBigONotation(int newItem,int size){
long init = System.currentTimeMillis();
theArray = new int[size];
theArray[itemsOfArray++] = newItem;
System.out.println("Add item Array Took : " + (System.currentTimeMillis()-init) + " ms" );
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.diegopacheco.sandbox.java.md5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5HashFun {

private static MessageDigest md = null;
static{
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
long init = System.currentTimeMillis();
String hash = hashIt(java.util.UUID.randomUUID().toString());
System.out.println("Hash Took : " + (System.currentTimeMillis()-init) + " ms" );
System.out.println("Hash: " + hash);
}

private static String hashIt(String base){
md.update(base.getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}

0 comments on commit db34d0f

Please sign in to comment.