How do I perform a bubble sort in Python [closed]

2024/7/8 7:33:10

Question: You have been given an array A of size N . you need to sort this array non-decreasing order using bubble sort. However, you do not need to print the sorted array . You just need to print the number of swaps required to sort this array using bubble sort

Input Format

The first line consists of a single integer N denoting size of the array. The next line contains N space separated integers denoting the elements of the array.

Output Format Print the required answer in a single line

Constrains 1 <= N <= 100 1 <= a[i] <= 100

My Solution which returns nothing

def bubbleSort(arr, arrSize):temp = 0swap = 0for i in range(arrSize):for e in range(arrSize-i-1):if arr[e] < arr[e + 1]:temp = arr[e + 1]arr[e] = tempswap += 1return swap
Answer

Problem in your code is you are returning swap variable it's not array. And your swapping of elements is incomplete. In python you can swap two elements easily using this notation a, b = b, a.

And one more thing you don't need to return the array changes will automatically reflect in original array.

try this:

def bubbleSort(arr, n):swap = 0for i in range(n - 1):for j in range(n - i - 1):if arr[j] > arr[j + 1]:arr[j], arr[j + 1] = arr[j + 1], arr[j]swap += 1return swapa = [5, 2, 3, 1, 4]print('swaps =', bubbleSort(a, 5))
print(a)

Output:

swaps = 6
[1, 2, 3, 4, 5]
https://en.xdnf.cn/q/120613.html

Related Q&A

check if number is between row of numpy array

Want to check if value is between the row of array. here the value 347 is in between the 1st row of aa but not second , so how to check for it ? aa= np.array([[348 ,345],[460 , 459 ]])value = 347prin…

Print a word diagonally? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Click a button using Selenium and Python

I have the following code: <a class="sectionname" href="#" onclick="expandAll();return false;">Expand all</a> When I click on expand all, the whole page loads…

how to do Json format [duplicate]

This question already has answers here:How to store ner result in json/ database(2 answers)Closed 8 years ago.(S(PERSON Rami/NNP Eid/NNP)is/VBZstudying/VBGat/IN(ORGANIZATION Stony/NNP Brook/NNP Univers…

python numpy arange dtpye? why converting to integer was zero

x = np.arange(0.3, 12.5, 0.6)print(x)[ 0.3 0.9 1.5 2.1 2.7 3.3 3.9 4.5 5.1 5.7 6.3 6.9 7.5 8.1 8.7 9.3 9.9 10.5 11.1 11.7 12.3]x = np.arange(0.3, 12.5, 0.6,int)print…

Where is my syntax error?

Im trying to see where a Python syntax error would be hiding. Both Django and pylint claim a syntax error at custom.py:41. Lines 41-42 read:(reading_threshold =int(request.POST[reading_threshold]))I do…

programming challenge: how does this algorithm (tied to Number Theory) work?

In order to work on my python skills, I am sometimes doing various challenges on the internet (eg on hackerrank). Googling for something else, I found this problem, and the accompanying solution on the…

Running total for list of dict

Have a python list of dict as following:Dict1 = [{date: 1, name: xyz, qty: 100},{date: 1, name: xyz, qty: 200},{date: 1, name: xyz, qty: 300},{date: 1, name: xyz2, qty: 30},{date: 2, name: xyz, qty: 10…

How to convert CSV file to a specific JSON format with nested objects?

I want to populate my json message with data from a CSV file. I want each row to be a "new" json object. I am using Python and will connect the the code to an API once done. Some of the data …

How do I repeat the program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…