1.Another well-known sorting algorithm is thebubble sort.It is based on the process of repeatedly comparing two adjacent names and interchanging them if they are not in the correct order relative to each other. Let us suppose that the list in question hasnentries. The bubble sort would begin by comparing (and possibly interchanging) the entries in positionsnandn – 1.Then, it would consider the entries in positionsn – 1and
n – 2,and continue moving forward in the list until the first and second entries in the list had been compared (and possibly interchanged). Observe that this pass through the list will pull the smallest entry to the front of the list. Likewise, another such pass will ensure that the next to the smallest entry will be pulled to the second position in the list. Thus, by making a total ofn – 1passes through the list, the entire list will be sorted. (If one watches the algorithm at work, one sees the small entries bubble to the top of the list—an observation from which the algorithm gets its name.) Use our pseudocode to express a function similar to that in for sorting a list using the bubble sort algorithm.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here