Sunday, March 30, 2014

Sorting and Efficiency - Week 11

#You know when you have a lot of rocks and always wanted to sort them from smallest to largest or you must organize books in alphabetical order? It looks like you need some sorting algorithms. OMG WHAT IS THAT? I'll tell you what it is, it is a revolutionary concept. For only $29.99 that you just paid for looking at this blog.#

Okay past this nonsense that I have posted, sorting algorithms are methods that can be used to organize piles and piles of junk in the correct order that are needed to be sorted such as ascending from smallest to largest, visa versa, alphabetical order like my example above, anything needs to be rearranged can go through a sorting algorithms and become sorted. Now here are some sorting algorithms to get you started on your journey through the many different ways to sort, merge sort, bubble sort, quick sort, insertion sort, selection sort, heap sort, and many more. All of these use different ways to sort:

1. Insertion sort - Good for small lists/sorted lists
Method: Sorts the list from left to right placing with each item it moves onto moves the chosen item in its correct place in the sorted portion leaving the left side of the list sorted and right side unsorted.

2. Selection sort - Same as insertion but worse
Method: Searches through the whole unsorted portion of the list looking for the lowest value in the unsorted list. Once the lowest value has been found it swaps it with the first item in the unsorted side of the list.

3. Merge Sort - a very stable (the element order) and popular sort
Method: The Divide and Conquer algorithm. In simple terms, it takes the list and splits it in two and continues to split it in half until it reaches only piles of two. It then splits each pile of two and the compares the two of them and places them in order. It then proceeds to merge them back together placing them in order comparing with each pile next to it.

These are just some basic examples. Quick sort though is one of the most interesting because out of all of these sorts it uses a pivot (a chosen item). From the chosen pivot (some use the last item in the list or the middle item in the list, this can change how efficient it can be) it sorts everyone lower than the pivot on the left and everything greater than the pivot on the right. These groups are unsorted the only item that is definitely in the right place of the list is the pivot. It then chooses a new pivot on the left group (middle or last, depends) repeats until it everything is sorted left and right. This is done for the whole list.

I hope you enjoyed the basic explanation of sorting algorithms and how they can affect efficiency.

Quick Sort - Versatile

No comments:

Post a Comment