Monday, November 12, 2007

Shaker Sort


Everyone who has ever programmed a sort has probably done a Bubble sort (and hopefully lived to tell about it). The truth is is that bubble sorts are incredibly simple. But they do have worst cases, such as a reverse ordered list of items. It would take bubble sort order n^2 time to complete any semi-reverse ordered items. Shaker sorts can help resolve this problem.


Generally bubble sorts move from one end of a list of items to the next. Shaker sorts move from one end of the list to the next and then from the other end of the list to the beginning. Pretty straight forward? Well you can write up a C bubble sort in about 6 lines of code. Shaker sorts take a little bit more, and are a little more complicated, but well worth the study.

Here is a C based example of a shaker sort.

#include

#define key(A) (A)

#define less(A, B) (key(A) <>

#define exch(A, B) {int t = A; A = B; B = t;}

#define compexch(A, B) if(less(B, A)) exch(A, B)


void shaker(int *, int, int);


int main(){

int array[10] = {4, 1, 7, 3, 2, 8, 9, 6, 5};

shaker(array, 0, 9);

int i;

for(i = 1; i < 10; i++){

printf("%d ", array[i]);

}

return 0;

}


void shaker(int *array, int l, int r){

int sorted = 1, i, swaps = 0; // Define two integers. Initialize sorted to 1. Sorted will terminate the while loop if nothing has been sorted.

while(sorted){

sorted = 0; // Always set sorted to 0 just to make sure we can continually test to see if it has been sorted

for(i = l; i <>// This loop will be the left-to-right swapping

if(less(array[i], array[i - 1])){ // Check adjacent elements to see if i is less than  i - 1. Swap them if they are.

exch(array[i - 1], array[i]);

sorted = 1; // Sorted will be set to 1 to make sure we either continue looping or don't.

swaps++;

}

if(sorted == 0) break; // An extra text but it will terminate the function if it is already sorted.

}

for(i = r; i > 0; i--){ // This loop will be the right-to-left swapping

if(less(array[i], array[i - 1])){ // Check adjacent elements to see if i is less than  i - 1. Swap them if they are.

exch(array[i - 1], array[i]); 

sorted = 1; // Sorted will be set to 1 to make sure we either continue looping or don't.

swaps++;

}

if(sorted == 0) break;

}

}

printf("\nIt took a total of %d exchanges to sort the list.\n", swaps);

}

Saturday, November 10, 2007

Recursive Postfix Expression Evaluator


In a previous post (here). I created a Reverse Polish Notation calculator which would take digits in along with operators in postfix form and return an evaluated expression. The RPN calculator was implemented using my own Stack data structure (based off of an array). The following program is a postfix expression evaluator which uses recursion as a kind of stack implementation. As operands are found in the expression the eval function goes deeper into the recursion (recurses = push operands on stack). When a operator is found the eval function effectively moves up levels in the recursion (popping) values and performing the necessary operations. Enjoy:


/*

 *  

 *

 *  Created by Devin Eldreth on 11/2/07.

 * This program contains a recursive postfix expression evaluator (eval). A global array of characters is set to a postfix expression.

 * eval is called and recursively evaluates the expression to return the integer result. Current the function only works with single digit operands

 * and any of the four mathematical operators +, -, *, /

 */


#include

#include


char *a; // Define a string to hold the expression

int i = 0, y = 0, skip = 0; // Initilize 3 integer values. y will basically the top of the stack (recursively). 

    //Skip will be the test to see if we have already recursed twice and will increment i


int main(){

//a = "5 9 8 + 6 4 * * 7 + *"; //= 2075 (Problem in book)

//a = "2 6 / 8 *"; //= 24

a = "3 5 4 6 * * * 2 / 3 -"; //= 177

printf("\n%s = %d\n", a, eval());

return 0;

}


int eval(){

int x = 0;

for(;;){

while(a[i] == ' ') i++; // Always increment array index if it is just a space

// Return the calculated value y if there is an operator or assume termination when i is the value of the length of the array

// This will actually bring values back from the recursion

if((i == strlen(a)) || (a[i] == '*') || (a[i] == '+') || (a[i] == '-') || (a[i] == '/')) return y;

// Read each array element and recurse after getting the numberical value at index i

while((a[i] >= '0') && (a[i] <= '9')){

x = (a[i++] -'0');

eval();

}

if(a[i] == '*'){ // Handle the multiplicative operator

if(y == 0) y++;

y *= x; 

skip++; // Check to see if the y value is 0 and increment it to handle the initial multiplication. Increment skip.

}

if(a[i] == '+'){ // Handle the addition operator

y = (y == 0) ? x : y + x; 

skip++; // Test to see if y is 0 and set it to x (this isn't quite necessary). Increment skip.

}

if(a[i] == '-'){ // Handle the difference operator

y = (y == 0) ? x : y - x; 

skip++; // Identical to the addition handling test.

}

if(a[i] == '/'){ // Handle the division operator

y = (y == 0) ? x : y / x; 

skip++; // Identical to the addition test.

}

// Check to see if skip is equal to 2. Increment i and set skip equal to 1. This will handle the event that the counter becomes stuck on a single operator.

if(skip == 2) { i++; skip = 1; }

}

}


Thursday, October 18, 2007

Apple Announces iPhone and iPod touch SDK

Steve Jobs announced, yesterday, that Apple is working on and would be releasing a SDK (software development kit) for the iPhone and iPod touch. This is HUGE news, because the biggest debate about both devices is that fact that they are currently closed to 3rd party development. Of course people have figured out how to hack their devices, but Apple has also made more and more restrictions on the hardware. Its really a big deal because Apple has to weed through their open SDK for developers to a point where it is secure enough to be released for mobile devices that could potentially pose problems to wireless networks (AT&T wouldn't be too happy about it), and they have to make sure that virus' are not a big problem. There have been a lot of other phones which have had viral problems which bounce from phone to phone over the wireless network.

But it also means that one of the most advanced mobile communications and multimedia devices will be an open platform for development.


Sounds like fun, I can't wait until February. Here is a quotation from the original message from Steve.


Let me just say it: We want native third party applications on the iPhone, and we plan to have an SDK in developers’ hands in February. We are excited about creating a vibrant third party developer community around the iPhone and enabling hundreds of new applications for our users. With our revolutionary multi-touch interface, powerful hardware and advanced software architecture, we believe we have created the best mobile platform ever for developers.

It will take until February to release an SDK because we’re trying to do two diametrically opposed things at once—provide an advanced and open platform to developers while at the same time protect iPhone users from viruses, malware, privacy attacks, etc. This is no easy task. Some claim that viruses and malware are not a problem on mobile phones—this is simply not true. There have been serious viruses on other mobile phones already, including some that silently spread from phone to phone over the cell network. As our phones become more powerful, these malicious programs will become more dangerous. And since the iPhone is the most advanced phone ever, it will be a highly visible target.

Some companies are already taking action. Nokia, for example, is not allowing any applications to be loaded onto some of their newest phones unless they have a digital signature that can be traced back to a known developer. While this makes such a phone less than “totally open,” we believe it is a step in the right direction. We are working on an advanced system which will offer developers broad access to natively program the iPhone’s amazing software platform while at the same time protecting users from malicious programs.

We think a few months of patience now will be rewarded by many years of great third party applications running on safe and reliable iPhones.

Steve

P.S.: The SDK will also allow developers to create applications for iPod touch.

[Oct 17, 2007]

Friday, October 12, 2007

Synergie Codes development blog moving!

No matter how much I may love to use iWeb to depoly my blogs I miss the ability to be able to post to it anywhere without my computer. So all of the new posts for synergiecodes.com will be found under this blog accound. http://www.synergiecodes.blogspot.com. Thats about it for this post! Happy coding.


However, should you need to see any of my old posts please refer to this link: