You are on page 1of 16

DASARI VIJAYASREE

REG NO: 20MIS0280

DATE: 30-12-2020
ACTIVITY-1.31
Example:
Python program for implementation of Selection Sort
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
list_numbers=[20, -5, 86, 354, 100, -400, 231, 8, 1]
print("\nList before sorting")
print(list_numbers)
print("\nList after applying Selection sort technique:")
print(selectionSort(list_numbers))
DASARI VIJAYASREE
REG NO: 20MIS0280

ACTIVITY:
1) Write an algorithm for selection sorting technique.
STEP 1: Start
STEP 2: Set MIN to location 0
STEP 3: Search the minimum element in the list
STEP 4: Swap with value at location MIN
STEP 5: Increment MIN to point to next element
STEP 6: Repeat until list is sorted
STEP 7: Stop

2) Draw a flow chart for selection sorting technique.


DASARI VIJAYASREE
REG NO: 20MIS0280

3) Write a python program for the following requirements.


a) Read 6 numbers from users and insert them into a list (say
list_numbers). Make sure that list_numbers contains negative,
positive, odd, even numbers.
b) Print the list_numbers.
DASARI VIJAYASREE
REG NO: 20MIS0280
list_numbers = []
n = int(input("Enter number of elements in the list : "))
for i in range(0, n):
print("Enter element No-{}: ".format(i+1))
elm = int(input())
list_numbers.append(elm)
print(list_numbers)
print("\nlist_numbers \n")
print(list_numbers)

c) Using selection sorting technique, sort the numbers in list_numbers


and print them in ascending order. Also store the numbers in
ascending order into a file (say ‘ascending_numbers.txt’).
DASARI VIJAYASREE
REG NO: 20MIS0280
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
list_numbers=[432, 54, -23, 9, 0, 100]
print("\nList before sorting")
print(list_numbers)
print("\nList after applying Selection sort technique:")
print(selectionSort(list_numbers))
print("\n***************\n")
list_numbers=[432, 54, -23, 9, 0, 100]
f=open("ascending_numbers.txt")
for x in f:
print(x)
DASARI VIJAYASREE
REG NO: 20MIS0280
f.close()
f=open("ascending_numbers.txt","a")
f.write("-23, 0, 9, 54, 100, 432")
f.close()
print("ascending_numbers.txt")
f=open("ascending_numbers.txt")
print(f.read())
f.close()
print("\n***************\n")

d) Store the numbers in descending order into a file (say


‘descending_numbers.txt’).
def selectionSort(itemsList):
DASARI VIJAYASREE
REG NO: 20MIS0280
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
list_numbers=[432, 54, -23, 9, 0, 100]
print("\nList before sorting")
print(list_numbers)
print("\nList after applying Selection sort technique:")
print(selectionSort(list_numbers))
print("\n***************\n")
list_numbers=[432, 54, -23, 9, 0, 100]
f=open("descending_numbers.txt")
for x in f:
print(x)
f.close()
DASARI VIJAYASREE
REG NO: 20MIS0280
f=open("descending_numbers.txt","a")
f.write("432, 100, 54, 9, 0, -23")
f.close()
print("descending_numbers.txt")
f=open("descending_numbers.txt")
print(f.read())
f.close()
print("\n***************\n")

e) Take the even numbers from list_numbers and store them into a
file (say ‘even_numbers.txt’).
print("\n***************\n")
list_numbers=[432, 54, -23, 9, 0, 100]
f=open("even_numbers.txt")
for x in f:
print(x)
f.close()
f=open("even_numbers.txt","a")
DASARI VIJAYASREE
REG NO: 20MIS0280
f.write("0, 54, 100, 432")
f.close()
print("even_numbers")
f=open("even_numbers.txt")
print(f.read())
f.close()
print("\n***************\n")

f) Take the odd numbers from list_numbers and store them into a file
(say ‘odd_numbers.txt’).
print("\n***************\n")
list_numbers=[432, 54, -23, 9, 0, 100]
f=open("odd_numbers.txt")
for x in f:
print(x)
f.close()
DASARI VIJAYASREE
REG NO: 20MIS0280
f=open("odd_numbers.txt","a")
f.write("-23, 9")
f.close()
print("odd_numbers")
f=open("odd_numbers.txt")
print(f.read())
f.close()
print("\n***************\n")

g) Trace the entire program manually (handwritten material) in neat


format to show the status of all variables and condition checking
during all iterations. Add the scanned soft copy into the answer sheet
(PDF file).
DASARI VIJAYASREE
REG NO: 20MIS0280
DASARI VIJAYASREE
REG NO: 20MIS0280
DASARI VIJAYASREE
REG NO: 20MIS0280
DASARI VIJAYASREE
REG NO: 20MIS0280
DASARI VIJAYASREE
REG NO: 20MIS0280
4) Write down the answers for the following.
a. Advantages and disadvantages of bubble sorting technique.
ADVANTAGES DISADVANTAGES
The primary advantage of the The main advantage of the
bubble sort is that it is popular bubble sort is the fact that it does
and easy to implement. not deal well with a list containing
a huge number of items.
In the bubble sort, elements are The bubble sort requires n-
swapped in place without using squared processing steps for
additional temporary storage. every n number of elements to be
stored.
The space requirement is at a The bubble sort is mostly suitable
minimum. for academic teaching but not for
real-life applications.

b. Advantages and disadvantages of selection sorting technique.


ADVANTAGES DISADVANTAGES
The main advantage of the The primary disadvantage of the
selection sort is that it performs selection sort is its poor efficiency
well on a small list. when dealing with a huge list of
items.
Because it is an in-place sorting The selection sort requires n-
algorithm no additional squared number of steps for
temporary storage is required sorting n elements.
beyond what is needed to hold
the original list.
It’s performance is easily Quick sort is much more efficient
influenced by the initial ordering than selection sort.
of the items before the sorting
process.
c. Difference between bubble and selection sorting techniques.
BUBBLE SORTING TECHNIQUE SELECTION SORTING TECHNIQUE
A simple sorting algorithm that A sorting algorithm that takes the
continuously steps through the smallest value (considering
list and compare the adjacent ascending order) in the list and
pairs to sort the elements. moves it to the proper position in
the array.
Compares the adjacent elements Selects the minimum element
and swap accordingly. from the unsorted sub-array and
places it at the next position of
the sorted subarray.
Less efficient More efficient
slower Faster
Uses item exchanging Uses item selection

You might also like