Jump to content

PHP Bubble Sort Algorithm


dakotaboy99

Recommended Posts

The class I’m in went from basic PHP to Bubble Sort, the teacher gave us a take home exam to define an algorithm for a bubble sort, I have no idea how to begin this, even after a few hours trying to teach myself from online to the books I have, and any help would be appreciated. Thanks

Link to comment
Share on other sites

maybe if you could elaborate on what you are stuck on, or provide some code that you've tried, that might help us help you. Just saying you don't get it is kinda vague and doesn't really help us identify what you need help with.

Link to comment
Share on other sites

My instructor is very vague also, no one in class understands he teaching method, that why we all were shocked for him to give us a take home exam on something we never covered. He wants us to sort an array with 15 elements in descending order, 1,3,2,4,5,6,7,8,9,10,11,12,13,14,15

 

He gave us this to turn into the algorithm...

 

function bubbleSort ($items) { $size = count($items); for ($i=0; $i<$size; $i++) { for ($j=0; $j<$size-1-$i; $j++) { if ($items[$j+1] < $items[$j]) { arraySwap($items, $j, $j+1); } } } return $items;}function arraySwap (&$arr, $index1, $index2) { list($arr[$index1], $arr[$index2]) = array($arr[$index2],$arr[$index1]); }

Link to comment
Share on other sites

Never heard of bubble sort until i looked it up.

 

http://en.wikipedia.org/wiki/Bubble_sort

 

I immediately recognized the pattern for a moving average (seasonality in economics). That's what I was taught to call it in the 70s.

 

tomato / tomaato.

 

You have the right approach davej. He'll might show us his code eventually.

Edited by niche
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...