Thursday, June 22, 2023

Python Data Structure

 

                     DATA STRUCTURES

 [Sorting and Searching]

Bubble  sorting

For Example:

#Bubble sort method to short list elements in ascending order

N=(83,25,50,70,14,74,67,90)

for i in range (0,len(N)-1):

  for j in range (0,i+1):

     if N[j]>N[j+1]:

          temp=N[j]

          N[j]=N[j+1]

          N[j+1]=temp

print(N)

OUTPUT:

[14,25,50,70,67,74,83,90]

>>> 

SELECTION SORT

#Selection sort method to sort list elements in ascending order

N=[83,25,50,70,14,74,67,90]

for i in range(0,len(N)-1):

      minpos=i

      for j in range(i+1,len(N)-1):

          if N[j]<N[minpos]:

              minpos=j

          temp=N[i]

          N[i]=N[minpos]

          N[minpos]=temp

else:

      print(N)

INSERTION SORT:

#Insertion sort method to sort list elements is ascending order

N=[83,25,50,70,14,74,67,90]

for i in range (1,len(N)):

        curval=N(i)

        curpos=i

        while  curpose >0 and N[curpos-1]>curval:

              N[curpos]=N[curpos-1]

              curpos=curpos-1

              N[curpos]=curval

#this is else part of for loop.

#it execute at the end of for loop.

else:

      print(N)

SEARCH:

Search is a process of finding a values in a list of values.

Linear or Sequential Search

Linear search or sequential search is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted.The list need not be ordered.

#Example for Linear Search data from 1 .... n in a unordered list

N=[13,5,7,4,12,8,9,11,23,45,56]

while True:

            find_num=int(input("Enter number to find.."))

            for i in range(0,len(N)-1)

                if N[i]==find_num:

                        print('Number Found')

                  break

            else:

                print('The given is not in the List')

            ch=raw_input("continue[y/n]..?")

            if ch in ('y','Y'):

                    continue

            else:

                    break

OUTPUT

Enter number to find ....11

Number Found

Continue[y/n]...?y

Enter number to find ....34

The given is not in the List

Continue[y/n]...?n

Binary search:

Binary search will search the data in the middle of the interval ,first and last of every position.

#Binary search method to search data using midpoint

N=[3,5,7,8,8,11,23,45,56]

while True:

      find_num=int(input("enter number to find..."))

      first=0

      last=len(N)-1

      while first<=last:

            midpoint=(first+last)//2

            if N[midpoint]==find_num:

                print("found")

            break

      else:

        if find_num<N[midpoint]:

             last=midpoint-1

        else:

            first=midpoint+1

    else:

        print("The given is not in list")

      ch=raw_input("continue[y/n]..?")

    if ch in('y','Y'):

       continue

    else:

       break

OUTPUT:

Enter number to find...11

Found

continue[y/n]..?y

enter number to find...56

Found

continue[y/n]..?y

enter number to find...3

Found

continue[y/n]...?y

enter number to find...7

Found

continue[y/n]..?y

enter number to find...10

The given is not in list

                                     

 

0 comments:

Post a Comment