-2

I tried to write a bubble sort program which doesn't work

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

for example if i enter input no as "10,9,8,7,6,5,4,3,2,1" the sorted array comes out to be "1,10,2,3,4,5,6,7,8,9" for array "639,541,854,45,8" the answer is "854,45,639,45,541"

  • Questions seeking debugging help ("why isn't this code working?") must contain a specific problem or error. – timgeb Jul 08 '14 at 11:53
  • Are you getting an error message? Is the output simply incorrect? Can you show typical input, expected output, and actual output? – Cory Kramer Jul 08 '14 at 11:55
  • the sorting is not proper for eg if i enter input no as "10,9,8,7,6,5,4,3,2,1" the sorted array comes out to be "1,10,2,3,4,5,6,7,8,9" – user3816204 Jul 08 '14 at 11:59
  • @user3816204 Add this additional data to your question (use edit option) – Ari Jul 08 '14 at 12:20

2 Answers2

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

you need to convert your elements into int if there sorting of numbers

sundar nataraj
  • 8,524
  • 2
  • 34
  • 46
0

your program should be like

  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

you should check how 'input' and 'raw_input' works

at the following link

What's the difference between raw_input() and input() in python3.x?

Community
  • 1
  • 1
Anuj
  • 994
  • 11
  • 21
  • 1
    If the OP is using Python 2.x, he should not be using `input` instead of `raw_input`. – chepner Jul 08 '14 at 11:59
  • @chepner the link given by anuj tells that raw_input takes string but then what input function does? – user3816204 Jul 08 '14 at 12:05
  • here you take a string value for range. you need to convert it to int – sundar nataraj Jul 08 '14 at 12:05
  • `input` evaluates the input string as Python code. That functionality was removed altogether from Python 3 (which allowed `raw_input` to be renamed `input`). – chepner Jul 08 '14 at 14:02
  • And the reason it was removed is because it was always a terrible idea; it shouldn't be used in Python 2, either. – Wooble Jul 08 '14 at 14:03