Posts

Showing posts with the label Sorting

Sorting and Searching Programs in Python

SORTING Bubble Sort: def bubble_sort(nums):     # We set swapped to True so the loop looks runs at least once     swapped = True     while swapped:         swapped = False         for i in range(len(nums) - 1):             if nums[i] > nums[i + 1]:                 # Swap the elements                 nums[i], nums[i + 1] = nums[i + 1], nums[i]                 # Set the flag to True so we'll loop again                 swapped = True # Verify it works # nums = [5, 2, 1, 8, 4] nums = input('Enter the list of numbers: ').split() nums = [int(x) for x in nums] bubble_sort(nums) print("List Sorted using Bubble Sort") print(nums) Output: Enter the list of numbers: 5 23 3 65 74 55 List Sorted using B...