dakotaboy99 Posted August 1, 2014 Share Posted August 1, 2014 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 More sharing options...
davej Posted August 1, 2014 Share Posted August 1, 2014 Bubble-sort is simple in Php or any other language. Link to comment Share on other sites More sharing options...
dakotaboy99 Posted August 1, 2014 Author Share Posted August 1, 2014 Thats no help, considering my instructor, did not teach us this, basically wanted to see where we stand in the class, just gave us a take home exam and said do this, I dont have issues with the code, just the algorithm. Link to comment Share on other sites More sharing options...
davej Posted August 1, 2014 Share Posted August 1, 2014 Do you understand loops? Do you understand if statements? Do you understand array variables? That is all it is, and the algorithm is explained in detail... http://en.wikipedia.org/wiki/Bubble_sort Link to comment Share on other sites More sharing options...
thescientist Posted August 1, 2014 Share Posted August 1, 2014 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 More sharing options...
dakotaboy99 Posted August 1, 2014 Author Share Posted August 1, 2014 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 More sharing options...
davej Posted August 1, 2014 Share Posted August 1, 2014 Very vague? He essentially gave you the answer except that it may not be exactly 100% correct. All you have to do is test it and fix any bugs. Link to comment Share on other sites More sharing options...
dakotaboy99 Posted August 1, 2014 Author Share Posted August 1, 2014 I understand that ^^ but again he did not teach us this, thus Im trying to teach myself before Monday, I guess what i really need to know is how to start the algorithm in plain english... Link to comment Share on other sites More sharing options...
davej Posted August 2, 2014 Share Posted August 2, 2014 Do you know how to initialize an array? Do you know how to call a function? Link to comment Share on other sites More sharing options...
niche Posted August 2, 2014 Share Posted August 2, 2014 (edited) 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 August 2, 2014 by niche Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now