Answers for "bubble sort strings in python"

2

bubble sort in python

def bubble_sort(arr):
    x=-1
    n=len(arr)#length of array 6
    for i in range (0,n):
        for j in range(1,n-i):
            if arr[j-1]>arr[j]:
                arr[j-1],arr[j]=arr[j],arr[j-1]
        if (n-i)<=1:
            break
    return arr
            
if "__main__"==__name__:
    arr=[7,1,2,6,9,3,8,4]
    result=bubble_sort(arr)
    print(result)
Posted by: Guest on July-19-2020
-1

Bubble Sort Algorithm in Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 10 18:05:51 2019

@note: Exchanging sort - Bubble Sort algorithm
@source: http://interactivepython.org/runestone/static/pythonds/SortSearch/TheBubbleSort.html

"""

def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp
Posted by: Guest on August-22-2021

Python Answers by Framework

Browse Popular Code Answers by Language