![]() For the second pass, the second largest element is placed at the second last position in the list and so on. Thus as shown in the illustration above, at the end of the first pass, if the array is to be sorted in ascending order, the largest element is placed at the end of the list. The above illustration can be summarized in a tabular form as shown below: We take an array of size 5 and illustrate the bubble sort algorithm. Let us now illustrate this technique by using a detailed illustration. The above given is the pseudo-code for bubble sort technique. Pseudocode Procedure bubble_sort (array, N) At the end of each iteration of the outer loop, the heaviest element bubbles up at the end. In the inner loop body, we compare each of the adjacent elements and swap them if they are not in order. In the first loop, we start from the 0 th element and in the next loop, we start from an adjacent element. Here is a pseudo-code for bubble sort algorithm, where we traverse the list using two iterative loops. ![]() We have given a general algorithm of bubble sort technique below. In other words, the largest element in the list bubbles up. Thus at the end of each iteration, the heaviest element is placed at its proper place in the list. ![]() Using the bubble sort technique, sorting is done in passes or iteration. Complexity Analysis Of The Bubble Sort Algorithm. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |