我试着编写一个不起作用的冒泡程序
n = raw_input("enter no of elements to be added in array")
print 'enter elements for an array to be sorted'
arr = []
for i in range(n):
p=raw_input("enter next element of array\n")
arr.append(p)
for j in range (len(arr)-1):
for k in range(len(arr)-j-1):
if(arr[k]>arr[k+1]):
temp=arr[k]
arr[k]=arr[k+1]
arr[k+1]=temp
print("the sorted array is")
print arr
例如,如果我输入输入号为" 10,9,8,7,6,5,4,3,2,1和#34; 排序后的阵列出现了" 1,10,2,3,4,5,6,7,8,9" 对于阵列" 639,541,854,45,8" 答案是" 854,45,639,45,541"
答案 0 :(得分:1)
n = int(raw_input("enter no of elements to be added in array"))
print 'enter elements for an array to be sorted'
arr = []
for i in range(n):
p=raw_input("enter next element of array\n")
arr.append(int(p))
for j in range (len(arr)-1):
for k in range(j,len(arr),1):
if(arr[j]>arr[k]):
arr[j],arr[k]=arr[k],arr[j]
print arr
如果有数字排序
,您需要将元素转换为int
答案 1 :(得分:0)
你的程序应该像
n = input("enter no of elements to be added in array")
print 'enter elements for an array to be sorted'
arr = []
for i in range(n):
p=input("enter next element of array\n")
arr.append(p)
for j in range (len(arr)-1):
for k in range(len(arr)-j-1):
if(arr[k]>arr[k+1]):
temp=arr[k]
arr[k]=arr[k+1]
arr[k+1]=temp
print("the sorted array is")
print arr
你应该检查'input'和'raw_input'是如何工作的
在以下链接
What's the difference between raw_input() and input() in python3.x?