Skip to main content
Skip to content

Lists & Objects · Intermediate

Bubble Sort in Hindi Programming

Sort a list by hand with nested loops, then compare with chhanto.

The program

Edit anything and press Run. The output below was produced by running this exact code.

Expected output

Press Run to try it

Pehle: [29, 4, 71, 15, 8, 42]Baad mein: [4, 8, 15, 29, 42, 71]chhanto() se: [4, 8, 15, 29, 42, 71]

How it works

Bubble sort walks through the list comparing neighbours and swaps any pair that is in the wrong order. After the first pass the largest number has "bubbled" to the end, so each later pass can stop one place earlier — that is the n - 1 - pass in the inner loop.

Swapping needs a third variable: copy one value into temp, overwrite it, then put temp into the other position. The list is changed in place.

The last line sorts the same numbers with chhanto, the built-in that returns a sorted copy. In real programs use the built-in; write bubble sort once to understand what sorting involves.