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 XII - Python Connectivity with MySQL

  Python Connectivity with MySQL In real-life applications, the data inputted by the user and processed by the application needs to be saved permanently, so that it can be used for future manipulation. Usually, input and output data is displayed during execution but not stored, as it resides in main memory , which is temporary — i.e., it gets erased once the application is closed. This limitation can be overcome by sending the data to be stored in a database , which is made accessible to the user through a Front-End interface . Key Concepts Database A database is an organized collection of data that is stored and accessed electronically from a computer system. DBMS (Database Management System) A DBMS is software that interacts with end-users, applications, and the database to capture and analyze data. Front-End The Front-End is the user interface of the application, responsible for input/output interaction with the user. Back-End The Back-End refe...

GRADE XI | DESIGN THINKING & INNOVATION (848) | INTRO | CH-1

  DESIGN THINKING &  INNOVATION (848)  INTRODUCTION LINK : INTRODUCTION PPT PPT LINK Notes: Design Thinking and Innovation Grade XI Introduction Section   I. What is Design? (0.1.1) Definitions from Experts: John Maeda: "Design is solution to a problem". Saul Bass: "Design is thinking made visual". Charles Eames:   “Design is a plan for arranging elements in such a way as best to accomplish a particular purpose.” Steve Jobs:  "Design is not just what it looks like and feels like. Design is how it works". Prof. Sudhakar Nadkarni:  "Essentials of design are— purity, precision, details". Design is a way of understanding needs, identifying problems, and creating appropriate and innovative solutions. It is not only about appearance, but also about usefulness and sustainability. Design is explained as something that helps solve problems and make a positive difference. Key ...