Thursday, March 31, 2016

bubble sort - o(n*n)


def bubble_sort(arr):
for i in range(0, len(arr)):
for j in range(i, len(arr)):
if arr[i] > arr[j]:
arr[j], arr[i] = arr[i], arr[j]

arr= [8, 3, 0, 8, 9, 3, 0, 5]
print arr

selection sort - O(n*n)

def selection_sort(arr):
    for i in range(0,len(arr)):
        min_index = i
        ## find minimum element in remaining
        ## array
        for j in range(i+1, len(arr)):
            if arr[j] < arr[min_index]:
                min_index = j
        ## swap the minimum element with arr[i]
        arr[i], arr[min_index] = arr[min_index], arr[i]

arr = [3, 0, 9, 4, 2, 9, 1, 0]
print arr

Wednesday, March 30, 2016

Binary Search O(log N)


### A recursive binary search function
def binarySort(arr,l,r,ele):
    m = (l+r)/2
    if arr[m] == ele:
        return m;
    elif arr[m] < ele:
        return binarySort (arr, m + 1, r, ele)
        return binarySort (arr, l, m - 1, ele)
    return -1
##A iterative binary search function
def binarySort(arr, l, r, ele):
    while l < r:
        m = (l+r)/2;
        if arr[m] == ele:
            return m
        elif arr[m] < ele:
            l = m + 1
            r = m - 1
    return -1
arr = [1,2,3,4,5,6,7]
l= 0
r= len(arr)-1
ele= 2
index = binarySort(arr, l, r, ele)
if index == -1:
    print "not found"
    print "found at index -- "+str(index)

Friday, March 4, 2016

How to setup SVN server on ubuntu 14.04

Step 1 » Issue the below command to update the repositories.

sudo apt-get update
Step 2 » After updating repositories , Issue the below command to install SVN and apache webserver (To access SVN through http ) .
sudo apt-get install subversion apache2 libapache2-svn apache2-utils
Step 3 » Now create a directory and create a new repository in that directory ( Here i’m using testrepo as repository name ).
sudo mkdir -p /svn/repos/
sudo svnadmin create /svn/repos/testrepo

Step 4 » Now change ownership for the repository.
sudo chown -R www-data:www-data /svn/repos/testrepo
Step 5 » Create a file testrepo.conf in /etc/apache2/sites-available/ and add the below lines for creating apache virtual host.
SVNParentPath /svn/repos/ : Parent Directory without repository name.
AuthUserFile /etc/svnpasswd : File need to be created ( Step 8) for user details.
Step 6 » Issue the below command to enable the Site ( testrepo in the below command should match the file name created in the previous step )
sudo a2ensite testrepo
Step 7 » Now restart or reload apache service.
sudo service apache2 reload
Step 8 » Issue the command to create user for accessing repository and add the user details to /etc/svnpasswd file.
Use this command to create first user.
sudo htpasswd -cm /etc/svnpasswd user1
Use the same command without c option to create additional users.
sudo htpasswd -m /etc/svnpasswd user2
Step 9 » Now you can access http://yourip/svn/testrepo in your browser ( Eg ) and you can see the page like below after successful authentication.
Setup svn server ubuntu 14.04
That’t it , your server is ready.