Monday, February 16, 2015

NOTICE : Blog moved to Wordpress

NOTICE

This is to inform whosoever stumbles upon this blog, that I do not blog here anymore. For my most recent and better posts please follow me on my Wordpress blog.

http://mdnaseemashraf.wordpress.com/


Though, the source codes discussed and shared here will be still available via my public Google Drive which has source codes and other shared files.


https://drive.google.com/folderview?id=0B4e1TZA7mwrNZU9vRVBVakRxMjA&usp=sharing


- MD Naseem Ashraf

Thursday, December 5, 2013

Insertion Sort in Different Flavors

NOTE: Blog moved to Wordpress. Click here for latest posts and more cool stuffs.


Been revving up algorithms for my job hunt. Thought, I should share some. Here's Insertion Sort, in Ruby, Java and C.
It has a Worst and Average Case Scenario Complexity of Theta(n^2) (or Theta n-squared). It is efficient for smaller data sets while larger data sets would require a lot of time for sorting.
Oh! I've also been dabbling in the art of Ruby Programming.
I find the language a lot more neat and simpler than my previously known languages - C and Java. The best thing is that with multiple syntax possibility, I can quickly code in free flow without the need to frequently check and recheck whetehr I'm sticking to the syntax rules to express my thoughts.
This is one of the features of ruby to have multiple syntax rules for the same task, incorporated from C, Python, Small-Talk, Java and more. This makes it easy to learn and apply.
Without further delay, here's the algorithm;

INSERTION SORT (PSEUDO-CODE)

A = [1,5,3,7,6,2,8,9,10,4]
print "Initial Array Order " + A
for j = 1 to A.length
{
i = j-1
key = A[j]
while i>=0 && A[i]>key
{
A[i+1] = A[i]
i = i-1
}
A[i+1] = key
}
print "Sorted Array Order "+ A
OUTPUT>> [1,2,3,4,5,6,7,8,9,10]
Note: The above algorithm assumes a Zero indexing for the Array. For One indexed Arrays, alter the for loop to -
"for j = 2 to A.length"
and alter the while loop to -
"while i>0 && A[i]>key"
Now, Insertion Sort in Ruby;

INSERTION SORT (RUBY)

A = [1,5,3,7,6,2,8,9,10,4]
p "Initial Array Order"
p A
for j in 1...A.length
i = j-1;
key = A[j];
while i>=0 && A[i]>key
A[i+1] = A[i];
i = i-1;
end
A[i+1] = key;
end
p "Sorted Array Order"
p A

Here's the screen shot of the running Ruby program:

ruby Insertion Sort

InsertionSort (Java)

public class InsertionSorting
{
public static void main( String[] args )
{
int A[] = { 10, 9, 5, 7, 3, 1 };
System.out.printf("\nInitial Array:\n");
for( int k=0;  k<A.length;  k++)
{
System.out.printf( "%d, ", A[k]);
}
for( int j=1;  j<A.length;  j++)
{
int i = j-1;
int key = A[j];
while((i>=0)&&(A[i]>key))
{
A[i+1] = A[i];
i=i-1;
}
A[i+1] = key;
}
System.out.println("\nFinal Array:");
for( int k=0;  k<A.length;  k++)
{
System.out.printf("%d, ",A[k]);
}
}
}

Here's the screen shot of the running Java program:

InsertionSort Java Capture
Java program run in Netbeans IDE.

INSERTION SORT (C)

//Insertion Sort
void main()
{
int A[6] = { 10, 5, 7, 3, 4, 1 };
printf("\nInitial Array\n");
for ( int l=0;  l<6;  l++)
{
printf("%d\n", A[l]);
}
int j =0;
for( j=1;  j<6;  j++)
{
int i = j-1;
int key = A[j];
while((i>=0)&&(A[i]>key))
{
A[i+1] = A[i];
i=i-1;
}
A[i+1] = key;
}
printf("\nFinal Array\n");
for (int k=0; k<6; k++)
{
printf("%d\n", A[k]);
}
getch();
}

Here's the screen shot of the running C program:

tcc Insertion Sort
[C Program compiled with TCC.]
Hey! It's a nice idea to code the same stuff in C, Java and Ruby. Next time onward I'll try to post programs in all three languages. Not entire applications, though, it gets tedious when the program runs long.
Next hopefully should be Data Structures Stack and Queue, or Merge Sort. Lets see. Till then, adios!
PS: Attached Ruby, Java and C files. [LINK]

Bibliophile: 1984 By George Orwell (A Glimpse)

NOTE: Blog moved to Wordpress. Click here for latest posts and more cool stuffs.


I have heard a lot of praise of "1984", by George Orwell. Let me first quote directly from the book's summary on its back cover:
"Winston Smith is a low-rung member of the Party, the ruling government of Oceaina. He works in the Ministry of Truth, the Party's propaganda arm, where he isin charge of revising history. He is but a small brick in the pyramid that is the Party, at the head of which stands Big Brother. Big Brother the infallible. Big Brother the all-powerful.
In a totalitarian society, where individuality is suppressed and freedom of thought has its antithesis in the Thought Police, Winston finds respite in the company of Julia. Originality of thought awakens, love blossoms and hope is rekindled. But what they don't know is that Big Brother is always watching."
And....I ADORE this book!! Well, 'Volume One' or 'Part One' (Whatever you call the partitions.) of the book. Instantly a lot of ideas, thoughts and memories begin swirling in the reader's mind, when you start reading the book. This book is primarily evocative and thought provoking.
A lot of (Yes, a Hell, lot of!) other writers, stories and books have copied (another word is stolen, or adapted if you're being polite) the ideas from this masterpiece. Truly, original and most influential, I agree.
I've started with 'Part Two'. The first fifteen or so pages of 'Volume Two', is a little repetitive. More over, I believe these could have been shortened. I fear the same themes and ideas would be reiterated over the entire 'Volume Two' (But, I cannot say it with certainty, unless I've read it.). I may need to skim and skip a few more pages to get back into the juicer parts of the (main) plot.
Still, I'd recommend anyone to read this book even for the ideas and imagination that rings so true in internet/espionage days in 'Volume One'. Also, the book is aimed primarily as an anti-communist propaganda but, works well for any authority figure or government.
Full review after I get through the remaining two volumes (Yes, the entire text is in Three Volumes). Catch you later.

Books Discussed:

1. 1984 By George Orwell.

Bibliophile: Adios, Invisible Man.

NOTE: Blog moved to Wordpress. Click here for latest posts and more cool stuffs.


Reading of "The Invisible Man" was finally completed! Things could be so much better in today's world of information overload and instant gratification, if one were locked up in a room with nothing but a book, that man would read, reread and even memorize that book.
Something similar occurred with me. I was on a two and a half hour flight, yesterday, from Delhi to Bangalore, to resume my job hunt here. I was allotted a window seat and, surprise, I didn't have any travelling companion on my side. No sir not even one! I was free to move up the side arms and sprawl on that three seat couch. Cheers! I still had the distraction of my laptop but, as I've not got it's battery replaced, this repaired junk would barely run a few minutes. Hence, no more distracting laptop.
All options snatched away from my hands. All distractions muted in my mind. I was finally free to do some quality work - Crack open a book and read it.

The Invisible Man: My review -

The Invisible Man was a good book. Though parts of it were prolix and a bit dated as are most classics. Some pages are full of unneeded imagery. Now, onto the finer points of the story. The story shows in small glimpses the psychological effects of Invisibility on a man. His effort to handle this new found advantage (power) and finally, his downfall.
A lot of questions comes to my mind. "Where is the blame?" Is the society or these mundane and average
people to be blamed in the crime of not understanding a man's achievement or their mistreatment of him just
cause he was invisible? The first part of the blame I believe is on the society. Unwilling to agree and sympathize with the protagonist.
This mistreatment (discrimination?) of the invisible man by the society may have been the prime cause of
his insanity, I'd argue. But, then, "Are we inherently evil (or good) only a moment away to show our true nature and color?"
A quote comes to mind about blame:
"The superior man resolves to walk alone, and is caught in the rain. He becomes bespattered and people
murmur against him. Where is the blame in this?" - I Ching
And as is custom, here is a quote from The Invisible Man -
"Hitherto I have gone on vague lines. We have to consider all that invisibility means; all that it does not mean." - The Invisible Man.
Now, I'll be off to reading 1984 and job hunting. Wish me luck.

Books Discussed:

1. The Invisible Man by H.G Wells (Science Fiction, Classic, Tragedy, Thriller)

Books Mentioned:

1. 1984 by George Orwell (Dystopian Future, Society, Science-Fiction, Psychology)

Bibliophile: Lord of the Flies + Hardboiled Wonderland and The End of The World

NOTE: Blog moved to Wordpress. Click here for latest posts and more cool stuffs.


Today, I decided to post about two previously read books, as my progress with The Invisible Man is quite slow. One eBook and the other traditional trade paperback.
Both these books are thought provoking and deep. They have great deal to say on the various nuances of life, existence, philosophy and the human condition, including our own humanity. Even though my previously chosen words may add a slight hesitation in the modern microwave minded easy readers, both these books are very good as story and easily accessible.

Lord of The Flies

An engaging story about a group of stranded children on an uninhabited utopian island. The story follows the adventures and events occurring with survival and daily activities of these plane crash survivors. Slowly progressing into more complicated themes and mature allegories. A good read for both young and adult readers. Lord of The Flies, packs in a multitude of characters and their personalities, making the book a very rich reading experience. The past and background of these characters is left unknown and very vague. The starting situation is much like a hypothesis for an experiment on human nature, psychology, civilization and society.
The allegory and in depth meaning may not leap directly for the reader. But, it becomes a great story, once we start assimilating the nature of the characters and connect them with real life people. That may not be a point of concern for the first time reader. But, this book deserves a second re-reading or at least some time to think over it. (The Wikipedia Page on this book may help you as a cheat sheet but, only after you've first read the story in its entirety.)

Hard-Boiled Wonderland and The End of The World

by Haruki Murakami (Translated from Japanese)
Read this as an e-book, now, I crave to buy it as a paperback. This book is primarily Science-Fiction, but, laden with philosophy. This book may not be for everybody. The ordinary reader may find the book verbose; And a tough book to digest especially if the reader has aversion to thinking, wondering and questioning, his or her own-self and mindset. Primarily aimed at mature readers and specifically those stuck in this modern life very similar to the dystopian life of the story's protagonist.
The book narrates two parallel stories (Hard-Boiled Wonderland and The End of The World) with indirect connection as alternating chapters. Their is a marked contrast between the setting, theme and other plot points of the two stories. This is purposefully designed to reflect our own internal conflict, with our mixed feelings and somewhat confused (not actually confused. The thoughts of the protagonist in the two stories is crisp but, they deal with different aspects of our humanity) thoughts.
Some, readers (who are in my opinion addicted and acclimated to most of the ordinary low quality reading materials) find it difficult to follow or grasp the stories; especially the alternating parallel narratives. Some of the critiques go on to complain that the two stories could've been separated into two volumes one after the another, avoiding all the alternating chapters format. They, better keep mum, for every book and story is entitled to be told in it's own unique format, sequence and style. That is the freedom a writer and a story teller has, which builds the true nature of the story. It is not a technical book, to stringently follow a format or sequence order! (Try, House of Leaves for that matter and you'd find it convoluted, eerie and hauntingly non-sequential, thereby adding the horror theme and flavor to a far greater degree than ordinary stories.)
Finally, as a work of fiction the story is pretty short and even depressing for the reader. But, as a food for thought, the questions, observations and thoughts of the protagonist is very profound. This should be a warning to those seeking an easy read or an entertaining story. For a story, pick any other book. For an experience, choose this one. It is primarily a psychological thriller, once the reader starts relating to the questions, thoughts and ideas of the protagonist or his lifestyle. It oozes of existentialism, soul-searching and self-realization.
I personally, read it in PDF format on Foxit Reader, which facilitated me with highlighting, underlining and note making tools. I have underlined, highlighted, commented and argued my own thoughts and feelings as I read the story and marked it in the book. So, paperback owners would be better off with their pencils about them when they're reading this book, if they want to get to the depth of this book.
Happy Reading!!

Books Discussed:

  1. Lord of The Flies by William Golding (Thriller, Adventure, Psychological Thriller, Philosophical, Tragedy?).
  2. Hard-Boiled Wonderland and The End of The World by Haruki Murakami. (Psychological Thriller, Philosophical, Translated, Tragedy?)

Books Mentioned:

  1. House of Leaves by Mark Danielewski. (Horror, Psychological Thriller, Mystery, Supernatural)
  2. The Invisible Man by H.G. Wells. (Science Fiction, Classic)

Reinstallation Complete! Let's code on!!

My software, data etc re-installation on my laptop is now sufficiently complete.
STEP 1: Install the Omnipresent Java (i.e JDK) and two IDEs Eclipse and NetBeans.
I ran into issues with the JDK, Eclipse Galileo and PATH Environment variable here's another post on what I had to struggle with.
Step 2: Add some new age uber powerful simplistic language - Ruby On Rails (Primarily Ruby. I'm still learning it.)
I installed Ruby On Rails Bundle from this Rails Installer, Link. This bundle contains a Ruby Installer, Rails Installer and a Ruby Gems Package Manager (Which handles Ruby Libraries), as well as Git. I've been tinkering with Ruby these last few months but, have not worked something substantial - I'm still in the beginner learning phase.
Step 3: And, then there was the wise old owl, C.
Installed a no questions asked hassle free C compiler - TCC from here. Tiny C Compiler is by far the smallest compiler and its compilations/builds are also comparatively small. [Reference Link].
Step 4: Add some cool sounding and fancy tools.
Installed a set of Editors to make me gloat in pride...Vim and Notepad++. But, still uses Notepad (Force of habit).
Step 5: Install the Essentials of Human Computer Interaction worthy applications.
Then I went on to install the little subtle essentials: Skype, Firefox and VLC Player.
Step 6: Finally, the Bells, Whistles and Eye Candy: [Details linked.]
  • CCleaner
  • Malwarebytes Anti-Malware Software
  • Pandora Recovery
  • Free Alarm Clock
  • Stardock's Logon Studio
  • Stardock's Fences
Step 7: Rest in the after glow of achievement and plan your next code projects........doing what is meant to be done.

Issues with the JDK, Eclipse Galileo and PATH Environment variable

Problems with the JDK and Eclipse:
"JDK Not found...." & "JNI Error..."
I had quite a lot of trouble fixing my Java JDK and Eclipse Galileo IDE. Thing was that I was trying to reuse my old installation of Eclipse (which being portable does not require a setup/installation) with the old JDK 1.6.0 Setup, I had saved. My old eclipse started throwing up strange problems when I tried working on new projects in my old workspace, even though the previous projects and files were working fine with the IDE.
I decided on updating my Java which gave me a new JRE 1.7 which existed with JDK 1.6.0 (Creating more path related confusions). This created more hassles with my system when I tried installing a new version of Eclipse IDE Kepler. The IDE started throwing "JDK not found" errors and my command prompt (Yes, I'm still stuck with Windows 7. I will build a dual boot with Linux Mint, as soon as I get the download finished.) also did not respond to "java" or "java -version" commands. So, I figured out that my JDK has been screwed.
Ineffective Solution:Capture
I tried all the fixes available from altering the "eclipse.ini" configuration file shipped with eclipse to point to my JDK using "-vm %JAVA_HOME%"\bin\" and other similar combinations. Then the system started throwing JNI Error of a missing .dll file from the jre in the client folder. One way was to link to its path the location in the configuration file (Messy! Don't do that! Well, unless you know what you're doing).
Solution:
Uninstalled and Reinstalled Java (JDK 1.7). [My suggestion; You should try CCleaner after uninstalls to clean and fix Windows Registry.] Set the PATH environment variable from:Capture 2
Advanced System Settings (From My Computer Properties after Right Clicking) > System Properties (Dialog) > Advanced (Tab) > Environment Variables (Button) > System Variables (Window) > New/Edit (Button below the window depending whether "path" or "PATH" variable exists or not) > Adding the path/address to "%JAVA_HOME%\bin". Then you may need to restart the system.
Capture3  Capture4Capture5
I downloaded the download available from Oracle with the latest Netbeans IDE bundled together. [Link] (For the JDK only try this Link.)
Did You Know: There are different JDKs available to use. They have the same standard libraries but, they also have special libraries bundled with them to be compatible with the other vendor services and tools. The two giants IBM and Oracle, there are Open Source versions too, like the one from GNU and OpenJDK; Courtesy of Sun Microsystems, which had released the Source Code as open source sometime, back in 2006.
Conclusion: My JDK, Eclipse Kepler and NetBeans IDE works like magic.
Other known Reasons for similar JDK and Eclipse Problems are incompatible softwares, like using 64 Bit JDK or IDE on a 32 Bit System. Here is a small note on it:
Working: 32 Bit OS + 32 Bit JDK + 32 Bit Eclipse IDE
Working: 64 Bit OS + 32 Bit JDK + 32 Bit Eclipse IDE
Working: 64 Bit OS + 64 Bit JDK + 64 Bit Eclipse IDE