Python 7 Levels, L4, Unit 33, Bubble Sort and Selection Sort

Powered By EmbedPress

Exercise 1:

a = [45,12,53,2,35]
for j in range(len(a)-1):
	if a[j] > a[j+1]:
		t = a[j]
		a[j] = a[j+1]
		a[j+1] = t
for j in a:
	print(j)

Exercise 2:

a = [45,12,53,2,35]
for i in range(len(a)-1):
	for j in range(len(a)-1):
		if a[j] > a[j+1]:
			t = a[j]
			a[j] = a[j+1]
			a[j+1] = t
for j in a:
	print(j)

Exercise 3:

a = [45,12,53,2,35]
for j in range(len(a)-1):
	if a[0] > a[j+1]:
		t = a[0]
		a[0] = a[j+1]
		a[j+1] = t
for j in a:
	print(j)

Exercise 4:

a = [45,12,53,2,35]
for i in range(len(a)-1):
	for j in range(i+1,len(a)):
		if a[i] > a[j]:
			t = a[i]
			a[i] = a[j]
			a[j] = t
for j in a:
	print(j)

Homework:

a = [45,12,53,2,35]
for j in range(len(a)-1):
	if a[j] > a[j+1]:
		t = a[j]
		a[j] = a[j+1]
		a[j+1] = t
for j in a:
	print(j)

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts