Skip to main content

CS - SORTING/SEARCHING ALGORITHMS

 

SORTING ALGORITHMS

                     SORTING ALGORITHM PDF LINK

#Bubble Sort

       ·       The outer loop iterates through the entire array.

·       The inner loop compares adjacent elements and swaps them if they are out of order.

·       The outer loop runs n times, and each pass moves the largest element to its correct position.


arr=[3,8,5,2,1]

n = len(arr)

print(n)

for i in range(n):  #traverse through all the elements

        # Last i elements are already sorted, no need to check them

        for j in range(0, n-i-1):

             # Swap if the element found is greater than the next element

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

                arr[j], arr[j+1] = arr[j+1], arr[j]        

print(arr)


SELECTION SORT ALGORITHM EXPLANATION


num=[64,25,12,22,11]

print(num)

for i in range(len(num)):

        # Find the minimum element in the remaining unsorted array

        mini = i

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

                if num[j] < num[mini]:  

                    mini = j

        # Swap the found minimum element with the first element of the unsorted part

        num[i], num[mini] = num[mini], num[i]

print(num)


REFER PDF FOR DRY RUN :SORTING ALGORITHM PDF LINK

#INSERTION SORT

list1=[]

n=int(input("enter no of elements"))  

i=0

while i<n:

    list1+=[int(input("enter element"))]  

    i+=1

for i in range(1,n):

    temp=list1[i]  

    j=i-1  

    while temp<list1[j] and j>=0:

        list1[j+1]=list1[j]  

        j-=1

        list1[j+1]=temp

print(list1)


#SEARCHING ALGORITHM

#Linear / Sequential Search

list = [2, 4, 7, 10, 13, 17, 20, 24, 27, 31]

element=4

found=0

for i in range(len(list)):

    if list[i]==element:

        print("found at index :",i)

        found=1

        break

if not found:

    print("Element not found")

    

#BINARY SEARCH

n=[10,20,30,40,50,60,70]

target=60

beg=0

low=len(n)-1

while beg<=low:

    mid=(beg+low)//2

    if n[mid]==target:

        print("element found:",mid)

        break

    elif n[mid]<target:

        beg=mid+1

    else:

        low=mid-1

else:

    print("target not found")

Comments

Popular posts from this blog

GRADE XI - NESTED FOR LOOP

                                                         NESTED FOR LOOP 1. for var1 in range(3):      print(var1,"OUTER LOOP")          # 0 outer loop       1 outer loop      2 outer loop          for var2 in range(2):                  print(var2+1,"INNER LOOP")    #1  2 inner loop     1  2  inner loop   1 2 inner loop  2. Print the following pattern using for loop: 1  1 2  1 2 3  1 2 3 4  Sol: for r in range(1,5):   #ROWS     for c in range(1,r+1):   #COLUMNS         print(c,end=" ")     print() 3. Print the following pattern using for loop: @  @ @  @ @ @...

LIST IN PYTHON - 14TH AUG

  LIST IN PYTHON BEGINNERS PRACTICE QUESTIONS 1. Creating List by accepting data from user List1=[] #Empty list n=int(input("Enter number of elements"))    i=0 while i < n:     num= int(input("Enter element " + str(i) + ": "))     List1+=[num]       i+=1 print(List1) OR #Creating List by accepting data from user List1=[ ] n=int(input("Enter number of elements"))   i=0 while i < n:     List1+=[int(input("Enter the element"))]       i+=1     print(List1) 2.Write a program to input the size and the list of elements from user. Count the number of elements  which are divisible by 5 list1= [ ] s =int (input("Enter no of elements")) for i in range(s):     list1 += [int (input("Enter the Elements"))] count=0 for num in list1:         if num % 5 ==0:             count = count+1 print("No. of elements divisible by 5--",c...