Friday, March 28, 2014

Week 11: Sorting and Efficiency

This week's topic is about the efficiency of sorting algorithms and searching techniques given. I have learned how to make possible sorting algorithms and which one is the best depending on the given situation.

         For example, say if I have 7 cards with each having a different value,(we'll use 1, 3, 4, 7, 9, J and Q) you want me to arrange them in an order from lowest to highest, that is what I will do. Quick sort uses pivot method and compares the next card on the right and compares if the values are less or more, if less, the card moves down, elif more, the card remains the same position and the next card gets examined. Such as if the pivot is 7, and the element on the right is 3, 3 goes to the left of 7, it the card is 9, it remains there. You have to ensure all the elements are correctly arranged in this position. Using an order of O(nlgn), it may exactly be efficient, but during my labs studies, I have learned that selection, quick and lists sorts work best when sorted, due to pivot changing every instance and how they don't have to switch and exchange for every value coming its way. There are many ways to sort using quick and merge. However, Merge sort and best case insertion sort works better in the case where lists were not sorted.

       Here's why, merge sort splits up lists until each list has only one element and , but because the sorting treats elements like lists, it may take a while, but it cuts down and merges everything until a fine list is made. Selection sorts and bubble sorts may not be a good idea because they have to look over all the elements for every turn, and the algorithm will also compare each other to other elements, so the order is O(n**2). A quick sort is generally faster, but may not work in some cases because of they may take a while when it comes to much larger lists, ranging the 1000s. So these are examples that if lists were somewhat sorted or put into a sever random order, different sorts must be used to understand which one is best efficient and useful.

Anyways, that should be it for sorting and maintaining the best possible efficient code while minimizing work and
An example as proof of my knowledge:
def merge(L1, L2) -> list:
 L = empty list for accumulator
 // take two elements
i1, i2 = 0, 0
#    every slice is a (linear) copy!
    while L1[i1:] and L2[i2:]:
#    while i1 < len(L1) and i2 < len(L2):
        if L1[i1] < L2[i2]:
            L.append(L1[i1])
            i1 += 1
        else:
            L.append(L2[i2])
            i2 += 1
    return L + L1[i1:] + L2[i2:]


Please check out my other SLOG for the past weeks, they contain pretty interesting information that I managed to pick up and practice on my own.

1 comment:

  1. thank you for posting this. it actually really helped me :)

    ReplyDelete