Aaron Gadberry

Help – v. helped, help·ing, helps

Was this site helpful?
My Amazon.com Wishlist

Archive for the 'Programming' Category

Any programming tips, techniques, bug solutions, or work arounds.

How To: Run a Perl script from PHP

23rd January 2006 - By Aaron

While you may be dreading this part of your project, this task is surprisingly easy for non-major implementations.

The example I’m going to use for this is implementing a perl calendar into a php web page.

First off, you need to have your web server correctly serving the perl as is. In other words, call the perl you will want from a web browser. If http://www.yoursite.com/cgi-bin/helloworld.pl works right, then you’re set. If it doesn’t, you need to troubleshoot why your perl isn’t being executed correctly.

Next you need to use a simple include-style command called virtual.

Here’s an example of this in use.

Read the rest of this entry »

Posted in Computers, Programming | 5 Comments »

Sorting: Driver Program for Sorts

13th January 2006 - By Paradochs

Description

This code implements and runs the sort algorithm code in other sort articles found here.

Read the rest of this entry »

Posted in Computers, Programming | No Comments »

Sorting: Quick Sort

13th January 2006 - By Paradochs

Information

  • Order: O(n²)
  • Best Case: O(nlog(n))
  • Average: O(nlog(n))
  • Memory Usage: O(log(n))
  • Stable: No
  • Comparison Based: Yes

Description (from wikipedia)

Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, on average, makes O(n log n) comparisons to sort n items. However, in the worst case, it makes O(n²) comparisons. Typically, quicksort is significantly faster in practice than other O(n log n) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data it is possible to make design choices which minimize the possibility of requiring quadratic time. Quicksort is a comparison sort.
Quicksort sorts by employing a divide and conquer strategy to divide a list into two sub-lists.

The steps are:

  1. Pick an element, called a pivot, from the list.
  2. Reorder the list so that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
  3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements.

The base case of the recursion are lists of size zero or one, which are always sorted. The algorithm always terminates because it puts at least one element in its final place on each iteration.

Read the rest of this entry »

Posted in Computers, Programming | No Comments »

Sorting: Merge Sort

13th January 2006 - By Paradochs

Information

  • Order: O(nlog(n))
  • Best Case: O(nlog(n))
  • Average: O(nlog(n))
  • Memory Usage: O(n)
  • Stable: Yes
  • Comparison Based: Yes

Description (from wikipedia)

Conceptually, merge sort works as follows:

  1. Divide the unsorted list into two sublists of about half the size
  2. Sort each of the two sublists
  3. Merge the two sorted sublists back into one sorted list.

The algorithm was invented by John von Neumann in 1945.

Read the rest of this entry »

Posted in Computers, Programming | No Comments »

Sorting: Insertion Sort

13th January 2006 - By Paradochs

Information

  • Order: O(n²)
  • Best Case: O(n)
  • Average: ?
  • Memory Usage: O(1)
  • Stable: Yes
  • Comparison Based: Yes

Description (from wikipedia)

Insertion sort is a simple sort algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than the more advanced algorithms such as quicksort, heapsort, or merge sort, but it has various advantages:

  • Simple to implement
  • Efficient on (quite) small data sets
  • Efficient on data sets which are already substantially sorted
  • More efficient in practice than most other simple O(n2) algorithms such as selection sort or bubble sort
  • Stable (does not change the relative order of elements with equal keys)
  • In-place (only requires a constant amount O(1) of extra memory space)
  • It is an online algorithm, in that it can sort a list as it receives it.

In abstract terms, each iteration of an insertion sort removes an element from the input data, inserting it at the correct position in the already sorted list, until no elements are left in the input. The choice of which element to remove from the input is arbitrary and can be made using almost any choice algorithm.

Sorting is typically done in-place. The result array after k iterations contains the first k entries of the input array and is sorted. In each step, the first remaining entry of the input is removed, inserted into the result at the right position.

The most common variant, which operates on arrays, can be described as:

  1. Suppose we have a method called insert designed to insert a value into a sorted sequence at the beginning of an array. It operates by starting at the end of the sequence and shifting each element one place to the right until a suitable position is found for the new element. It has the side effect of overwriting the value stored immediately after the sorted sequence in the array.
  2. To perform insertion sort, start at the left end of the array and invoke insert to insert each element encountered into its correct position. The ordered sequence into which we insert it is stored at the beginning of the array in the set of indexes already examined. Each insertion overwrites a single value, but this is okay because it’s the value we’re inserting.

Read the rest of this entry »

Posted in Computers, Programming | 2 Comments »