ascension of bubble to its appropriate level of weight (see table this method is widely known as the bubblesort table sample of bubblesorting procedure bubblesort(adens _sorts *var ijintegerxitembegin for : to - do for : - to by - do if [ - [jthen : [ - ] [ - : [ ] [ : end end end end bubblesort this algorithm easily lends itself to some improvements the example in table shows that the last three passes have no effect on the order of the items because the items are already sorted an obvious technique for improving this algorithm is to remember whether or not any exchange had taken place during pass last pass without further exchange operations is therefore necessary to determine that the algorithm may be terminated howeverthis improvement may itself be improved by remembering not merely the fact that an exchange took placebut rather the position (indexof the last exchange for exampleit is plain that all pairs of adjacent items below this index are in the desired order subsequent scans may therefore be terminated at this index instead of having to proceed to the predetermined lower limit the careful programmer noticeshowevera peculiar asymmetrya single misplaced bubble in the heavy end of an otherwise sorted array will sift into order in single passbut misplaced item in the light end will sink towards its correct position only one step in each pass for examplethe array is sorted by the improved bubblesort in single passbut the array requires seven passes for sorting this unnatural asymmetry suggests third improvementalternating the direction of consecutive passes we appropriately call the resulting algorithm shakersort its behavior is illustrated in table by applying it to the same eight keys that were used in table procedure shakersortvar jklrintegerxitembegin : : - :rrepeat for : to by - do (adens _sorts *