0% found this document useful (0 votes)
53 views6 pages

Selection Sort and Insertion Sort

The document describes selection sort and insertion sort algorithms. It includes the aim, algorithm, program code, and output for each sorting method. For selection sort, it iterates through the list and in each iteration selects the minimum element and places it in the sorted position. For insertion sort, it splits the array into sorted and unsorted parts, then iterates through the unsorted part and inserts each element into the correct position in the sorted part. Both algorithms are demonstrated on a sample input list and output the sorted list.

Uploaded by

john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views6 pages

Selection Sort and Insertion Sort

The document describes selection sort and insertion sort algorithms. It includes the aim, algorithm, program code, and output for each sorting method. For selection sort, it iterates through the list and in each iteration selects the minimum element and places it in the sorted position. For insertion sort, it splits the array into sorted and unsorted parts, then iterates through the unsorted part and inserts each element into the correct position in the sorted part. Both algorithms are demonstrated on a sample input list and output the sorted list.

Uploaded by

john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXP NO : 4 NAME : RAIZUDEEN S

DATE : STUDENT ID : Sec20cs 135

SELECTION SORT AND INSERTION SORT


SELECTION SORT :
AIM :
To arrange the elements in a list using selection sort.

ALGORITHM :

Step 1:Get the value of n which is the total size of the


array
Step 2 : Partition the list into sorted and unsorted
sections. The sorted section is initially empty while the
unsorted section contains the entire list
Step 3 : Pick the minimum value from the
unpartitioned section and placed it into the sorted section.
Step 4 : Repeat the process (n – 1) times until all of
the elements in the list have been sorted.
EXP NO : 4 NAME : RAIZUDEEN S
DATE : STUDENT ID : Sec20cs 135

PROGRAM :

def selectionSort( itemsList ):

n = len( itemsList )

for i in range( n - 1 ):

minValueIndex = i

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

if itemsList[j] < itemsList[minValueIndex] :

minValueIndex = j

if minValueIndex != i :

temp = itemsList[i]

itemsList[i] = itemsList[minValueIndex]

itemsList[minValueIndex] = temp

return itemsList

list1= [21,6,9,33,3]

print(selectionSort(el))
EXP NO : 4 NAME : RAIZUDEEN S
DATE : STUDENT ID : Sec20cs 135

OUTPUT SCREENSHOT :

RESULT:
The selection sort program executed successfully and
output is verified.
EXP NO : 4 NAME : RAIZUDEEN S
DATE : STUDENT ID : Sec20cs 135

INSERTION SORT
AIM :
To arrange the elements in a list using insertion sort.

ALGORITHM :

Step 1:Spilt a list in two parts - sorted and unsorted.

Step 2 : Iterate from arr[1] to arr[n] over the given


array.

Step 3 : Compare the current element to the next


element.

Step 4 : If the current element is smaller than the next


element, compare to the element before, Move to the
greater elements one position up to make space for the
swapped element.
EXP NO : 4 NAME : RAIZUDEEN S
DATE : STUDENT ID : Sec20cs 135

PROGRAM:

def insertionSort(array):

for step in range(1, len(array)):


key = array[step]
j = step - 1
while j >= 0 and key < array[j]:
array[j + 1] = array[j]
j=j-1
array[j + 1] = key
data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted Array in Ascending Order:')
print(data)
EXP NO : 4 NAME : RAIZUDEEN S
DATE : STUDENT ID : Sec20cs 135

OUTPUT SCREEN SHOT:

RESULT :
The insertion sort program has been executed successfully
and output is verified.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy