# Sorting: Bubble Sort

# Sorting: Bubble Sort

thugwaffle + 3 comments I think this is a bit too easy to classify as "Medium", especially since you not only give most of the code in the question, but the solution too!

levanlevi + 0 comments absolutely! WHY?..

MobilityWins + 3 comments what language were you using, cuz in C# it wasnt easy for me since there are no built in functions for Swap

levanlevi + 1 comment but swap is fairly easy to implement yrself?..

MobilityWins + 2 comments to be honest, no because I'm kind of brand new to the different sorts. I tried copying the for loops and then changing swap(a[j], a[j + 1]); to a[j] = a[j+1];

`but i dont think that works and im away from the computer right now to test it`

d_koudinov + 0 comments [deleted]l_mohamed_157 + 1 comment i agree with you even if you try to implement the Swap method as follow Swap (ref int a , ref int b) { int temp; temp = b; b = a; a = temp; } the compiler of hacker rank did not recoginze the ref keywork which refers to pass by refernce

levanlevi + 0 comments So difficulty of the problem reduces to the difficulty of swap :)) you don't need "ref" in c# - array keeps references by default

mihir7759 + 0 comments there are no built in function for the swaps in C also

AlexM77 + 0 comments there was no built in swap method, but i was able to add it myself pretty easily. I think its one of those things that once you do it once, its fairly easy to do it next time. you know?

amrkhaledccd + 1 comment This should be classified as easy. The fact it is marked as Medium made me think 100 times before submitting my solution, i thought there is something i didn't understand

adilek + 0 comments Mine passed from 1st attempt. However, I still think there is something I did not understand.

X1011 + 2 comments fun fact: you don't need to actually perform the bubble sort to find the total number of swaps necessary; you can just count the inversions, as in the problem Merge Sort: Counting Inversions. not that it would be easier to code, but it would have lower time complexity.

louis_grimaldi + 1 comment True. Count the inversions, and then iterate once through the array to find the minimum and maximum value (in the end the first and last element as the array is sorted). O(n^2) -> O(n).

siebenschlaefer + 0 comments Can you really count the inversions in O(n)?

dkim331 + 0 comments But isn't the question asking # min swaps when using bubble sort? Sure merge sort would have O(nlogn) worst case and get the MINIMUM number of swaps but I think it's diff from what the question is asking.

MaxNRG + 4 comments Python 3 solution:

n = int(input().strip()) a = list(map(int, input().strip().split(' '))) numSwaps = 0 while True: SwapsFlag = False for i in range(len(a)-1): if a[i] > a[i+1]: a[i], a[i+1] = a[i+1], a[i] numSwaps += 1 SwapsFlag = True if not SwapsFlag: break print('Array is sorted in', numSwaps, 'swaps.') print('First Element:', a[0]) print('Last Element:', a[-1])

mspagon + 1 comment a[i], a[i+1] = a[i+1], a[i]

Could you explain this line please?

EDIT: Answer is here

https://codeandchaos.wordpress.com/2014/11/15/swap-two-objects-in-python/

joshiii + 0 comments swapping of elements in python 3

weldon_thomas + 1 comment Interesting implementation. So best case O(N), worst case still O(N^2)?

randa_el_mrabet1 + 0 comments Yes :)

joshiii + 0 comments how can you apply loop in first two lines

gleventhal + 0 comments I did basically the same algo. I don't understand how some solutions seem to just be doing a single loop without being wrapped in a while True, that wouldn't result in a sorted list for many situations, like if the lowest number is last in the list. Maybe I am missing something.

mattie1 + 0 comments JavaScript E6 solution

var swapCount = 0; for (let i = 0; i < a.length; i++) { for (let j = 0; j < a.length - 1; j++) { if (a[j] > a[j + 1]) { [a[j], a[j + 1]] = [a[j + 1], a[j]]; swapCount++; } } }

kakyoin01 + 0 comments This is an odd problem. It tries to provide a foundation for learning the ins and outs of bubble sort by giving the basic algorithm and asking for metrics around it, but I think it would have been better to supply a general description of the bubble sort algorithm's behavior instead of flat-out give the algorithm in the problem. As it stands now, this feels more like an algorithm highlight exercise rather than an exercise in implementing/learning the algorithm. I guess since this used to be classified as a Medium difficulty problem, there was some problem trying to balance it for Easy difficulty...

alvinpon + 0 comments I can't believe that the level of this question is medium.

suribabubonu399 + 0 comments i didn't find any difficulty while solving this problem!!

khuatgia + 0 comments Hi everyone. I keep getting wrong answers even though my outputs are exactly the same as expected output. Does anyone has the same issue ? Here is my solution:

static void countSwaps(int[] a) {

`int n = a.length; int counter = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; counter++; } } } System.out.println("Array is sorted in " + counter + " swaps."); System.out.println("First element: " + a[0]); System.out.println("Last element: " + a[n - 1]); return; }`

pamu_mca + 1 comment Hi,

I have used this logic.

static void countSwaps(int[] a) { int arrLength = a.length; int temp; int count = 0; for(int i=0;i a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; flag = 1; count++; } } if(flag == 0) { break; } } System.out.println("Array is sorted in "+count+" swaps."); System.out.println("First Element: "+a[0]); System.out.println("Last Element: "+a[arrLength-1]); }

`Here, I am getting count as "0" in hackerrank output. But, when I am testing in my local machine, I am getting actual number of swaps(more than 0).`

Why it is happening like that?

yadav_raj7121 + 1 comment [deleted]yadav_raj7121 + 0 comments def countSwaps(a): i=0 swap=0 for j in a:

for i in range(len(a)-1): if a[i] print("First Element:",a[0]) print("Last Element:",a[n-1]) n = int(input("no. of inputs:")) a = list(map(int, input("enter the list to be sorted:").strip().split())) countSwaps(a)

Sort 170 Discussions, By:

Please Login in order to post a comment