![]() ![]() We repeat this process N number of times so as to sort all array elements. While doing so we place the largest element at the last position of the array. ![]() Again, if the one on the left is larger, we swap them else do nothing. Then we move over one position and compare the elements in positions 1 and 2. ![]() If the 1 st element is larger, we don't do anything. If the element positioned at 0 th index is larger, we swap them. Bubble sort starts from the left end of the array and compare two elements in position 0 and 1. While sorting the array elements, bubble sort at one time can see only two adjacent elements of the array. Let's assume that we have an array of length N having randomly ordered elements indexed from 0 to N-1, and we want to sort it in ascending order. Writing Java code for bubble sort is a trivial task. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |