Python Tutorial

Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Wednesday, April 24, 2013

Bubble sort in python

Bubble sort python code.

def bubble_sort(A):
    n = len(A)    
    for i in range(0,n):
        for j in range (i+1,n):
            if A[i]>A[j]:
                A[i],A[j]=A[j], A[i]

if __name__=="__main__":
    A = [7,3,5,2]
    print A
    bubble_sort(A)
    print A

Output:
[7, 3, 5, 2]
[2, 3, 5, 7]

Selection sort in python

Selection sort python code.

def get_index_of_smallest(A, i):
    index_of_smallest = i
    for j in range(i+1, len(A)):
        if A[index_of_smallest]>A[j]:
            index_of_smallest = j;
    return index_of_smallest


def selection_sort(A):
    for i in range (0, len(A)):
        index_of_smallest = get_index_of_smallest(A,i)
        A[index_of_smallest], A[i] = A[i], A[index_of_smallest]

if __name__=="__main__":
    A = [7,3,5,2]
    print A
    selection_sort(A)
    print A

Output:
[7, 3, 5, 2]
[2, 3, 5, 7]

Insertion sort in python

Insertion sort python code.


def insert(A,i):
    value = A[i]
    j = i
    while j != 0 and A[j-1]>value:
        A[j] = A[j-1]
        j = j - 1
    A[j] = value


def insertion_sort(A):
     for i in range(len(A)):
         insert(A, i)

if __name__=="__main__":
    A = [7,3,5,2]
    print A
    insertion_sort(A)
    print A


Output:
[7, 3, 5, 2]
[2, 3, 5, 7]

Thursday, June 7, 2012

python sort dictionary



Using sorted we can also dictionary data.



d={}
(d['a'],d['o'],d['g'])=("alpha","omega","gamma")

print d
for k in sorted( d.keys() ): 
   print k," ",d[k]

print d



Output:
{'a': 'alpha', 'g': 'gamma', 'o': 'omega'}
a   alpha
g   gamma
o   omega
{'a': 'alpha', 'g': 'gamma', 'o': 'omega'}

python sort tuple



Some example for sort tuple are given



a=[(1,"b"),(2,"a"),(1,"e")]
print a
print sorted(a)

def myTSort(d):
    return d[0] # sort according to first value

print sorted(a,key=myTSort)
print sorted(a,key=myTSort, reverse=True)
print a



Output:
[(1, 'b'), (2, 'a'), (1, 'e')]
[(1, 'b'), (1, 'e'), (2, 'a')]
[(1, 'b'), (1, 'e'), (2, 'a')]
[(2, 'a'), (1, 'b'), (1, 'e')]
[(1, 'b'), (2, 'a'), (1, 'e')]

python string array sort: pass custom function



Sorted is very easy to use. We can use both built-in and custom function for sorting. Some example are given here. Remember it always return a new array by kipping the original array



a=["Life", "is", "very","easy", "with", "python"]

def myFunction(data):
    return data[0]

print sorted(a)
print sorted(a,key=myFunction)
print sorted(a,key=len)
print sorted(a)



Output:
['Life', 'easy', 'is', 'python', 'very', 'with']
['Life', 'easy', 'is', 'python', 'very', 'with']
['is', 'Life', 'very', 'easy', 'with', 'python']
['Life', 'easy', 'is', 'python', 'very', 'with']

python sorted: sort array

I like sorted because it is easy to use, sort multi structered data and also keep the original data which very important some times
 
a=[3,2,1,4,5]
print a
print sorted(a)
print sorted(a,reverse=True)
print a


Output:
[3, 2, 1, 4, 5]
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
[3, 2, 1, 4, 5]