Sunday, March 30, 2014

Sorting and Efficiency

  The topic of this week is sorting and efficiency. So, the first question arise in my head is: what exactly is sorting and efficiency, and how is it relate to programming? According to wikipedia: A sorting algorithm is an algorithm that puts elements of a list in a certain order. Here is an example I found on the internet which explains how sorting algorithm works (source:http://hetland.org/coding/python/quicksort.html):
 #!/usr/bin/env python

# Written by Magnus Lie Hetland 

"Everybody's favourite sorting algorithm... :)"

def partition(list, start, end):
    pivot = list[end]                          # Partition around the last value
    bottom = start-1                           # Start outside the area to be partitioned
    top = end                                  # Ditto

    done = 0
    while not done:                            # Until all elements are partitioned...

        while not done:                        # Until we find an out of place element...
            bottom = bottom+1                  # ... move the bottom up.

            if bottom == top:                  # If we hit the top...
                done = 1                       # ... we are done.
                break

            if list[bottom] > pivot:           # Is the bottom out of place?
                list[top] = list[bottom]       # Then put it at the top...
                break                          # ... and start searching from the top.

        while not done:                        # Until we find an out of place element...
            top = top-1                        # ... move the top down.
            
            if top == bottom:                  # If we hit the bottom...
                done = 1                       # ... we are done.
                break

            if list[top] < pivot:              # Is the top out of place?
                list[bottom] = list[top]       # Then put it at the bottom...
                break                          # ...and start searching from the bottom.

    list[top] = pivot                          # Put the pivot in its place.
    return top                                 # Return the split point


def quicksort(list, start, end):
    if start < end:                            # If there are two or more elements...
        split = partition(list, start, end)    # ... partition the sublist...
        quicksort(list, start, split-1)        # ... and sort both halves.
        quicksort(list, split+1, end)
    else:
        return

    
if __name__=="__main__":                       # If this script is run as a program:
    import sys
    list = map(int,sys.argv[1:])               # Get all the arguments
    start = 0
    end = len(list)-1
    quicksort(list,start,end)                  # Sort the entire list of arguments
    import string
    print string.join(map(str,list))           # Print out the sorted list
Now, algorithmic efficiency on the other hand are the properties of an algorithm which relate to the amount of resources used by the algorithm. Efficiency on one hand can be thought as  the productivity for a repeating or continuous process. Here is an example of how to determine the efficiency of an algorithm(source:http://www.cprogramming.com/tutorial/computersciencetheory/algorithmicefficiency2.html):for(int x=0; x<n; x++)
 {
   int min = x;
   for(int y=x; y<n; y++)
   {
  if(array[y]<array[min])
    min=y;
   }
   int temp = array[x];
   array[x] = array[min];
   array[min] = temp; 
 }
We compute that the order of the outer loop (for(int x = 0; ..)) is O(n); then, we compute that the order of the inner loop is roughly O(n). Note that even though its efficiency varies based on the value of x, the average efficiency is n/2, and we ignore the constant, so it's O(n). After multiplying together the order of the outer and the inner loop, we have O(n^2).
It may look as simple as the example shows, but the actual operation is much harder, and sometimes i still have difficulties solving this problems. Sorting and efficiency can be quit challenging, I will continue to work on it until i am confident with this sort of topic. 

No comments:

Post a Comment