Wednesday, 17 October 2012

Merge Sort

If you have an array of 8 elements, merge sort works by grouping these 8 into 4 sorted sets of 2. The sort is done through sibling pairs.

With these 4 pairs, you pair these with their siblings and take the first elements of each. Compare element 1 in both and place the lowest value in a third new array. You then compare element 1 with element 2 and place the lowest in the third array. This will go on until you are left with single pair merge sorting into a single final array.

Insertion Sort

Insertion sort works by maintaining a fully sorted list and inserting unsorted data one at a time then immediately sorting it.

So to begin with you have a single element, and that is considered sorted.
Then you add an item to the right of it. You compare these with each other and you might have to swap them. Then you have a sorted list of 2.
Then you add another item to the right of your list. You compare this with the one before it, if they're already sorted then you're done comparing for that iteration, otherwise swap them and move to the left. This goes on till you have no more data to insert.

Bubble Sort

Bubble sort is a simple sorting algorithm that compares the first two elements in the array and keeps hold of the largest, for each pass the largest will 'bubble' to the top. Each pass will require n-i comparisons.

This algorithm is considered O(N^2)

Sunday, 2 September 2012

Clojure multimethods

Defmulti

Defmulti defines a multimethod call, the methods don't need to be defined yet. It can be as simple as the following

(defmulti salutation :gender)

With this defined now, we can add conditions in the form of multimethods

Defmethod

To add a condition to this we can use the following

(defmethod salutation :f [_] "Madam")
(defmethod salutation :m [_] "Sir")

This allows us to make calls to the function and have it transparently dispatch to the correct function based on the gender keyword in the map.

(salutation {:name "Kirsty" :gender :f})
"Madam"
(salutation {:name "Josh" :gender :m})
"Sir"

Web Design

Web Dev

I've annoying been lumbered with some front-end web stuff lately. Generally, I protest vehemently at any suggestion of me doing front facing work but this task has got me a bit interested. It probably won't last.

Pseudo

There are two kinds of pseudo in CSS: pseudo-classes and pseudo-elements

Pseudo-classes

  • :link
  • :visited
  • :hover
  • :focus
  • :active
  • :first-child
  • :lang()

Pseudo-elements

  • ::first-line
  • ::first-letter
  • ::before
  • ::after

Tuesday, 21 August 2012

Classpath error snippet

File Not Found (Classpath Resource)

If this is encountered, either try modifying the classpath or prepending the file location with 'file://'

Spring Jersey Contrib

Adding Jersey to a spring webapp

Since jersey has its own servlet that would normally take the REST requests, it's difficult to wire this in with Spring beans. That's where the spring contrib library comes in. Instead of having the WEB-INF/web.xml use the servlet com.sun.jersey.spi.container.servlet.ServletContainer, you can use the below com.sun.jersey.spi.spring.container.servlet.SpringServlet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:application-context.xml</param-value>
 </context-param>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <servlet>
  <servlet-name>service</servlet-name>
  <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
  <init-param>
   <param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
   <param-value>true</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>service</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
</web-app>

Maven dependency

Adding this using maven would consist of the following.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!-- Jersey + Spring -->
<dependency>
 <groupId>com.sun.jersey.contribs</groupId>
 <artifactId>jersey-spring</artifactId>
 <version>${jersey.version}</version>
 <exclusions>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
  </exclusion>
 </exclusions>
</dependency>