"""
This is a code example of
sorting an array using Bubble Sort
in Python Programming.
Author: Code2care.org
Date: 03-Apr-2022
Version: 1.0
"""
# The provided Input Array to sort using bubble sort
arr = [30, 20, 10, -20, 40, -40, 70, 60, 90, 80, -50]
def bubble_sort(input_arr):
for i in range(len(arr)-1):
for j in range(0, len(arr)-i-1):
if arr[j] > arr[j + 1] :
arr[j], arr[j + 1] = arr[j + 1], arr[j]
print ("Input Unsorted Array: %d", arr)
bubble_sort(arr)
print ("Bubble Sorted Array : %d", arr)
Output:
Input Unsorted Array: %d [30, 20, 10, -20, 40, -40, 70, 60, 90, 80, -50]
Bubble Sorted Array : %d [-50, -40, -20, 10, 20, 30, 40, 60, 70, 80, 90]
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- Update SharePoint Online List Item using REST API, HTML, Spfx, Postman - SharePoint
- Parsing a YAML file in Python Example - Python
- Show Hide SharePoint column in List Library form with the conditional formula - SharePoint
- How to enable Auto-Save Files in VS Code - HowTos
- Java 8: Convert Stream to Array - Java
- How to Export a man page using Mac Terminal to a file - MacOS
- Notepad++ Convert text from lower to upper case - NotepadPlusPlus
- java: cannot infer type for local variable, cannot use var on variable without initializer - Java