Get Second Largest Number in Python List Using Bubble Sort Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Finding the second-largest number in a list is a common programming task that involves sorting the list in ascending order. Bubble sort is a simple sorting algorithm that can be used for this purpose. In this article, we will explore the logic behind finding the second largest number using bubble sort and provide a Python program to implement it. Main LogicThe bubble sort algorithm works by repeatedly swapping adjacent elements if they are in the wrong order.In each pass, the largest unsorted element "bubbles up" to its correct position at the end of the list.After the first pass, the largest element is in the last position.After the second pass, the second-largest element is in the second-to-last position, and so on.Bubble Sort Algorithm:Identifying the Second Largest:To find the second-largest number, we perform the bubble sort and keep track of the largest and second-largest elements during each pass.Mathematical Representation:Let n be the length of the list.After the first pass, the largest element is at index n-1.After the second pass, the second-largest element is at index n-2.Find The Second Largest Number In A List Using Bubble SortExample : The bubble_sort function implements the Bubble Sort algorithm to sort a given list in ascending order.The outer loop runs n times (where n is the length of the list), and the inner loop iterates through the unsorted portion of the list, swapping adjacent elements if they are in the wrong order.The find_second_largest function uses the bubble_sort function to sort the list and then returns the second-to-last element, which is the second-largest number.The example usage demonstrates finding the second largest number in the list [12, 34, 7, 23, 32] and prints the result using the print statement. Python3 def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] def find_second_largest(arr): bubble_sort(arr) return arr[-2] # Example Usage: numbers = [12, 34, 7, 23, 32] second_largest = find_second_largest(numbers) print(f"The second largest number is: {second_largest}") OutputThe second largest number is: 32ConclusionIn this conclusion ,as we discussed the logic behind finding the second largest number in a list using the bubble sort algorithm. The key idea is to perform a bubble sort and keep track of the largest and second-largest elements during each pass. The provided Python program demonstrates the implementation of this logic, showcasing how bubble sort can be used to efficiently find the second-largest number in a list. Comment More infoAdvertise with us Next Article Get Second Largest Number in Python List Using Bubble Sort S sameerkhan6359 Follow Improve Article Tags : Python Data Structures BubbleSort Practice Tags : Data Structurespython Similar Reads Largest, Smallest, Second Largest, Second Smallest in a List-Python We are given a list of numbers and our task is to find the largest, smallest, second largest and second smallest elements in the list. For example, if the list is [4, 1, 7, 3, 9], the largest is 9, the smallest is 1, the second largest is 7 and the second smallest is 3. Let's explore different ways 4 min read Python - Sort list of list by specified index When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py 3 min read Tag sort or Bucket sort or Bin sort in Python Tag sort, also known as Bucket sort or Bin sort, is a non-comparison based sorting algorithm that distributes elements of an array into a number of "buckets", and then sorts each bucket individually. Tag sort or Bucket sort or Bin sort Algorithm:Determine Range:Find the maximum and minimum values in 2 min read How to get the n-largest values of an array using NumPy? Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, ax 2 min read Python program to Sort a list according to the second element of sublist Sorting a list by the second element of each sublist means rearranging the sublists based on the value at index 1. For example, given the list [[1, 3], [2, 1], [4, 2], [3, 5]], sorting by the second element results in [[2, 1], [4, 2], [1, 3], [3, 5]], where the second elements (1, 2, 3, 5) are in as 3 min read Like