ILLUSTRATIVE PROGRAM
data = []
print('Selection
Sort :')
n =
int(raw_input('Enter Number of Elements in the Array: '))
for i in
range(0, n):
x =
raw_input('Enter the Element %d :' %(i+1))
data.append(x)
print('Original
Array :')
print(data)
print('Intermediate
s :')
for i in
range(0,n-1):
small=int(data[i])
pos=i
for j in
range(i+1,n):
if
int(data[j])<small:
small=int(data[j])
pos=j
temp=data[i]
data[i]=data[pos]
data[pos]=temp
print(data)
print('Sorted
Array :')
print(data)
Insertion
sort
data = []
print('Insertion
Sort :')
n =
int(raw_input('Enter Number of Elements in the Array: '))
for i in
range(0, n):
x =
raw_input('Enter the Element %d :' %(i+1))
data.append(x)
print('Original
Array :')
print(data)
print('Intermediate
s :')
for i in range(1,n):
temp=int(data[i])
j=i-1
while
temp<int(data[j]) and j>=0:
data[j+1]=data[j]
j=j-1
data[j+1]=temp
print(data)
print('Sorted
Array is:')
print(data)
def
mergeSort(alist):
print("Splitting
",alist)
if
len(alist)>1:
mid = len(alist)//2
lefthalf
= alist[:mid]
righthalf
= alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i
< len(lefthalf) and j < len(righthalf):
if
int(lefthalf[i]) < int(righthalf[j]):
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i
< len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j
< len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging
",alist)
data = []
print('Merge
Sort :')
n =
int(raw_input('Enter Number of Elements in the Array: '))
for i in
range(0, n):
x =
raw_input('Enter the Element %d :' %(i+1))
data.append(x)
print('Original
Array :')
print(data)
print('Intermediate
s :')
mergeSort(data)
print('Sorted
Array is:')
print(data)
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:
**
***
******
*****
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.