“Python sortowania bańki” Kod odpowiedzi

Python sortowania bańki

def bubbleSort(lis):
    length = len(lis)
    for i in range(length):
        for j in range(length - i):
            a = lis[j]
            if a != lis[-1]:
                b = lis[j + 1]
                if a > b:
                    lis[j] = b
                    lis[j + 1] = a
    return lis
Comfortable Cat

Jak wykonać sortowanie bańki w Pythonie?

"""
Bubble sort sorts a list by repeatedly
swapping adjacent out-of-order values.
The process continues until the list
becomes sorted. 
"""


def bubble_sort(array):
    isSorted = False
    passes = 0
    length = len(array)
    while not isSorted:
        isSorted = True
        # perform a pass through the array
        # excluding already sorted positions
        for i in range(length-1-passes):
            if array[i] > array[i+1]:
                swap(i, i+1, array)
                # array is not sorted yet
                isSorted = False
        passes += 1
    return array


def swap(i, j, array):
    # Swap values at indexes i and j
    array[i], array[j] = array[j], array[i]


arr = [1, 9, 3, 2]
print(bubble_sort(arr))  # [1, 2, 3, 9]
Wissam

Python sortowania bańki

def bubble(st):
    for i in range(len(st),1,-1):
        for j in range(0,i-1):
            if st[j]>st[j+1]:
                st[j],st[j+1]=st[j+1],st[j]
            else:
                pass
    print(st)
bubble([64, 34, 25, 12, 22, 11, 90] )
Quaint Quail

Python Bubble Sort

def bubble_sort(li_to_sort):
    # Looping from size of array from last index[-1] to index [0]
    for n in range(len(li_to_sort)-1, 0, -1):
        for i in range(n):
            if li_to_sort[i] > li_to_sort[i + 1]:
                # swapping data if the element is less than next element in the array
                li_to_sort[i], li_to_sort[i + 1] = li_to_sort[i + 1], li_to_sort[i]


li = [39, 12, 18, 85, 72, 10, 2, 18]

print("Unsorted list: ", li)
bubble_sort(li)
print("Sorted List: ", li)
Defeated Deer

Python sortowania bańki

s= [1,2,3,4,5,6,7,8,9,10]
for i in range(len(s)-1):
    for j in range(len(s)-1-i):
        if s[j]>s[j+1]:
            s[j],s[j+1] = s[j+1],s[j]
print(s)
Sore Stork

Python sortowania bańki

# Python3 Optimized implementation
# of Bubble sort
 
# An optimized version of Bubble Sort
def bubbleSort(arr):
    n = len(arr)
  
    # Traverse through all array elements
    for i in range(n):
        swapped = False
 
        # Last i elements are already
        #  in place
        for j in range(0, n-i-1):
  
            # traverse the array from 0 to
            # n-i-1. Swap if the element
            # found is greater than the
            # next element
            if arr[j] > arr[j+1] :
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
 
        # IF no two elements were swapped
        # by inner loop, then break
        if swapped == False:
            break
          
# Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
  
bubbleSort(arr)
  
print ("Sorted array :")
for i in range(len(arr)):
    print ("%d" %arr[i],end=" ")
 
# This code is contributed by Shreyanshi Arun
INR

Odpowiedzi podobne do “Python sortowania bańki”

Pytania podobne do “Python sortowania bańki”

Więcej pokrewnych odpowiedzi na “Python sortowania bańki” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu