Monthly Archives: August 2015

Sorting Algorithm : Insertion Sort

From this two video I tried to re learn and implement the algorithm with pseudo code.

Pseudocode:

Implemented Code in C:

Output:
2015-08-07 13_30_30-C__Users_User_Desktop_insertionsort.exe

Here ary[] means array 🙂

Simulation:(Wikipedia)

Harvard CS50 course:
http://cs50.tv/2016/fall/

GeeksForGeeks Algo Implement(Not Worked Yet):

http://quiz.geeksforgeeks.org/insertion-sort/
Practice Problem:
http://www.practice.geeksforgeeks.org/problem-page.php?pid=700148
https://www.hackerearth.com/practice/algorithms/sorting/insertion-sort/practice-problems/
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_A

CLRS Theory:

http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/insertionSort.htm

Simple Implementation of Insertion Sorting(Idea Sanfoundry):

It is helpful: http://www.sanfoundry.com/cpp-program-sort-array/

Time complexity of Insertion Sort:

  • Worst case complexity : O(n^2)
  • Best case complexity :    O(n)
  • Average case complextiy:  O(n^2)Reference:
    http://bigocheatsheet.com/

How to calculate complexity there is a example available at the upper video “Lecture 7”

and there are some links
http://stackoverflow.com/questions/19827193/time-complexity-of-insertion-sort

Here a good presentation about this sorting

http://www.cs.mcgill.ca/~cs203/lectures/lecture7/sld006.htm

Reference: http://www.cquestions.com/2009/09/insertion-sort-algorithm.html

For descending order I took help from:
http://cboard.cprogramming.com/c-programming/73433-sorting-descending-order.html
Here they change the comparison value for descending at while loop condition

Another good website:

Time complexity of insertion sort when there are O(n) inversions?


For some theory revise this video is good:

URI Online Judge Solution | 1041 Coordinates of a Point

Here is the logic,  1041

If x!=0 and y==0 then it will be in the y asix and
If y!=0 and x==0 then it will be in the x axis

that means you have to understand if what the value is equivalent to 0 then it will be in that axis

And one thing = means assignment operator if x=y then if x=5 then y=5

and == means comparison operator if x==y then if x==4 and y==5 then x!=y so it will be false

and if x=5 and y=5 then x==y and it is true

Reference:
http://www.allinterview.com/showanswers/17960/what-is-the-difference-between-and-in-c.html
http://www.mathsisfun.com/data/cartesian-coordinates.html

code:

and like this can do

 

 

Here in this code you have to think about the flow which normally starts from the 0 and the compiler starts punctuating the code from Zero 0.

logic flow can be start from 0 then can be happen through greater than or equal

It’s better to do in C/C++ main format

Bash For Loops

Syntax:

example:

we can check at the end that loop is not working by this

 

 

Another examples can be c like styles:

Bash IF Else with for loop break and continue  have to practice later time 🙂

 

Here is the video all for loop explained beautifully

References:

http://www.cyberciti.biz/faq/bash-for-loop/

Bash Programming : Say user hello !

Take a name from user and say hello user! 😀

 

Bash Programming : Check typed name is a valid $USER or not

I tried it hardly….Here you have to maintain the syntax

if[expression]
then
echo expr ""Hello Name
fi

and this conditional

if [ “foo” = “foo” ]; then
echo expression evaluated as true
fi

or the same things can be written as

if[ “foo” = “foo” ]
then
echo expr "Hello !"
fi

if..then..else in bash

if [ “zaki” = “live” ]
then
echo expr "True !"
else echo
expr “False!”

conditional with variables
t1="zaki"
t2="live"
if [ "$t1" = "t2" ]
then
echo
expr “True”
else
echo
expr “False”`
fi #you have to end with this if you started with if

Reference: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html#ss6.1
http://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php

 

Bash Programming : Multiplication

Here I tried this on ubuntu

To create the file

code type in the terminal:

save the file and close editing mode:

change the permission of this file:

to show the output

 

Bash Programming : Addition

Here I tried this on ubuntu

To create the file

code type in the terminal:

don’t give space after or before equal sign

save the file and close editing mode:

change the permission of this file:

to show the output

Shortcut method for addition or any other:
create a file with cat then type code from the below in one line

here 1 2 and 3 is variable is taking input from user and also giving teh output so we sue $ before them….it is the shortcut method

then just type

and you will get the addition of this variables