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

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

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

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]

ganapathis510 + 0 comments # include

int main() { int a[1000000],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; }

khuatgia + 4 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; }`

4cankursharma + 1 comment Do you know why this is happening? I keep getting the same errors even though my output is same

fabby_h89 + 0 comments I had the same issue until I change the print lines to:

print("Array is sorted in %d swaps." % count)

print("First Element: %d" % a[0])

print("Last Element: %d" % a[-1])

This was for Python3.

fabby_h89 + 0 comments I had the same issue until I change the print lines to:

`print("Array is sorted in %d swaps." % count) print("First Element: %d" % a[0]) print("Last Element: %d" % a[-1])`

This was for Python3 but I think you can try something similar.

lucasjavierespi1 + 0 comments do it like this

System.out.print("Array is sorted in " + counter + " swaps.\n");

System.out.print("First Element: " + a[0]+"\n");

System.out.print("Last Element: " + a[n - 1]);

souvik839 + 0 comments yes, me too...

Sort 183 Discussions, By:

Please Login in order to post a comment