___________________________________________________________________________________
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
- def binary_search(list1, n):
- low = 0
- high = len(list1) - 1
- mid = 0
- while low <= high:
- # for get integer result
- mid = (high + low) // 2
- # Check if n is present at mid
- if list1[mid] < n:
- low = mid + 1
- # If n is greater, compare to the right of mid
- elif list1[mid] > n:
- high = mid - 1
- # If n is smaller, compared to the left of mid
- else:
- return mid
- # element was not present in the list, return -1
- return -1
- # Initial list1
- list1 = [12, 24, 32, 39, 45, 50, 54]
- n = 45
- # Function call
- result = binary_search(list1, n)
- if result != -1:
- print("Element is present at index", str(result))
- else:
- 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(1, len(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]
 
 
 
 
 Posts
Posts
 
 
 
 
0 Comments