0% found this document useful (0 votes)
2 views

5Insertion and shell sort

The document outlines a Python program designed to store and sort the second-year percentage scores of students using two sorting methods: Insertion Sort and Shell Sort. It prompts the user to input the number of students and their respective scores, then allows the user to choose which sorting method to apply. The program displays the unsorted list and the sorted list based on the chosen method.

Uploaded by

Yash Dhumal
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)
2 views

5Insertion and shell sort

The document outlines a Python program designed to store and sort the second-year percentage scores of students using two sorting methods: Insertion Sort and Shell Sort. It prompts the user to input the number of students and their respective scores, then allows the user to choose which sorting method to apply. The program displays the unsorted list and the sorted list based on the chosen method.

Uploaded by

Yash Dhumal
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/ 3

B-15 Write a Python program to store second year percentage of students in array.

Write
function for sorting array of floating point numbers in ascending order using
a) Insertion sort
Shell Sort and display top five scores

def insertion_sort(list1):

# Outer loop to traverse through 1 to len(list1)

for i in range(1, len(list1)):

value = list1[i]

# Move elements of list1[0..i-1], that are

# greater than value, to one position ahead

# of their current position

j=i-1

while j >= 0 and value < list1[j]:

list1[j + 1] = list1[j]

j -= 1

list1[j + 1] = value

return list1

# Driver code to test above

def shellSort(array, n):


# Rearrange elements at each n/2, n/4, n/8, ... intervals

interval = n // 2

while interval > 0:

for i in range(interval, n):

temp = array[i]

j=i

while j >= interval and array[j - interval] > temp:

array[j] = array[j - interval]

j -= interval

array[j] = temp

interval //= 2

list = []

num_stu = int(input("How many number of students:"))

for i in range (num_stu):

list.append(float(input("Enter the marks:")))

print("1.Insertion Sort \n2.ShellSort")

choise = int(input("Enter your choise:"))

if (choise == 1):
print("The unsorted list is:", list)

print("The sorted marks list by Insertion method is:", insertion_sort(list))

elif (choise==2):

print("The unsorted list is:", list)

size = len(list)

shellSort(list, size)

print('Sorted marks list by ShellSort method is:',list)

else:

print("Invalid your choise.")

You might also like