Categories
Programming & Web Development

Select sort algorithm explained with code and dance

Select sort is a very simple algorithm but it’s very inefficient for large lists, but it is good when memory is an issue, since it is an in-place sorting algorithm. Meaning it is an algorithm that doesn’t need to use extra memory to store sorted from unsorted elements.

The way it works is the following: Take the fist element in the list and compare with the next elements to find the minimum value of the list. If found swap values. Repeat starting with second element and so on until reaching the last element in the list.

Select sort example

Big-O notation

O(n2)

Pseudo code

for each current_element in the list:
  assume_minimum = current_element
  for each element in the list:
     if assume_minimum > element[value]
        assume_minimum = element[value]
  swap current element with assume_minimum values

PHP code example

/**
 * Select sort
 *
 * @param array $list The unsorted list
 * @return array The sorted list
 */
function select_sort($list) {
  for($i = 0; $i <= count($list); $i++) {
      $min_position = $i;
      for($j = $i + 1; $j <= count($list); $j++) {
          if($list[$min_position] > $list[$j]) {
              $min_position = $j;
          }
      }
      list($list[$i], $list[$min_position]) = array($list[$min_position], $list[$i]);
  }
  return $list;
}

Python code example

def select_sort(a_list):
  """ Performs a select sort on list """
  for i in range(0,len(a_list)-1):
    min_position = i
    for j in range(i+1,len(a_list)):
      if a_list[min_position] > a_list[j]:
        min_position = j
    a_list[i], a_list[min_position] = a_list[min_position], a_list[i]
  return a_list

Gypsy folk dance example

Categories
Programming & Web Development

Insert-sort algorithm explained with code and dance

Insert sort is a fast algorithm for small lists. On some languages, insert sort is used to sort small arrays (small sometimes means less than 10) instead of more complex algorithms because it’s fast enough and doesn’t need much memory.

Insertion sort example

To perform Insert-sort: take one element from the list on each iteration, starting from the first, and compare with the next element. If the previous one is greater, move it one position ahead. Continue comparing the rest of the elements pushing them forward until you find a the place of the current value and no elements are left. Repeat with next element in list.

Big-O notation

O(n2)

Pseudo code

function insert_sort(list):
  (evaluate the list forward)
  for i from 1 to length_of_list - 1:
    value = list[i]
    (evaluate the list backward from i):
    while prev_position >= 0 and list[prev_position] > value:
      (swap values from current position to the next)
      list[prev_position+1] = list[prev_position]
      go on position back in the list
    if there was no swap, set value back in current position

PHP code example

function insert_sort($list) {
    for($i = 0; $i < count($list); $i++) {
        $value = $list[$i];
        $j = $i - 1;
        while($j >= 0 AND $list[$j] > $value) {
            $list[$j + 1] = $list[$j];
            $j--;
        }
        $list[$j + 1] = $value;
    }
    return $list;
}

Python code example

def insert_sort(unordered_list):
    for i in xrange(0,len(unordered_list)):
        value = unordered_list[i]
        j = i -1
        while (j >= 0) and (unordered_list[j] > value):
            unordered_list[j+1] = unordered_list[j]
            j -= 1
        unordered_list[j+1] = value
    return unordered_list

Insert sort algorithm can be explained better visually with a Romanian folk dance:

Categories
Photography

Ballet class

GST_DSC_1261

I had the opportunity to photograph a warm-up ballet class for the little girls at the National Dance Contest in Mexico city. This event is done yearly with students of many ages from all around the country from private and public dance schools.

In all the years we’ve been together, this was the first time I was able to photograph her at work through a complete class. I’ve taken pictures before on pre-show warm-ups, but never at a classroom, a full class.

GST_DSC_1213

GST_DSC_1239

It was interesting to watch her work with the kids. I’ve witnessed Ada’s teaching career since before she started professionally training for it. She’s very good at talking with kids, directing them and having fun while at the same time not loosing control and respect of the group. A very hard task to accomplish if you’ve ever worked with children.

GST_DSC_1225

GST_DSC_1244

GST_DSC_1274

Categories
Events Photography

25th Anniversary of the Nutcracker ballet by the ESMDM in Monterrey

A few weeks ago I was invited to the 25th Anniversary of the Nutcracker ballet by the ESMDM in Monterrey. As I’ve done for the last 3 or 4 years (maybe even more) I’ve been attending to the Nutcracker (and other events) by the ESMDM and took some pictures of it. Instead of just sharing on my Flickr page, I’d like to share them here too.

This year I initially forgot my camera at home, but since the theater is about 5 blocks away, in the intermediate time I ran back to get it. So I don’t have any pictures of the first act this year. Before the ballet started there was a celebration and commemoration of the 25 years of the Nutcracker every winter. It was that they made a plaque with the names of every dancer that participated on the ballet for those 25 years.

At the end, all the ex-alumni of the ESMDM who were at some point part of the Nutcracker cast took a group picture. I think I saw at least two generations of the same family in that picture, that was something nice to see.

I used my Canon EF-S 18-55mm f/3.5-5.6 IS lens with varying exposure times. For these type of events, since there’s low light and little time to adjust your camera, I use the Tv setting and adjust shutter speed as needed depending on current illumination. I still find it hard to find the perfect balance between fast shutter speeds and low light compensation to capture the movements in mid air, but I’ve found that the shutter speed setting and AI Servo auto focus mode makes it easier for starters.

Do you have tips for stage photography? What lens do you recommend for these events? Let me know in the comments.