Showing posts with label Sorting. Show all posts
Showing posts with label Sorting. Show all posts

Nested List Sorting

# Nested List sort


L = [['IT', 56], ['CSE', 53], ['EEE', 52], ['ECE', 55]]


# Sorting a nested List using bubble sort

# default - sorts by first element of nested list


for i in range(0,len(L)):

for j in range(0,len(L)-1):

if(L[j]>L[j+1]):

L[j],L[j+1]=L[j+1],L[j]


print("Sort by first element (default)")

print(L)


# Sorting a nested List using bubble sort

# Sort by second element of nested list


for i in range(0,len(L)):

for j in range(0,len(L)-1):

if(L[j][1]>L[j+1][1]):

L[j],L[j+1]=L[j+1],L[j]

print("Sort by Second element")

print(L)


"""

Sample output

 >python NestedListSort.py


Sort by first element (default)


[['CSE', 53], ['ECE', 55], ['EEE', 52], ['IT', 56]]


Sort by Second element


[['EEE', 52], ['CSE', 53], ['ECE', 55], ['IT', 56]]


"""

PYTHON ASSIGNMENT 7: MISCELLANEOUS PROGRAMS

Operators and Control Structures

Swap two numbers

Circulate the values of n variables

Find GCD of two numbers

Distance between two points

Square root of a number using Newton’s method 

Exponentiation of a number 

First N prime numbers



List

Maximum in a given list of numbers

Sum of elements in a list

Matrix multiplication


Search

Linear search

Binary search


Sort

Insertion sort

Selection sort

Merge sort


Dictionaries : Histograms

Read a string and find the 5 most frequent characters

Read a string and find the 3 most frequent words


File operations:

File copy

Merge two files

Read a file name from command line and count the number of lines, words and characters in the file.

Read a file and create a dictionary for the frequency of words in the file

Read a file and create a dictionary for the frequency of characters in the file.

Sorting - Merge sort - Functions

# Merge sort

# Function to merge sorted lists
def merge(a, b):
    c = []
    while (len(a) != 0 and len(b) != 0):
        if a[0] < b[0]:
            c.append(a[0])
            a.remove(a[0])
        else:
            c.append(b[0])
            b.remove(b[0])
    if(len(a) == 0):
        c = c + b
    else:
        c += a
    return c

# Function - create sublists
def mergesort(L1):
    if len(L1) == 0 or len(L1) == 1:
        return L1
    else:
        middle = len(L1)//2
        a = mergesort(L1[:middle])
        b = mergesort(L1[middle:])

    return merge(a,b)

# Read elements and create list

no_of_terms = int(input("Enter number of terms : "))
Original_List = []

for idx in range(0, no_of_terms, 1):
    ele = int(input("Enter element : "))
    Original_List.append(ele)

Sorted_list = mergesort(Original_List)

print("Original list :", Original_List)
print("Sorted List :", Sorted_list)

Sorting - Selection sort

# Selection sort

# Read elements and create list

no_of_terms = int(input("Enter number of terms : "))
List1 = []

for idx in range(0, no_of_terms, 1):
    ele = int(input("Enter element : "))
    List1.append(ele)

print("Original list :", List1)

for idx in range(0, len(List1)-1, 1):
    min_ele = min(List1[idx:len(List1)])
    pos = List1.index(min_ele)
    if(pos != idx):   
        List1[pos], List1[idx]  = List1[idx], List1[pos]
       
print("Sorted List :", List1)

Sorting - Insertion sort

# Insertion sort

# Read elements and create list
no_of_terms = int(input("Enter number of terms : "))
List1 = []

for idx in range(0, no_of_terms, 1):
    ele = int(input("Enter element : "))
    List1.append(ele)

print("Original list :", List1)

for idx in range(1, len(List1), 1):
    while( idx>0 and List1[idx-1]>List1[idx] ):
        List1[idx],List1[idx-1] = List1[idx-1],List1[idx]
        idx = idx-1
       
print("Sorted List :", List1)