Skip to content

Instantly share code, notes, and snippets.

View mhshams's full-sized avatar

Mohammad Sarbandi mhshams

  • Berlin, Germany
View GitHub Profile
@mhshams
mhshams / Address
Created December 12, 2011 09:24
Address (Groovy Bean)
class Address {
String number
String firstLine
String secondLine
String postCode
String City
String State
}
@mhshams
mhshams / Person
Created December 12, 2011 09:24
Person (Groovy Bean)
class Person {
String firstName
String lastName
Address address
}
@mhshams
mhshams / Main
Created December 12, 2011 09:26
Only talk to your immediate friends.
println person.getAddress().getFirstLine()
println person.getAddress().getSecondLine()
@mhshams
mhshams / Person
Created December 12, 2011 09:40
Person (Second Implementation)
class Person {
String firstName
String lastName
private Address address
String getAddressFirstLine() {
address.firstLine
}
String getAddressSecondLine() {
@mhshams
mhshams / Main
Created December 12, 2011 09:41
Client (second implementation)
println person.getAddressFirstLine()
println person.getAddressSecondLine()
@mhshams
mhshams / File
Created December 14, 2011 07:29
private static File generateFile(String prefix, String suffix, File dir)
throws IOException
{
long n = LazyInitialization.random.nextLong();
if (n == Long.MIN_VALUE) {
n = 0; // corner case
} else {
n = Math.abs(n);
}
return new File(dir, prefix + Long.toString(n) + suffix);
def mergeSort(list) {
def size = list.size()
if (size < 2) {
return list
} else {
def m = (int)(size / 2)
def left = list[0..<m]
def right = list[m..<size]
def heapSort(list) {
def size = list.size()
if (size < 2) {
return list
}
/*We need to create a valid binary heap first*/
heapify(list)
def quickSort(list) {
/*Make sure there are at least 2 elements in the list*/
if (list == null || list.size() < 2) {
return list
}
/*break the list to three different groups
group 1: with elements less than pivot
group 2: with elements equal to pivot
class PriorityQueue {
/*a list to contains the queue elments in a binary heap format*/
def list = []
/*Add new item to the queue*/
def add(item) {
list.add(item)
/*After adding new item, we need to re-validate the heap*/
siftUp(list, 0, list.size() - 1)