Bubble Sort

by levans, Feb 17, 2020, 12:05 AM

A long time ago, when the Earth was green, there were more types of animals than you've ever seen. I also had a high school programming class in which we learned about bubble sort, along with a bunch of other things. In college, we learned many more algorithms. The problem, though, is that I have not had much need to use any of these algorithms in my day to day working. That's OK. The point of much of this learning was how to think about algorithms, not the algorithm itself. But, since I've basically forgotten all the original algorithms, I've decided to go back to high school and re-learn some of these things. First up is something really simple, a sorting algorithm called Bubble Sort. Bubble sort is slow, but it's pretty neat in that, unlike many algorithms, it's extremely easy to visualize what is going on. It's simply going through a list of items, comparing them to the next item, and swapping the two if they are out of order. Repeat. Repeat. Repeat. Then when all the items are sorted, break out. I'd draw pictures if I weren't lazy, but I personally find it a lot easier just to read code.
/**
 * Implementation of bubble sort algortihm, generally considered
 * one of the slowest, but most understandable sorting algorithms.
 *
 * @param array $list An array of elements to sort.
 */
function bubbleSort($list) {

    // Get size of list, subtracting 1 because arrays are 0 indexed.
    $size = sizeof($list) - 1;

    do {
        // For each iteration, we reset the $swapped variable.
        // When the entire list has been iterated over, and
        // no swaps have occured, we know the list is sorted
        // and we end there.
        $swapped = false;

        // Loop through each item comparing it to the next item
        // in the list. We us < $size instead of <= $size, because
        // we'd otherwise try to compare the last element to something
        // outside of the array, which will lead to an out of
        // bounds error.
        for ($i = 0; $i < $size; $i++) {
            // Get element values
            $current = $list[$i];
            $next = $list[$i + 1];

            // Swap if out of order. Change to > if you want descending
            if ($next < $current) {
                $list[$i + 1] = $current;
                $list[$i] = $next;

                // Set flag indicating a swap has been made
                $swapped = true;
            }
        }
    } while ($swapped);

    return $list;
}

$x = [1, 8, 7, 2, 10, -8, 39, 12];
print_r(bubbleSort($x));
This post has been edited 3 times. Last edited by levans, Mar 23, 2020, 6:46 AM

Comment

5 Comments

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
What do the $ before variable names mean?

by fuegocaliente, Feb 17, 2020, 7:56 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
That's just how languages like Perl, PHP, and Bash indicate variable names.
This post has been edited 1 time. Last edited by levans, Feb 17, 2020, 8:00 PM

by levans, Feb 17, 2020, 7:59 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Cool. Ive never seen that before.

by fuegocaliente, Feb 18, 2020, 6:32 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
I just implemented bubble sort using ARM assembly language!

by SirCalcsALot, Mar 10, 2020, 7:55 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Cool, have you posted it online?

by levans, Mar 23, 2020, 6:46 AM

Opinions expressed herein are my own and not necessarily those of AoPS Incorporated.

avatar

levans
Archives
- January 2025
+ September 2023
+ September 2022
+ August 2020
+ November 2019
+ March 2019
+ January 2019
+ December 2018
+ September 2016
+ August 2015
+ July 2015
+ April 2015
+ February 2015
+ October 2014
+ August 2014
+ March 2014
+ February 2014
+ October 2013
+ September 2013
+ August 2013
CoC
+ June 2013
+ April 2013
+ February 2013
+ November 2012
+ September 2012
+ August 2012
+ April 2012
+ February 2012
+ January 2012
+ November 2011
Shouts
Submit
  • beautiful css omg

    by yaxuan, Feb 10, 2025, 5:31 AM

  • hello I just saw this

    the CSS here is pro
    $\text{also what is this font}$

    by Yrock, Feb 4, 2025, 2:36 AM

  • hiii it's been a long time

    300th shout!! :D

    by evt917, Dec 20, 2024, 5:15 PM

  • @LostInBali, it's totally based on terminals.

    by levans, May 22, 2024, 12:09 AM

  • HIIII! This is beautiful CSS and it reminds me of bash!!!!!!!
    (I do hope you like terminals)

    by LostInBali, May 1, 2024, 8:41 AM

  • COOOOL BLOG!!

    by Yummo, Feb 4, 2024, 7:09 PM

  • geez 100k+ views

    by ujulee, Dec 5, 2023, 5:47 PM

  • HOW SO ORZ WHAT THE HECK

    by madeleinelee, Dec 5, 2023, 6:03 AM

  • hi $          $

    by Bob_Smart, Nov 4, 2023, 4:38 AM

  • I like this blog so much it's so impressive!

    by BabaLama, May 2, 2023, 1:26 AM

  • bump$    $

    by TethysTide, Nov 16, 2022, 4:18 PM

  • pls don't click my username thanks :)

    by RyanWang, Sep 28, 2022, 4:26 AM

  • aops* $     $

    by programmeruser, Sep 27, 2022, 9:51 PM

  • Hello!$    $

    by Peregrine11, Aug 29, 2022, 9:02 PM

  • this is the best blog on aopa

    by programmeruser, Jun 16, 2022, 12:10 AM

302 shouts
About Owner
  • Posts: 5190
  • Joined: Apr 27, 2007
Blog Stats
  • Blog created: Jul 27, 2011
  • Total entries: 105
  • Total visits: 107916
  • Total comments: 616
Search Blog
a