# Sorting: Bubble Sort

# Sorting: Bubble Sort

thugwaffle + 4 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?

blackpanther19 + 0 comments The difficulty has been changed to "Easy" now.

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.

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...

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++; } } }

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

pranavpb_a97 + 0 comments Run and failled 10 times with correct output and MISSING FULLSTOP man!!!!!!!!

alonare + 0 comments What do we have to return? My output is exactly the same but it still is not accepting my solution. I have tried to return all the three values in combinations after printing but still no luck.

klganesh + 1 comment # include

int main() { int a[10000],n,i,j,temp,count=0; scanf("%d",&n); for(i=0;ia[j+1]) { count++; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("Array is sorted in %d swaps.\n",count); printf("First Element: %d\n",a[0]); printf("Last Element: %d\n",a[n-1]); return 0; }

nitishnani84 + 0 comments [deleted]

Sort 192 Discussions, By:

Please Login in order to post a comment