0% found this document useful (0 votes)
10 views3 pages

Unit Iv Ip

The document contains Python programs for sorting numbers using selection sort, insertion sort, and merge sort, along with an example of creating a histogram from a list of integers. Each sorting algorithm is presented with its function definition, input, and output examples. The histogram function visually represents the frequency of integers using asterisks.

Uploaded by

NivedhithaV
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)
10 views3 pages

Unit Iv Ip

The document contains Python programs for sorting numbers using selection sort, insertion sort, and merge sort, along with an example of creating a histogram from a list of integers. Each sorting algorithm is presented with its function definition, input, and output examples. The histogram function visually represents the frequency of integers using asterisks.

Uploaded by

NivedhithaV
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

UNIT IV

1.python program to sort a set of numbers using selection sort.

def selectionsort( a ):
for i in range( len( a ) ):
min = i
for k in range( i + 1 , len( a ) ):
if a[k] < a[min]:
min = k

tmp = a[min]; a[min] = a[i]; a[i] = tmp

a=list(input("Enter the list of numbers"))


selectionsort(a)
print("sorted list:")
for i in a:
print(i)

Output:
Enter the list of numbers56,78,-23,1,0,89,100,6
sorted list:
-23
0
1
6
56
78
89
100

2.python program to sort a set of numbers using insertion sort.

def insertionSort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j =j-1
arr[j+1] = key

arr = list(input("Enter the list of elements"))


insertionSort(arr)
print ("Sorted array is:")
for i in arr:
print (i)
Output:
Enter the list of elements56,0,-8,12,3
Sorted array is:
-8
0
3
12
56

3.python program to sort the set of numbers using merge sort.

def mergeSort(nlist):
if len(nlist)>1:
mid = len(nlist)//2
left = nlist[:mid]
right = nlist[mid:]

mergeSort(left)
mergeSort(right)
i=j=k=0

while i < len(left) and j < len(right):


if left[i] < right[j]:
nlist[k]=left[i]
i=i+1
else:
nlist[k]=right[j]
j=j+1
k=k+1

while i < len(left):


nlist[k]=left[i]
i=i+1
k=k+1

while j < len(right):


nlist[k]=right[j]
j=j+1
k=k+1

nlist =list(input("Enter the list of numbers"))


mergeSort(nlist)
print(nlist)
Output:
Enter the list of numbers45,14,37,23,78,56,0,-10
[-10, 0, 14, 23, 37, 45, 56, 78]

4.python program to create a histogram from a list of integers.

def histogram( items ):


for n in items:
output = ''
times = n
while( times > 0 ):
output += '*'
times = times - 1
print(output)

histogram([2, 3, 6, 5])

output:

**
***
******
*****

You might also like