Tuesday, August 9, 2011
How to find median of 5 numbers in 7 comparisons? No other constraints.
Algorithm: findMedian(a, b, c, d, e)
step 1: sort(a, b, c, d) {sort the first four numbers using the sort module in 5 comparisons}
step 2:
if e < b then median <- b else if e > c then median <- c
else median <- e
return median
Total comparisons to sort the first four numbers = 5
Total comparisons to locate median = 2
Therefore total comparisons to find median of four numbers = 5 + 2 = 7
step 1: sort(a, b, c, d) {sort the first four numbers using the sort module in 5 comparisons}
step 2:
if e < b then median <- b else if e > c then median <- c
else median <- e
return median
Total comparisons to sort the first four numbers = 5
Total comparisons to locate median = 2
Therefore total comparisons to find median of four numbers = 5 + 2 = 7
How to sort four numbers in 5 comparisons? Efficiency in terms of space, time and number of assignments is not considered.
Algorithm: sort(a, b, c, d)
if a > b then swap(a, b)
if c > d then swap(c, d)
if a > c then swap(a, c)
if b > d then swap(b, d)
if b > c then swap(b, c)
subroutine: swap(x, y)
temp <- x
x <- y
y <- temp
The resulting sequence a, b, c, d will be sorted in ascending order.
if a > b then swap(a, b)
if c > d then swap(c, d)
if a > c then swap(a, c)
if b > d then swap(b, d)
if b > c then swap(b, c)
subroutine: swap(x, y)
temp <- x
x <- y
y <- temp
The resulting sequence a, b, c, d will be sorted in ascending order.
Labels:
4 numbers,
sort,
sort 4 numbers in 5 comparisons
Location:
Fairfield, IA, USA
Sunday, October 24, 2010
Fix Windows Live Messenger 2011 Sign In Error 80040154
If you are experiencing error signing in to Windows Live Messenger 2011 (installed from Microsoft update or mannual installation), following steps can help resolve the issue.
64-bit Operating System
Fix Windows Live Messenger 2011 Sign In Error 80040154
64-bit Operating System
- go to C:\Program Files (x86)\Windows Live\Shared\
- copy the 2 files
- wldcore.dll
- wldlog.dll
- paste the 2 files to C:\Program Files (x86)\Windows Live\Contacts\
- go to C:\Program Files\Windows Live\Shared\
- copy the 2 files
- wldcore.dll
- wldlog.dll
- paste the 2 files to C:\Program Files\Windows Live\Contacts\
Fix Windows Live Messenger 2011 Sign In Error 80040154
Labels:
80040154,
Error,
Sign In,
Windows Live Messenger 2011
Thursday, June 11, 2009
How to deploy an application war file in the ROOT context in JBoss
Steps
--------------
- Rename the directories named "ROOT.war" placed inside the following locations (if the directory is there) to something else
- "JBOSS_HOME\server\default\deploy\jbossweb-tomcat55.sar\"
- "JBOSS_HOME\server\all\deploy\jbossweb-tomcat55.sar\"
- AND/OR any other instance "JBOSS_HOME\server\\deploy\jbossweb-tomcat55.sar\"
- Create an xml file named jboss-web.xml with the following contents
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaxFM9cdBQVpP-K7NoIxa0YTuR9wPM02_0cgDAeV7TKst103rNk45I9t9Ko7puqsNcbOe2-z5_AswExdHSGds92QCgwEDm5cFE01MQVLaFUtTSk2mVRiuNoM03a1gGjWlDcTZjWjDYj0vb/s320/root.bmp)
- Put the jboss-web.xml file in the WEB-INF directory of your war file and deploy the war file.
--------------
- Rename the directories named "ROOT.war" placed inside the following locations (if the directory is there) to something else
- "JBOSS_HOME\server\default\deploy\jbossweb-tomcat55.sar\"
- "JBOSS_HOME\server\all\deploy\jbossweb-tomcat55.sar\"
- AND/OR any other instance "JBOSS_HOME\server\\deploy\jbossweb-tomcat55.sar\"
- Create an xml file named jboss-web.xml with the following contents
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaxFM9cdBQVpP-K7NoIxa0YTuR9wPM02_0cgDAeV7TKst103rNk45I9t9Ko7puqsNcbOe2-z5_AswExdHSGds92QCgwEDm5cFE01MQVLaFUtTSk2mVRiuNoM03a1gGjWlDcTZjWjDYj0vb/s320/root.bmp)
- Put the jboss-web.xml file in the WEB-INF directory of your war file and deploy the war file.
Is Java "Pass By Reference" or "Pass by Value"?
Java is always "Pass By Value".
Live with the statement that
In Java, Object references are passed by values and primitives are passed by values.
Consider the following examples:
1. Primitives:
public static void main(String args[]){
int a = 5;
int b =10;
System.out.println("Before swap, a = " + a + " , b = " + b);
swap(a, b)
System.out.println("Afterswap, a = " + a + " , b = " + b);
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
Result:
You will see zero effect of the swap method, because the parameters are passed by values and the during swapping inside the swap method only the copies were modified.
2. Object References:
Class Person{
private String address;
public String getAddress(){
return this.address;
}
public void setAddress(String address){
this.address = address;
}
}
public static void main(String args[]){
Person a = new Person();
Person b = new Person();
a.setAddress("Address A");
b.setAddress("Address B");
swap(a, b);
}
public void swap(Person a, Person b){
Person temp = a;
a = b;
b = temp;
}
You will again see Person objects unaffected by the swap operation.
Now, if the swap function is modified as shown below
public void swap(Person a, Person b){
String tempAddress = a.getAddress();
a.setAddress(b.getAddress());
b.setAddress(tempAddress);
}
In this case, the original objects will be modified.
Live with the statement that
In Java, Object references are passed by values and primitives are passed by values.
Consider the following examples:
1. Primitives:
public static void main(String args[]){
int a = 5;
int b =10;
System.out.println("Before swap, a = " + a + " , b = " + b);
swap(a, b)
System.out.println("Afterswap, a = " + a + " , b = " + b);
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
Result:
You will see zero effect of the swap method, because the parameters are passed by values and the during swapping inside the swap method only the copies were modified.
2. Object References:
Class Person{
private String address;
public String getAddress(){
return this.address;
}
public void setAddress(String address){
this.address = address;
}
}
public static void main(String args[]){
Person a = new Person();
Person b = new Person();
a.setAddress("Address A");
b.setAddress("Address B");
swap(a, b);
}
public void swap(Person a, Person b){
Person temp = a;
a = b;
b = temp;
}
You will again see Person objects unaffected by the swap operation.
Now, if the swap function is modified as shown below
public void swap(Person a, Person b){
String tempAddress = a.getAddress();
a.setAddress(b.getAddress());
b.setAddress(tempAddress);
}
In this case, the original objects will be modified.
Tuesday, June 9, 2009
Quotations
"Learn from the mistakes of others. You can't live long enough to make them all yourself." - Chanakya
"If someone betrays you once, it's his fault. If he betrays you twice, it's your fault."
"If someone betrays you once, it's his fault. If he betrays you twice, it's your fault."
Subscribe to:
Posts (Atom)