Code Monkey home page Code Monkey logo

sorting-algorithms's Introduction

Selection sort and Insertion sort

Aim:

To write a program to perform selection sort and insertion sort using python programming.

Equipment’s required:

  1. Hardware – PCs
  2. Anaconda – Python 3.7 Installation / Moodle-Code Runner

Algorithm:

Selection Sort Algorithm:

  1. Set the first unsorted element as the minimum
  2. For each of the unsorted elements, check if the element < current minimum.
  3. If yes, set the element as the new minimum.
  4. Swap minimum with first unsorted position.
  5. Repeat the steps 2 and 3 for all the elements in the array.

Insertion Sort Algorithm:

  1. Set the first element as sorted element j.
  2. For each unsorted element X, check if current sorted element j >X.
  3. If yes, move sorted element to the right by 1.
  4. Break the loop and insert X.
  5. Repeat the steps 2 to 4 for sorting all the elements in the array.

Program:

i) #Selection Sort

'''To sort the elements in the list using the Selection Sort algorithm
Developed BY : LAVANYA S
Register No : 212223230112'''
num=eval(input())
for i in range(len(num)):
    low=i
    for j in range(i+1,len(num)):
        if num[j]<num[low]:
            low=j
    num[i],num[low]=num[low],num[i]
print(num)    

ii) #Insertion Sort

'''To sort the elements in the list using the Insertion Sort algorithm
Developed BY : LAVANYA S
Register No : 212223230112'''
num=eval(input())
for i in range(1,len(num)):
    insert=num[i]
    j=i-1
    while j>=0 and num[j]>=insert:
        num[j+1]=num[j]
        j=j-1
    num[j+1]=insert
print(num)    
    

Output:

i) #Selection Sort alt text

ii) #Insertion Sort alt text

Result:

Thus the program is written to perform selection sort and insertion sort using python programming.

sorting-algorithms's People

Contributors

drgbhuvaneswari avatar lavanya-0709 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.