Skip to main content

Bubble Sort

Bubble Sort

Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items.

How bubble sort works ?
9    25    18    29    2
Bubble sort starts with very first two elements, comparing them to check which one is greater.
9    25    18    29    2
In this case, value 25 is greater than 9, so it is already in sorted locations. Next, we compare 25 with 18.
9    25    18    29    2
We find that 18 is lower than 25 and these two values must be swapped.
9    18    25    29    2
Next we compare 25 and 29. We find that both are in already true positions.
9    18    25    29    2
Then we move to the next two values, 29 and 2
We know then that 10 is smaller 35. So, we swapped.
9    18    25    2    29
After the last iteration, it should look like this
2    9    18    25    29

Algorithm

begin BubbleSort(list)
for all elements of list
      if list[i] > list[i+1]
         swap(list[i], list[i+1])
      end if
   end for
    return list
   end BubbleSort

Pseudocode

procedure bubbleSort( list : array of items )
loop = list.count;
   for i = 0 to loop-1 do:
      swapped = false
  for j = 0 to loop-1 do:  /* compare the adjacent elements */  
         if list[j] > list[j+1] then /* swap them */
            swap( list[j], list[j+1] )                       
            swapped = true
         end if
         end for
         if(not swapped) then
         break
      end if
       end for
  

end procedure return list

Comments

Popular posts from this blog

What is pinterest?

What is pinterest? Pinterest is a social network that allows users to visually share, and discover new interests by posting (known as 'pinning' on Pinterest) images or videos to their own boards. you'll always find Pins to make inspiration. When you discover Pins you like, save them to boards to keep your ideas and easy to find. Pins When everyone find things that they love,they pin it so,Pins are ideas that people on Pinterest find and save from around the internet. Each Pin links back to the website it was saved from. If you click the Pin, you can know the information of its. Home feed Pinterest remember category,prople,topics and boards that you often find or save it to pins and show them to your home feed every time you go through pinterest. Boards Your saved pins ,it saved on your boards and you can manage everything you want on your boards to share more ideas to others. Profile Your profile will be a mirror that reflect who you are to other p...

Search engine

          If you get many pictures on your boards,Pinterest will combine it together with keywords and category that makes from user all over the world to be a function in pinterest that’s called ‘Pins’.           Search engine works by combine pins with the category all your own pictures to make everytime .That user get into the app, Pins shows their last favourite pictures that they used to pins before. Let’s understand the type of searching! Searching Algorithms           Search algorithms are very important for many programs. Searchers looking for database, such as looking up your score record in the database. Although programmers can choose from search types, they select the algorithm that best matches the size and structure of the database.