-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...ound/src/main/java/com/github/diegopacheco/sandbox/java/bigonotations/BigONotationOn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...nd/src/main/java/com/github/diegopacheco/sandbox/java/bigonotations/BigONotationOone.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ); | ||
} | ||
|
||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
java-guava-playground/src/main/java/com/github/diegopacheco/sandbox/java/md5/MD5HashFun.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |