# Python Program to Find the Second Largest Number in a List using Bubble Sort

«
»

This is a Python Program to find the second largest number in a list using bubble sort.

Problem Description

The program takes a list and finds the second largest number in the list using bubble sort.

Problem Solution

1. Take in the number of elements for the list and store it in a variable.
2. Take in the elements of the list one by one.
3. Then sort the list using bubble sort.
4. Print the second last element of the sorted list which is the second-largest number.
5. Exit.

Program/Source Code

Here is the source code of the Python Program to find the second largest number in a list using bubble sort. The program output is also shown below.

```a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
for i in range(0,len(a)):
for j in range(0,len(a)-i-1):
if(a[j]>a[j+1]):
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
print('Second largest number is:',a[n-2])```
Program Explanation

1. User must enter the number of elements for the list and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a list.
3. Then bubble sort algorithm may be used to sort the elements of the list.
4. The second last element of the sorted list is then printed which is basically the second largest element in the list.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
```
Case 1:
Enter number of elements:4
Enter element:56
Enter element:43
Enter element:78
Enter element:12
('Second largest number is:', 56)

Case 2:
Enter number of elements:6
Enter element:23
Enter element:45
Enter element:94
Enter element:10
Enter element:34
Enter element:95
('Second largest number is:', 94)```

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions. 