Git Product home page Git Product logo

sorting-algorithm'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

''' 
Program to sort the elements in the list using the Selection Sort algorithm.
Developed by: Lokesh N
RegisterNumber: 212222100023
'''
def selection_sort(nums):
    # write your code here using selection sort
    for i in range(len(nums)):
        low=i
        for j in range(i+1,len(nums)):
            if nums[j]<nums[low]:
                low=j
        nums[i],nums[low]=nums[low],nums[i]
    print(nums)
list_of_nums = eval(input())
selection_sort(list_of_nums)




ii) #Insertion Sort

''' 
Program to sort the elements in the list using the Insertion Sort algorithm.
Developed by: Lokesh N
RegisterNumber: 212222100023
'''
def insertion_sort(nums):
    # Write your code here to sort the elements in the list using Insertion sort algorithm
    for i in range(1,len(nums)):
        insert=nums[i]
        j=i-1
        while j>=0 and nums[j]>insert:
            nums[j+1]=nums[j]
            j-=1
        nums[j+1]=insert
    print(nums)
    
    
list_of_nums = eval(input())
insertion_sort(list_of_nums)


Output:

i) #Selection Sort image ii) #Insertion Sort image

Result:

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

sorting-algorithm's People

Contributors

lokeshnarayanan avatar etjabajasphin 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.