From fee86fa371b5e00932d34d81027bc3e4b6ffa992 Mon Sep 17 00:00:00 2001 From: Harsh Aggarwal Date: Wed, 19 Dec 2018 12:43:42 +0530 Subject: [PATCH] Changed python code of bubble sort (#24217) Changed python code of bubble sort - instead of using two functions, one function is used and all we need to do is to write the driver code and call the function and it will result in a sorted list(or array) --- .../sorting-algorithms/bubble-sort/index.md | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md b/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md index 06b89eaabd..dcb8eca609 100644 --- a/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md +++ b/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md @@ -149,16 +149,13 @@ func bubbleSort(_ inputArray: [Int]) -> [Int] { ### Example in Python ```py -def bubblesort( A ): - for i in range( len( A ) ): - for k in range( len( A ) - 1, i, -1 ): - if ( A[k] < A[k - 1] ): - swap( A, k, k - 1 ) - -def swap( A, x, y ): - tmp = A[x] - A[x] = A[y] - A[y] = tmp +def bubbleSort(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] + print(arr) ``` ### Example in C