Algorithm for Bubble Sort
• The element of an Array list[N] are to be sorted into an ascending order.
• Variable flag is being used to indicate the occurrence of an exchange during a pass over the list.
• Temp is a variable used to interchange the pair of elements not in order in the unsorted part of the list.
Steps of Algorithm for Bubble Sort
1. Flag= false, i=0; 2. While(i<N and(flag= false) repeat steps 3 to 5. 3. flag= true; i=i+1 4. for(j=1) to N-1 repeat steps 5 5. if (LIST[j]>List[J+1]) then { Temp=List[j]; List[j]=List[j+1] List[j+1]=Temp; flag=false; } { end of j loop } { end of while loop } 6. End.
Explanation:
1• Two loops the outer while loop and inner j loop. 2• j loop makes the pass on the array list i.e 1 to N-1, for any exchanges to be made and if yes, set flag to false. 3• Outer loop keep a check on termination of the sorting process with the help of two variables i and flag. 4• Where I conveys the end of all the passes and flag indicates whether to proceed with the rest of the pass or not.
19- Array- Bubble Sort Part-2- DataStructure
CLICK HERE TO Download This PDF NOTES