Wednesday, November 27, 2013

Bubble Sort

Sort Average Best Worst Space Stability Remarks
Bubble sort O(n^2) O(n^2) O(n^2) Constant Stable Always use a modified bubble sort
Modified Bubble sort O(n^2) O(n) O(n^2) Constant Stable Stops after reaching a sorted array
Selection Sort O(n^2) O(n^2) O(n^2) Constant Stable Even a perfectly sorted input requires scanning the entire array
Insertion Sort O(n^2) O(n) O(n^2) Constant Stable In the best case (already sorted), every insert requires constant time
Heap Sort O(n*log(n)) O(n*log(n)) O(n*log(n)) Constant Instable By using input array as storage for the heap, it is possible to achieve constant space
Merge Sort O(n*log(n)) O(n*log(n)) O(n*log(n)) Depends Stable On arrays, merge sort requires O(n) space; on linked lists, merge sort requires constant space
Quicksort O(n*log(n)) O(n*log(n)) O(n^2) Constant Stable Randomly picking a pivot value (or shuffling the array prior to sorting) can help avoid worst case scenarios such as a perfectly sorted array.


this is bubble sort example



lets make it c language


#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
 int arr[] = {5, 1, 12, -5, 16, 2, 12, 14};

 int count;
 int i=0, j=0, k=0, temp;
 count = sizeof(arr)/sizeof(arr[0]);
 for(j; j<count -1; j++){
  for(i=0; i<count-1-j; i++){
   if(arr[i]> arr[i+1]){
    temp = arr[i];
    arr[i]= arr[i+1];
    arr[i+1]= temp;
   }
  }
 
  k=0;
  for(k=0; k<count; k++){
   printf("%d ", arr[k]);
  }
  printf("\n");
 }
 return 0;
}


result :

1 5 -5 12 2 12 14 16
1 -5 5 2 12 12 14 16
-5 1 2 5 12 12 14 16
-5 1 2 5 12 12 14 16
-5 1 2 5 12 12 14 16
-5 1 2 5 12 12 14 16
-5 1 2 5 12 12 14 16

Selection sort

 
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort.




Selection sort example


lets make it C language


#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
 int arr[] = {5, 1, 12, -5, 16, 2, 12, 14};
 int i=0;
 int j=0;
 int count=0;
 int min=0;
 int temp;

 count = sizeof(arr)/sizeof(arr[0]);


 for(i;i<count-1; i++){
  min =i;
  for(j=i+1;j<count; j++){
   if(arr[min]>arr[j]){
    min=j;
   }
  }
  temp = arr[i];
  arr[i] = arr[min];
  arr[min] = temp;
 }
 i=0;
 for(i=0; i<count; i++){
  printf("%d ", arr[i]);
 }
 return 0;
}

Monday, November 11, 2013

This is basic concept of Memoization in Javascript

<!DOCTYPE>
<html>
<body>
<script>
<!--  Basic use -->
function square(num){
return num*num;
}

console.log(square(10));

function squareMemoization(num){
var result= '';
if(!squareMemoization.cache[num]){
console.log("computing value..");
result = num * num;
squareMemoization.cache[num]= result;
}

return squareMemoization.cache[num];

}

squareMemoization.cache = {};

console.log(squareMemoization(10)); //First time when we call this function. It calculates the value &amp; cache it.
console.log(squareMemoization(10)); // Second time onwards it return the result from cache.
console.log(squareMemoization(20)); // square function will calculate again if its a new value.


var a = ["a", "b", "c"];


console.log('a.slice(1,2)');
console.log(a.slice(1,2));
console.log('a.slice(1)');
console.log(a.slice(1));
console.log(a);

console.log('Array.prototype.slice.call(a, 1)');
console.log(Array.prototype.slice.call(a, 1));

</script>
</body>
</html>

Wednesday, November 6, 2013

make javascript Speed Up (2)

Asynchronous Transfer Mode getting script

function loadScript


this is sample

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Hello</p>
<script>
 window.onload = loadAllScript;
 function loadScript(url, callback){
  var script = document.createElement("script");
  script.type = "text/javascript";
  console.log('loadScript');
  if(script.readState){ //IE
   script.onreadystatechange =function(){
    if(script.readState =="loaded" || script.readyState =="complete"){
     script.onreadstatechage= null;
     callback();
    }
   };
  }else{ //ELSE
   script.onload=function(){  
    callback();
   };
  }

  script.src=url;
  document.getElementsByTagName("head")[0].appendChild(script);

 }

 function loadAllScript(){
  console.log('loadAllScript');
  loadScript("http://code.jquery.com/jquery-1.10.1.min.js", function(){
 
   loadScript("http://s1.daumcdn.net/cfs.tistory/v/130502110925/blog/plugins/A_ShareEntryWithSNS/script/shareEntryWithSNS.js", function(){
    alert();
   });
  });
 }
</script>
</body>
</html>

make javascript Speed Up (1)

Do you Think which sample is better between two samples

1. <html>
    <head>
         <script type ="text/javascript" src="file1.js"></script>
         <script type ="text/javascript" src="file2.js"></script>
         <script type ="text/javascript" src="file3.js"></script>
   </head>
<body>
...
</body>
<html>



2. <html>
    <head>
   </head>
<body>
...
//end of body
         <script type ="text/javascript" src="file1.js"></script>
         <script type ="text/javascript" src="file2.js"></script>
         <script type ="text/javascript" src="file3.js"></script>
</body>
<html> 



First is well-known pattern when we use javascript, and also we learn like 1 sample

but until scripts are fully loaded we will see white page


Second sample is better than first
because after body is loaded javascript will run

Tuesday, November 5, 2013

how to compare string values using jstl fn

At first~!

in html we need to add Declare using jstl

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYE html>
<html>
....
</html>

this is really good i think

       
        <jsp:useBean id="extension" class="java.util.HashSet" scope="request">
           <%
         extension.add("ap");extension.add("avi");extension.add("bmp");extension.add("doc");
         extension.add("docx");extension.add("exe");extension.add("gif");extension.add("gul");
               extension.add("htm");extension.add("html");extension.add("hwp");extension.add("jpg");
               extension.add("log");extension.add("mht");extension.add("mp3");extension.add("pdf");
               extension.add("png");extension.add("ppt");extension.add("pptx");extension.add("tif");
               extension.add("tiff");extension.add("txt");extension.add("vcf");extension.add("wav");
               extension.add("xls");extension.add("xlsx");extension.add("xml");extension.add("zip");
           %>
       </jsp:useBean>
       <c:choose>
        <c:when test="${fn:contains(extension, attach.contentType)}">
         <img src="/neo/img/neo/theme/fileIcon/file_${attach.contentType}.gif"/>${attach.name} ${attach.size }bytes</a>
        </c:when>
        <c:otherwise>
         <img src="/neo/img/neo/theme/fileIcon/file_unknown.gif"/>${attach.name} ${attach.size }bytes</a>
        </c:otherwise>
       </c:choose>

Monday, November 4, 2013

make Callback function and check if it is undefine in javascript

<!DOCTYPE html>

<html>
<script>
window.onload = hello;

function hello(){
test(function aaa(){
alert('callback success');
});

test();
}

function test(callback){
if(typeof callback  != 'undefined'){

callback();
}
}
</script>

</html>

Sunday, November 3, 2013

basic javascript 3type alert

JavaScript supports three important types of dialog boxes. These dialog boxes can be used to raise and alert, or to get confirmation on any input or to have a kind of input from the users.

Here we will see each dialog box one by one:

Alert Dialog Box:


An alert dialog box is mostly used to give a warning message to the users. Like if one input field requires to enter some text but user does not enter that field then as a part of validation you can use alert box to give warning message as follows:

<head>
<script type="text/javascript">
<!--
   alert("Warning Message");
//-->
</script>
</head>

Nonetheless, an alert box can still be used for friendlier messages. Alert box gives only one button "OK" to select and proceed.



Confirmation Dialog Box:


A confirmation dialog box is mostly used to take user's consent on any option. It displays a dialog box with two buttons: OK and Cancel.

If the user clicks on OK button the window method confirm() will return true. If the user clicks on the Cancel button confirm() returns false. You can use confirmation dialog box as follows:

<head>
<script type="text/javascript">
<!--
   var retVal = confirm("Do you want to continue ?");
   if( retVal == true ){
      alert("User wants to continue!");
   return true;
   }else{
      alert("User does not want to continue!");
   return false;
   }
//-->
</script>
</head>


Prompt Dialog Box:


The prompt dialog box is very useful when you want to pop-up a text box to get user input. Thus it enable you to interact with the user. The user needs to fill in the field and then click OK.

This dialog box is displayed using a method called prompt() which takes two parameters (i) A label which you want to display in the text box (ii) A default string to display in the text box.

This dialog box with two buttons: OK and Cancel. If the user clicks on OK button the window method prompt() will return entered value from the text box. If the user clicks on the Cancel button the window method prompt() returns null.

You can use prompt dialog box as follows:

<head>
<script type="text/javascript">
<!--
   var retVal = prompt("Enter your name : ", "your name here");
   alert("You have entered : " +  retVal );
//-->
</script>
</head>

Maven Using filter~ It is Awesome

When you use maven Filter is very good for user
We can easily control enviroment of the project

Let's use Porifle

Ex> pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mkyong.common</groupId>
 <artifactId>SpringMVC</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>SpringMVC Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>
   <scope>test</scope>
  </dependency>
  <!-- Spring framework -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring</artifactId>
   <version>2.5.6</version>
  </dependency>
  <!-- Spring MVC framework -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>2.5.6</version>
  </dependency>
  <!-- JSTL -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.1.2</version>
  </dependency>
  <dependency>
   <groupId>taglibs</groupId>
   <artifactId>standard</artifactId>
   <version>1.1.2</version>
  </dependency>
 </dependencies>
 <profiles>
  <profile>
   <id>dev</id>
   <activation>
    <activeByDefault>true</activeByDefault>
    <property>
     <name>env</name>
     <value>dev</value>
    </property>
   </activation>
   <properties>
    <appserver.home>dev</appserver.home>
   </properties>
  </profile>
  <profile>
   <id>real</id>
   <activation>
    <property>
     <name>env</name>
     <value>real</value>
    </property>
   </activation>
   <properties>
    <appserver.home>real</appserver.home>
   </properties>
  </profile>
 </profiles>
 <build>
  <filters>
   <filter>src/main/filter/${appserver.home}/spring-views.properties</filter>
  </filters>

  <resources>
   <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
   </resource>
  </resources>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

project tree
src/main/filter/dev-spring-views.properties
                       /real-spring-views.properties
src/main/java
src/resources/spring-views.properties


command line
$ mvn clean package -P real


filters poperties will be injected into resources/spring-veiw.properties

if you want have sample

reply

Saturday, November 2, 2013

Spring escapeXMl using StringEscapeUtils

escape xss from hacker

we have to  change value using  StringEscapeUtils.escapeXml(str)

this is apache library
import org.apache.commons.lang.StringEscapeUtils;


//handling xml special character & in Java String
        String xmlWithSpecial = "Java & HTML"; //xml String with & as special characters
        System.out.println("Original unescaped XML String: " + xmlWithSpecial);
        System.out.println("Escaped XML String in Java: "
                            +  StringEscapeUtils.escapeXml(xmlWithSpecial));
     
        //handling xml special character > in String on Java
        xmlWithSpecial = "Java > HTML"; //xml String with & as special characters
        System.out.println("Original unescaped XML String: " + xmlWithSpecial);
        System.out.println("Escaped XML String : " + StringEscapeUtils.escapeXml(xmlWithSpecial));
     
     

        //handling xml and html special character < in String
        xmlWithSpecial = "Java < HTML"; //xml String with & as special characters
        System.out.println("Original unescaped XML String: " + xmlWithSpecial);
        System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
     
     

        //handling html and xml special character " in Java
        xmlWithSpecial = "Java \" HTML"; //xml String with & as special characters
        System.out.println("Original unescaped XML String: " + xmlWithSpecial);
        System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
     
        //handling xml special character ' in String from Java
        xmlWithSpecial = "Java ' HTML"; //xml String with & as special characters
        System.out.println("Original unescaped XML String: " + xmlWithSpecial);
        System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));