Bubble sort is an algorithm that is use to arrange a list of item ascending or descending then cocktail sort is a develop bubble sort and they have the same uses it can arrange the items ascending or descending. Bubble sort work like this example we have 4,3,2,1 as an item bubble sort work like this the bubble sort algorithm has a what we so called passes and in every passes there a swapping of items to make it sorted ascending or descending. Our example in the arrangement is 4,3,2,1 we have to sort the items as 1,2,3,4 as a ascending arrangement. In the program will see like this:
(4,3,2,1 ),(3,4,2,1) the first two items which is 4 and 3 since 4 is greater than 3 then we swap the element and other element will wait until its turn. Pagkatpos pagpalitin yung una at pangalawang item lilipat naman ung item na ikukumpara which is 4 and 2 the arrangement will like this (3,4,2,1) since 4 is greater than 2 the item will swap then the arrangement will like this (3,2,4,1) then after 4 and 1 will compare and it goes like this (3,2,4,1) since 4 is greater than 1 swap the items then it will like this (3,2,1,4) as you can see its not sorted already if it is not sorted there will be one more pass will make then the passes will always compare going to the left to right passes will be like this and observe the numbers swapping in bold style since not sorted yet our arrangement is(3,2,1,4) s wapping will go like this:
(3,2,1,4) since 3>2 then swap,(2,3,1,4) proceed to the next element to compare.
(2,3,1,4) since 3>1 then swap (2,1,3,4) proceed to the next element to compare.
(2,1,3,4) since 3 is not greater than 4 then no swapping will execute. Not yet sorted Then another pass to sort the items the arrangement is (2,1,3,4) then compare again:
(2,1,3,4) since 2 is greater than 1 then swap( 1,2,3,4 )proceed to the next element to compare.
( 1,2,3,4 ) since two is not greater than 3 no swapping then proceed to the next element to compare. ( 1,2,3,4 ) since 3 is not greater than 4 then no swapping will execute. After this pass and its already sorted there will be a pass to satisfy that the elements are sorted. Titigil lang yung program kapag sa isang pass eh wala ng swapping na magaganap.
In cocktail sort naman has two way of sorting if the bubble sort starting from left to right the cocktail sort will sort and swap left to right and then right to left the movement will done like this.
sort here will move to left to right
(4,3,2,1)since 4>3 swap then It will become (3,4,2,1) then next element
(3,4,2,1)since 4>2 then swap and move like this(3,2,4,1)then next element
(3,2,4,1)since4>1 then swap and becomes(3,2,1,4) after this the sorting is going to the right to left.(in bubble sort the pass will start at the left while cocktail sort will sort start from the right and it goes and while comparing to right to left the condition now will be if less than swap else no swapping.)
(3,2,1,4)since 4 not less than 4 no swapping (3,2,1,4) then
(3,2,1,4) since 1<2 then swap(3,1,2,4) then
(3,1,2,4) since 1<3 then swap(1,3,2,4)then back in comparing left to right and it goes like this
(1,3,2,4) since 1 not greater than 3 no swapping (1,3,2,4)then
(1,3,2,4)since 3> 2 then swap(1,2,3,4)then
(1,2,3,4)since 3 not greater than 4 no swapping(1,2,3,4)again the program will pass once more until theres no swapping that will be done and it is already sorted.
Remember that in while comparing left to right if the element is greater than to the right swap if not no swapping then proceed to the next element and in comparing right to left if element in the right is less than to the left swap then if not no swapping then proceed to the next element to sort =)
According to eho.com
“This algorithm has several advantages. It is simple to write, easy to understand and it only takes a few lines of code. The data is sorted in place so there is little memory overhead and, once sorted, the data is in memory, ready for processing. The major disadvantage is the amount of time it takes to sort. The average time increases almost exponentially as the number of table elements increase. Ten times the number of items takes almost one hundred times as long to sort.”
Sources:
Ehow.com
Advantages & Disadvantages of Bubble Sort | eHow.com http://www.ehow.com/info_8201350_advantages-disadvantages-bubble-sort.html#ixzz1g2WHojTv
Xavier B. Bautista
Walang komento:
Mag-post ng isang Komento