python language programming lab file ( Aktu college ) , python programing



___________________________________________________________________________________


Write a python program linear search.


10 14 19 26 27 31 33 35 42 44

def linearsearch(arr, x):
   for i in range(len(arr)):
      if arr[i] == x:
         return i
   return -1
arr = ['t','u','t','o','r','i','a','l']
x = 'a'
print("element found at index "+str(linearsearch(arr,x)))

element found at index 6

___________________________________________________________________________________


Write python program binary search 



  1. def binary_search(list1, n):  
  2.     low = 0  
  3.     high = len(list1) - 1  
  4.     mid = 0  
  5.   
  6.     while low <= high:  
  7.         # for get integer result   
  8.         mid = (high + low) // 2  
  9.   
  10.         # Check if n is present at mid   
  11.         if list1[mid] < n:  
  12.             low = mid + 1  
  13.   
  14.         # If n is greater, compare to the right of mid   
  15.         elif list1[mid] > n:  
  16.             high = mid - 1  
  17.   
  18.         # If n is smaller, compared to the left of mid  
  19.         else:  
  20.             return mid  
  21.   
  22.             # element was not present in the list, return -1  
  23.     return -1  
  24.   
  25.   
  26. # Initial list1  
  27. list1 = [12, 24, 32, 39, 45, 50, 54]  
  28. n = 45  
  29.   
  30. # Function call   
  31. result = binary_search(list1, n)  
  32.   
  33. if result != -1:  
  34.     print("Element is present at index", str(result))  
  35. else:  
  36.     print("Element is not present in list1")  



Element is present at index: 4  4

____________________________________________________________________________________








Write a python programing merge sort 


def mergeSort(myList):
    if len(myList) > 1:
        mid = len(myList) // 2
        left = myList[:mid]
        right = myList[mid:]

        # Recursive call on each half
        mergeSort(left)
        mergeSort(right)

        # Two iterators for traversing the two halves
        i = 0
        j = 0
        
        # Iterator for the main list
        k = 0
        
        while i < len(left) and j < len(right):
            if left[i] <= right[j]:
              # The value from the left half has been used
              myList[k] = left[i]
              # Move the iterator forward
              i += 1
            else:
                myList[k] = right[j]
                j += 1
            # Move to the next slot
            k += 1

        # For all the remaining values
        while i < len(left):
            myList[k] = left[i]
            i += 1
            k += 1

        while j < len(right):
            myList[k]=right[j]
            j += 1
            k += 1

myList = [54,26,93,17,77,31,44,55,20]
mergeSort(myList)
print(myList)


output: [17, 20, 26, 31, 44, 54, 55, 77, 93]



























_____________________________________________________________________________________


Write a python programing insertion sort . 


def insertion_sort(alist):
    for i in range(1len(alist)):
        temp = alist[i]
        j = i - 1
        while (j >= 0 and temp < alist[j]):
            alist[j + 1] = alist[j]
            j = j - 1
        alist[j + 1] = temp
 
 
alist = input('Enter the list of numbers: ').split()
alist = [int(x) for x in alist]
insertion_sort(alist)
print('Sorted list: ', end='')
print(alist)


Output : Enter the list of numbers: 2 5 8 7 9 1
Sorted list: [1, 2, 5, 7, 8, 9]

Post a Comment

0 Comments