ReCom.org
Portal Page Forum Wiki Social Groups Scholarship Holders Infobase Site Map About
Go Back   ReCom.org > Forum > Social Group Forums > Coderz

Coderz Click here to visit this social group

Bubble Sort in various programming languages

Reply
 
Thread Tools
luke Male
KittyLover
Administrator
 
luke's Avatar
 
Join Date: Nov 2003
Posts: 1,878
  #1 Old 01-12-2008 Smile Bubble Sort in various programming languages

Just for fun, let's share codes for Bubble Sort algorithm in any programming languages here.

PHP
Code:
<?php

$data = array(
'Perlis', 'Kedah', 'Pulau Pinang',
'Perak', 'Selangor', 'Wilayah Persekutuan',
'Negeri Sembilan', 'Melaka', 'Johor',
'Pahang', 'Terengganu', 'Kelantan',
'Sarawak', 'Sabah');

for ($i = 0; $i < count($data); $i++) {
  for ($j = count($data) - 1; $j > $i; $j--) {
    if ($data[$j] < $data[$j - 1]) {
      $t = $data[$j];
      $data[$j] = $data[$j - 1];
      $data[$j - 1] = $t;
    }
  }
}

print '<pre>' . print_r($data, true) . '</pre>';
?>
__________________
luke is offline   Reply With Quote
chongkeat Male
Member
 
chongkeat's Avatar
 
Join Date: Aug 2008
Posts: 497
  #2 Old 02-12-2008 Default Re: Bubble Sort in various programming languages

I'm not familiar with PHP, but from what I see, you:

---------
Create an array.

do for number of data in array (variable A)
{
do for number of data - 1, while it is larger than A (variable B)
{
switch the data in positions array A & B if... (OK, now I'm confused.)
--------

A bubble sort is kinda like stepping through a list and sorting each one through? Like this?
156423
154236
142356
123456


And I don't really know this part. I know we shouldn't be spoonfed, but can you please explain with comments or something?
Quote:
if ($data[$j] < $data[$j - 1]) {
$t = $data[$j];
$data[$j] = $data[$j - 1];
$data[$j - 1] = $t;
You're sorting according to the word length, right? So, I guess $data[] is something like a letter count?

*Looking up Bubble Sort*
chongkeat is offline   Reply With Quote
luke Male
KittyLover
Administrator
 
luke's Avatar
 
Join Date: Nov 2003
Posts: 1,878
  #3 Old 02-12-2008 Default Re: Bubble Sort in various programming languages

The sorting algorithm is called "Bubble Sort" because smaller (or larger) elements move towards the end of the sequence, just like how bubble rises from the bottom of a lake to the surface.

let's say i have 2,4,3,5,1.
first step, i compare 1 to 5, 1 is less than 5 so we swap: 2,4,3,1,5
then compare 1 to 3, 1 is less than 3 so swap again: 2,4,1,3,5
then 1 to 4, again, swap: 2,1,4,3,5
then 1 to 2, swap: 1,2,4,3,5

see how 1 'bubbles' towards the left since it's smaller than all other elements?
now 1 is confirmed as the smallest element in the sequence.

let's continue,
compare 5 to 3, no swap: 1 --- 2,4,3,5
3 to 4, swap: 1 --- 2,3,4,5
3 to 2, no swap: 1 --- 2,3,4,5

now 2 is confirmed as smallest from what's left thus: 1,2 --- 3,4,5

continue until all elements are confirmed as ordered.

implementing it into the code, the outer loop for ($i ... ) handles the ordered vs unordered elements while the inner loop for ($j ... ) handles the actual comparison and swapping.
__________________
luke is offline   Reply With Quote
Reply

Bookmarks

Tags
algorithm, examples, programming

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Most spoken languages in the world Sherman Debates 48 08-04-2008 11:14 AM
Languages of south east asia StrikeFighter Window to the World 2 18-07-2007 06:19 PM
ECU Programming filarnski Programming and Web Development 3 15-11-2005 10:58 PM
Find Penpals to practice foreign languages Valentin1402 Education 0 04-06-2005 03:14 AM
Techniques In Learning Foreign Languages chenchow Education 9 27-09-2004 01:41 AM


All times are GMT +8. The time now is 10:36 PM.


Powered by vBulletin® Version 3.7.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

ReCom stands for Reborn Community. It has no affiliation with other organizations that may share the same name. The views expressed in this website solely represent the authors point of view and do not necessarily reflect the views of ReCom Anchors and other ReCom users.


 

Page generated in 0.07972 seconds with 16 queries