Skip to main content
Code Review

Return to Question

added 65 characters in body; edited tags
Source Link
RubberDuck
  • 31.2k
  • 6
  • 74
  • 176

In interrupted bubble sort, we need to stop the sorting based on the iteration count. This seems pretty straightforward to me. I have this code, which seems to work fine in Python, but it isn't fast enough for larger input sets. How can I make this faster?

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is", l, len(l)
print "count is ", icount
ic = 0
while ic < icount:
 i = 0
 while i < len(l) - 2:
 if l[i + 1] < l[i]:
 tup = l[i], l[i + 1]
 l[i + 1], l[i] = tup
 i += 1
 ic += 1
print ' '.join(str(i) for i in l)

It is not fast enough for larger input sets. How can I make this faster?

In interrupted bubble sort, we need to stop the sorting based on the iteration count. This seems pretty straightforward to me. I have this code, which seems to work fine in Python.

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is", l, len(l)
print "count is ", icount
ic = 0
while ic < icount:
 i = 0
 while i < len(l) - 2:
 if l[i + 1] < l[i]:
 tup = l[i], l[i + 1]
 l[i + 1], l[i] = tup
 i += 1
 ic += 1
print ' '.join(str(i) for i in l)

It is not fast enough for larger input sets. How can I make this faster?

In interrupted bubble sort, we need to stop the sorting based on the iteration count. This seems pretty straightforward to me. I have this code, which seems to work fine, but it isn't fast enough for larger input sets. How can I make this faster?

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is", l, len(l)
print "count is ", icount
ic = 0
while ic < icount:
 i = 0
 while i < len(l) - 2:
 if l[i + 1] < l[i]:
 tup = l[i], l[i + 1]
 l[i + 1], l[i] = tup
 i += 1
 ic += 1
print ' '.join(str(i) for i in l)

It is not fast enough for larger input sets. How can I make this faster?

Post Reopened by Community Bot, Phrancis, RubberDuck, rolfl
edited code . made it prettier.
Source Link
Rahul
  • 263
  • 1
  • 7

In interrupted bubble sort, we need to stop the sorting based on the iteration count. This seems pretty straightforward to me. I have this code, which seems to work fine in Python.

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is", l, len(l)
print "count is ", icount
ic = 0
while ic < icount:
 print "ic is ", ic 
 i = 0
 while i < (len(l) - 2):
 if l[i+1]l[i + 1] < l[i]: l[i+1],l[i] = l[i],l[i+1]
 i +=tup 1= l[i], l[i + 1]
 print "inc i " l[i + 1], il[i] = tup
 ic i += 1
 ic += 1
print "ic' after'.join(str(i) incfor is",i ic
printin l)

It is not fast enough for larger input sets. How can I make this faster?

In interrupted bubble sort, we need to stop the sorting based on the iteration count. This seems pretty straightforward to me. I have this code, which seems to work fine in Python.

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is", l, len(l)
print "count is ", icount
ic = 0
while ic < icount:
 print "ic is ", ic 
 i = 0
 while i < (len(l) - 2):
 if l[i+1] < l[i]: l[i+1],l[i] = l[i],l[i+1]
 i += 1
 print "inc i ", i
 ic += 1
 print "ic after inc is", ic
print l

It is not fast enough for larger input sets. How can I make this faster?

In interrupted bubble sort, we need to stop the sorting based on the iteration count. This seems pretty straightforward to me. I have this code, which seems to work fine in Python.

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is", l, len(l)
print "count is ", icount
ic = 0
while ic < icount:
 i = 0
 while i < len(l) - 2:
 if l[i + 1] < l[i]: tup = l[i], l[i + 1]
  l[i + 1], l[i] = tup
 i += 1
 ic += 1
print ' '.join(str(i) for i in l)

It is not fast enough for larger input sets. How can I make this faster?

deleted 28 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

In interrupted bubble sort, we need to stop the sorting based on the iteration count. SeemsThis seems pretty straightforward to me. I have this code, which seems to workswork fine in pythonPython.

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is" , l , len(l)
print "count is ", icount
ic = 0
while ic < icount:
 print "ic is ", ic 
 i = 0
 while i < (len(l) - 2):
 if l[i+1] < l[i]: l[i+1],l[i] = l[i],l[i+1]
 i += 1
 print "inc i ", i
 ic += 1 
 print "ic after inc is", ic
print l

It is not fast enough for larger input sets. How can I am wondering on how to make this faster. Any suggestions?

In interrupted bubble sort, we need to stop the sorting based on the iteration count. Seems pretty straightforward to me. I have this code, which seems to works fine in python.

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is" , l , len(l)
print "count is ", icount
ic = 0
while ic < icount:
 print "ic is ", ic 
 i = 0
 while i < (len(l) - 2):
 if l[i+1] < l[i]: l[i+1],l[i] = l[i],l[i+1]
 i += 1
 print "inc i ", i
 ic += 1 
 print "ic after inc is", ic
print l

It is not fast enough for larger input sets. I am wondering on how to make this faster. Any suggestions?

In interrupted bubble sort, we need to stop the sorting based on the iteration count. This seems pretty straightforward to me. I have this code, which seems to work fine in Python.

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is" , l , len(l)
print "count is ", icount
ic = 0
while ic < icount:
 print "ic is ", ic 
 i = 0
 while i < (len(l) - 2):
 if l[i+1] < l[i]: l[i+1],l[i] = l[i],l[i+1]
 i += 1
 print "inc i ", i
 ic += 1 
 print "ic after inc is", ic
print l

It is not fast enough for larger input sets. How can I make this faster?

deleted 213 characters in body
Source Link
user34073
user34073
Loading
added 183 characters in body
Source Link
Rahul
  • 263
  • 1
  • 7
Loading
Post Closed as "Not suitable for this site" by Jamal
Source Link
Rahul
  • 263
  • 1
  • 7
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /