# 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 + 1 comment 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)?

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.

fabriciovsa + 0 comments My Python 3 solution:

def countSwaps(a): issorted = False swaps = 0 while not issorted: issorted = True for i in range(0, len(a) - 1): if a[i] > a[i + 1]: a[i], a[i + 1] = a[i + 1], a[i] swaps += 1 issorted = False print("Array is sorted in %d swaps." % swaps) print("First Element: %d" % a[0]) print("Last Element: %d" % a[-1])

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

Hariprasath_1_17 + 0 comments # include

using namespace std; int main() { int n,temp,b=0; cin>>n; int a[n]; for(int i=0;i>a[i]; for(int i=0;ia[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; b++; } } cout<<"Array is sorted in "<

`for c++`

balvindersingh21 + 0 comments # include

int main() { int a[2000000],i,j,n,temp,swap=0,c,d; scanf("%d",&n); for(i=0;i

`} for(i=1;i<n;i++) { for(j=0;j<n-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; swap++; } } } c=a[0]; d=a[n-1]; printf("Array is sorted in %d swaps.\n",swap); printf("First Element: %d\n",c); printf("Last Element: %d\n",d);`

return 0; }

Sort 153 Discussions, By:

Please Login in order to post a comment