Posts

Showing posts with the label Searching and sorting

Selection sort program in python

Image
           WRITE A PROGRAM FOR SELECTION SORT def Selection_Sort(MyList):     #i - outer loop     #j - Inner loop     #k - index of the smallest element          for i in range(len(MyList)-1):         k=i   #i th element is assumed to be smallest         for j in range(i+1,len(MyList)):             if(MyList[j]<MyList[k]):                 k=j         if (k!=i):             temp=MyList[i]             MyList[i]=MyList[k]             MyList[k]=temp MyList=[12,34,56,2,6,44,32,67,89] print("Element before swapping:") print(MyList) Selection_Sort(MyList) print("Elements After Sorting:") print(MyList) PROGRAM:-  OUTPUT:-

BUBBLE SORT IN PYTHON

Image
WRITE A PROGRAM TO IMPLEMENT BUBBLE SORT. def Bubble_Sort(MyList):     for i in range(len(MyList)-1,0,-1):         for j in range(i):             if MyList[j]>MyList[j+1]:                 temp,MyList[j]=MyList[j],MyList[j+1]                 MyList[j+1]=temp MyList=[30,50,45,23,20,90,79] print("Elements of List Before Sorting:",MyList) Bubble_Sort(MyList) print('Element of List After Sorting:',end='') print(MyList)      EXPLANATION:- In the above program,the unsorted list is declared and initialized. The same list is passed as an argument to the function bubble sort.The list slicing operation is used to iterate through the loop. Each element is compared with its adjacent element and interchanged if the first element is greater than the second. Swapping of elements is done using Python's simultaneous assignment as shown. PROGRAM:- OU...

BINARY SEARCH IN PYTHON

Image
WRITE A PROGRAM FOR BINARY SEARCH def Binary_Search(MyList,key):     low=0     high=len(MyList)-1     while low<=high:         mid=(low+high)//2  #find the middle index         if MyList[mid]==key:  #if entered key matches the mid index element             return mid        #return mid index         elif key>MyList[mid]: #else if key is greater             low=mid+1         else:             high=mid-1     return -1          #if no match,return -1 MyList=[10,20,30,40,45,56,69,88] print(MyList) key=(eval(input("Enter the number to search:"))) x=Binary_Search(MyList,key) if(x==-1):     print(key,"is not present in the list") else:     print("The Element",key,"is found at the position",x+1) EXPLANATION:-...

LINEAR SEARCH PROGRAM IN PYTHON

Image
WRITE A PROGRAM TO SEARCH AN ELEMENT FROM A LIST IN PYTHON def Linear_Search(My_List,key):     for i in range(len(My_List)):         if(My_List[i]==key):             print(key,"is found at index ",i)             return i             break     return -1 My_List=[12,23,45,67,89] print("Constents of List are as follows:") print(My_List) key=(int(input("Enter the number to be searched:"))) L1=Linear_Search(My_List,key) if(L1!=-1):     print(key,"is found at position",L1+1) else:     print(key,"is not found in the List") EXPLANATION:-  In the above program,we have defined the function called Linear_Search(). The list and element to be searched, i.e the key ,is passed to the function. Th comparison starts from the first element of the list.The comparison goes on sequentially till the key element matches the element present within the list i...