Skip to main content

Posts

Showing posts with the label Python

Quick Sort using Python | Data Structure and Algorithm

  Code: Note:- Scroll horizontally to see the full line of code. def swap ( arr , i , j ):     # takes arr as list, and swaps the element at i and j th postion of the arr     temp = arr [ i ]     arr [ i ] = arr [ j ]     arr [ j ] = temp def partition ( arr , start , end ):     # return the index of the pivot element of arr     pivot = arr [ end ]     i = start - 1     for j in range ( start , end ):         if ( arr [ j ] < pivot ):             i = i + 1             swap ( arr , i , j )     swap ( arr ,( i + 1 ), end )     return ( i + 1 ) def quick_sort ( arr , start , end ):     # takes arr as list to be sorted and start and end index of list arr     if ( start < end ):         pivot = partition ( arr , start , end )         # ...

Shell Sort using Python | Data Structure and Algorithm

  Code: Note:- Scroll horizontally to see the full line of code. def shell_sort ( a , n ):     # a is list to be sorted and n is the length of list     gap = n // 2     p = 1     while ( gap >= 1 ):         for i in range ( gap , n ):             current = a [ i ]             j = i - gap             while (( a [ j ] > current ) and j >= 0 ):                 temp = a [ j + gap ]                 a [ j + gap ] = a [ j ]                 a [ j ] = temp                 j = j - gap             a [ j + gap ] = current         gap = gap // 2         print ( " Pass " , p , " : " , a )      ...

Insertion Sort using Python | Data Structure and Algorithm

  Code: Note:- Scroll horizontally to see the full line of code. def insertion_sort ( a , n ):     # a is list to be sorted and n is the length of list     for i in range ( 1 , n ):         current = a [ i ]         j = i - 1         while (( a [ j ] > current ) and j >= 0 ):             temp = a [ j + 1 ]             a [ j + 1 ] = a [ j ]             a [ j ] = temp             j = j - 1         a [ j + 1 ] = current         print ( " Iteration " , i , " : " , a )     return a

Fibonacci Search using Python | Data Structure and Algorithms

  Code: Note:- Scroll horizontally to see the full line of code. def fibonacci_search ( a , target ):     # a is list of intergers and target is key element to be searched     n = len ( a )     fibn_2 = 0     fibn_1 = 1     fibn = fibn_1 + fibn_2     while ( fibn <= n ):         fibn_2 = fibn_1         fibn_1 = fibn         fibn = fibn_1 + fibn_2     offset =- 1     while ( fibn_1 != 0 ):         i = min (( offset + fibn_2 ), n - 1 )         if ( target > a [ i ]):             fibn = fibn_1             fibn_1 = fibn_2             fibn_2 = fibn - fibn_1             offset = i         elif ( target < a [ i ]):             fibn = f...

Sentinel Search using Python | Data Structure and Algorithm

  Code: Note:- Scroll horizontally to see the full line of code. def sentinel_search ( a , target ):     # a is list of intergers and target is key element to be searched     a . append ( target )     i = 0     while ( a [ i ] != target ):         i += 1     if ( i < ( len ( a ) - 1 )):         print ( target , " is present at index " , i )     else :         print ( target , " not found in list " )

Binary Search using Python | Data Structures and Algorithms

  Code: Note:- Scroll horizontally to see the full line of code. def binary_search ( a , target ):     # a is list of intergers and target is key element to be searched     start = 0     end = len ( a ) - 1     while ( start <= end ):         mid = int (( start + end ) / 2 )         if ( a [ mid ] == target ):             print ( target , " is present at index " , mid )             break         elif ( target > a [ mid ]):             start = mid + 1         elif ( target < a [ mid ]):             end = mid - 1     else :         print ( target , " not found in list " )

Linear Search using Python | Data Structure and Algorithm

  Code:  Note:- Scroll horizontally to see the full line of code. def linear_search ( a , target ):     # a is list of intergers and target is key element to be searched     for i in range ( len ( a )):         if ( a [ i ] == target ):             print ( target , " is present at index " , i )             break     else :         print ( target , " not found in list " )

Operations on Matrix using OOP concepts in Python | Addition | Subtraction | Multiplication | Transpose

      This is the same problem Statement as mentioned in  Operations on Matrix using Python | Addition | Subtraction | Multiplication | Transpose but, in this solution, we used oop concepts like class and member function, etc.   Problem:-  Write a Python program to compute the following computation on the matrix:     a) Addition of two matrices     b) Subtraction of two matrices     c) Multiplication of two matrices     d) transpose of a matrix Note: Scroll Horizontally to see the full line of code. class Matrix :     def __init__ ( self ):         self . matrix = None         self . r = None         self . c = None     def addelem ( self ):         self . r = int ( input ( " Enter the no. of rows: " ))         self . c = int ( input ( " Enter the no. of columns: " ))       ...

Set Theory Operations using OOP concepts in Python

    This is the same problem statement mentioned in    SET THEORY USING LIST IN PYTHON but, in this solution, we used Object-Oriented Programming Concepts like classes and class functions. Problem Statement :- In second-year computer engineering class, group A student's play cricket, group B students play badminton and group C students play football.  Write a Python program using functions to compute the following:-     a) List of students who play both cricket and badminton     b) List of students who play either cricket or badminton or both     c) Number of students who play neither cricket nor badminton     d) Number of students who play cricket and football but not badminton. (Note- While realizing the group, duplicate entries should be avoided, Do not use SET built-in functions) Note :- Scroll horizontally to see the full line of code. class Set :     def addElems ( self ):         sel...

Implementation of Sorting Algorithms using Python | Quick Sort | Partition Function

Problem Statement:  Write a Python program to store first year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using quick sort and display top five scores. Note :- Scroll horizontally to see the full line of code. def swap ( arr , i , j ):     temp = arr [ i ]     arr [ i ]= arr [ j ]     arr [ j ]= temp def partition ( arr , start , end ):     pivot = arr [ end ]     i = start - 1     for j in range ( start , end ):         if ( arr [ j ]< pivot ):             i = i + 1             swap ( arr , i , j )     swap ( arr ,( i + 1 ), end )     return ( i + 1 ) def quick_sort ( arr , start , end ):     if ( start < end ):         pivot = partition ( arr , start , end )         # print("Pivot element i...

Implementation of Sorting Algorithms using Python | Insertion Sort | Shell Sort

  Problem Statement: Write a Python program to store second year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using      a) Insertion sort     b) Shell Sort and display top five scores. Note :- Scroll horizontally to see the full line of code. def insertion_sort ( a , n ):     for i in range ( 1 , n ):         current = a [ i ]         j = i - 1         while (( a [ j ]> current ) and j >= 0 ):             temp = a [ j + 1 ]             a [ j + 1 ]= a [ j ]             a [ j ]= temp             j = j - 1         a [ j + 1 ]= current         print ( "Iteration" , i , ": " , a )     return a def shell_sort ( a , n ):     gap = n // 2 ...