## Posts

Showing posts from November, 2013

### Bubble Sort

SortAverageBestWorstSpaceStabilityRemarksBubble sortO(n^2)O(n^2)O(n^2)ConstantStableAlways use a modified bubble sortModified Bubble sortO(n^2)O(n)O(n^2)ConstantStableStops after reaching a sorted arraySelection SortO(n^2)O(n^2)O(n^2)ConstantStableEven a perfectly sorted input requires scanning the entire arrayInsertion SortO(n^2)O(n)O(n^2)ConstantStableIn the best case (already sorted), every insert requires constant timeHeap SortO(n*log(n))O(n*log(n))O(n*log(n))ConstantInstableBy using input array as storage for the heap, it is possible to achieve constant spaceMerge SortO(n*log(n))O(n*log(n))O(n*log(n))DependsStableOn arrays, merge sort requires O(n) space; on linked lists, merge sort requires constant spaceQuicksortO(n*log(n))O(n*log(n))O(n^2)ConstantStableRandomly 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>…

### Selection sort

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

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;
}

this would be a good example

### 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));

</…

### make javascript Speed Up (2)

Asynchronous Transfer Mode getting script

this is sample

<!DOCTYPE html>
<html>
<body>
<p>Hello</p>
<script>
var script = document.createElement("script");
script.type = "text/javascript";
callback();
}
};
}else{ //ELSE
callback();
};
}

script.src=url;

}

### make javascript Speed Up (1)

Do you Think which sample is better between two samples

1. <html>
<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>
...
</body>
<html>

2. <html>
<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 sa…

### 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">
<%
…

### make Callback function and check if it is undefine in javascript

<!DOCTYPE html>

<html>
<script>

function hello(){
test(function aaa(){
});

test();
}

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

callback();
}
}
</script>

</html>

### 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:

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:

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 wi…

### 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.0http://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.springframewo…

### 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
xmlWith…