# 10+ Bubble Sort Programs (Solved) in python -practice questions

Share with others

## Q1. Write a program to arrange the following list in ascending order using bubble sort.

Original List = [23, 34, 54, 2, 12, 28]

Output List = [2, 12, 23, 28, 34, 54]

```Show Answer#Bubble Sort Program 1

Ans.

L = [23, 34, 54, 2, 12, 28]

n=len(L)

for i in range(n-1):

for j in range(n-i-1):

if L[j] > L[j+1]:

L[j], L[j+1] = L[j+1], L[j]

print("Sorted list is : ",L)

```

## Q2. Write a program to arrange all alphabets of “csiplearninghub” into descending order using bubble sort.

```Show Answer#Bubble Sort Program 2

Ans.

L=list("csiplearninghub")

n=len(L)

for i in range(n-1):

for j in range(n-i-1):

if L[j] < L[j+1]:

L[j], L[j+1] = L[j+1], L[j]

print("Sorted list is : ",L)

OUTPUT is :

['u', 's', 'r', 'p', 'n', 'n', 'l', 'i', 'i', 'h', 'g', 'e', 'c', 'b', 'a'] # all alphabets in descending order```

## Q3. Write a program to accept five names from user and arrange in increasing order using bubble sort.

```Show Answer#Bubble Sort Program 3

Ans.

L=[ ]

for i in range(5):

nm = input("Enter name")

L.append(nm)

n=len(L)

for i in range(n-1):

for j in range(n-i-1):

if L[j] > L[j+1]:

L[j], L[j+1] = L[j+1], L[j]

print("Sorted list is : ",L)```

## Q4. Write a program to accept numbers from the user till the user enter ZERO and store them in a list. After that arrange them in increasing order using bubble sort.

```Show Answer#Bubble Sort Program 4

Ans.

num = 1

L=[ ]

while num!=0:

num=int(input("Enter numbers"))

if num!=0:

L.append(num)

n=len(L)

for i in range(n-1):

for j in range(n-i-1):

if L[j] > L[j+1]:

L[j], L[j+1] = L[j+1], L[j]

print("Sorted list is : ",L)```

## Q5. After how many passes the given list will be sorted in increasing order using bubble sort? L = [23, 34, 54, 2, 12, 28]

`Show Answer Ans. After Five passes. `

## Q6. Write the status of L = [56, 34, 23, 67, 98, 85] after first and second pass in bubble sort for arranging in increasing order.

```Show Answer
Ans. Status is as follows

After first pass L = [34, 23, 56, 67, 85, 98]

After second pass L = [23, 34, 56, 67, 85, 98]
```

## Q7. Write the status of L = [60, 70, 40, 30, 50, 10] after first and second pass in bubble sort for arranging in decreasing order.

```Show Answer
Ans. Status is as follows

After First Pass L = [70, 60, 40, 50, 30, 10]

After Second Pass L = [70, 60, 50, 40, 30, 10]  ```

## Q8. Show the status of L = [201, 301, 101, 901, 601, 401] after first and second pass of arranging elements in ascending order using bubble sort.

```Show Answer
Ans. Status is as follows

After First Pass L = [201, 101, 301, 601, 401, 901]

After Second Pass  L = [101, 201, 301, 401, 601, 901]  ```

## Q9. Write the status after all the passes when given List L = [22, 25, 65, 35, 81, 44] is arranged in ascending order using bubble sort.

```Show Answer

Ans. Status are as follows

After first pass : L = [22, 25, 35, 65, 44, 81]

After Second Pass : L = [22, 25, 35, 44, 65, 81]

After second pass the list is sorted in ascending order

```

## Q10. How many times the outer loop will execute in bubble sort for given list. L [ 25, 12, 56, 34, 23, 98, 29, 4]

`Show Answer Ans. Outer Loop will execute 7 times `

## Q11. Show the status of L = [‘a’, ‘A’, ‘b’ , ‘B’, ‘c’, ‘C’, ‘d’, ‘D’] after first and second pass of arranging elements in ascending order using bubble sort.

```Ans. Status are as follows

After First Pass : L = ['A', 'a', 'B',  'b', 'C', 'c', 'D', 'd']

After Second Pass : L = ['A', 'B', 'a', 'C', 'b', 'D', 'c', 'd']```