Saturday, 29 March 2014

SORTING AND EFFICIENCY

Sorting is a way to arrange a set of data in a specific order. There are different ways by which we could sort data. They all do the same thing but with different approaches. We have also been able to compare these different kinds by timing them in order to obtain their efficiency and to know which one to use regarding the length of data we have to sort. There are different ways by which we could data. Some of the methods we were introduced to are:
1.) Bubble Sort: This method goes over every item in the list and swaps them until the list is in the right order.
2.) Selection Sort: This method continuously selects the smallest remaining item in the list and places it where it belongs.
3.) Insertion Sort: This method divides the list into two part, the other part an empty list. It goes through every item in the list and places them in their respective position in the sorted part.
4.) Merge Sort: This method works by splitting the list into two part and sorting them separately then merging them.
5.) Quick Sort: This method works recursively by sorting the list according to the first item in the list. It checks every item and divides the list into two part, one part being the items that are less than the first item; it repeatedly calls itself for the other parts of the list.
6.) list.sort: This is Python's built-in function for sorting data and is usually the most efficient in most cases.

Some particular methods are more efficient in some cases while some are not. It all depends on the size of the list and the format(sorted, unsorted, reversed). Generally, the built-in function proves to be the efficient in most cases and quick sort is less efficient because it's recursive and as the length of the list increases, the function reaches its maximum recursive.


No comments:

Post a Comment