The number of required passes in bubble sort
In bubble sort, the minimum number of passes are required is 1 or the maximum number of passes are required is n-1. The pass is used in bubble sorts are sorting the elements repeated until the element is sorted.
If the list of the element is already sorted the sort can stop.
If you have a list of unsorted elements and you want to sort the list of these elements then you can use the passes algorithm.
the passes algorithm compares the first two numbers if the first number is greater than the second number then swap these numbers but if the first number is less than the second number then you can compare the next two numbers because the number is already sorted.
Let’s See an example of it how it works :
you have a list of elements that is [6,2,3,4,8]
Step 1: compare the first two-element that is 6>2. then swap these numbers and update the list of element id [2,6,3,4,8].
Step2: compare the next two numbers. 6>3. then swap these numbers and updated list of elements is [2,3,6,4,8]
Step3: compare the next two elements is 6>4 that is true so swap these numbers and updated list of elements is [2,3,4,6,8]
Step4: and this is the last step to compare the next element. 6>8 that is false so we can’t swap these numbers because it is already sorted and the final sorting is [2,3,4,6,8]
In this step, you can see how many are required in bubble sort. I hope it helps you.
Comments
Post a Comment