Online Book Reader

Home Category

Mastering Algorithms With C - Kyle Loudon [144]

By Root 1601 0
it is spelled correctly.

The runtime complexity of spell is O (lg n), the same time as bisearch, where n is the number of words in dictionary. The runtime complexity of checking an entire document is O (m lg n), where m is the number of words in the text to validate and n is the number of words in the dictionary.

Example 12.9. Header for Spell Checking

/*****************************************************************************

* *

* -------------------------------- spell.h ------------------------------- *

* *

*****************************************************************************/

#ifndef SPELL_H

#define SPELL_H

/*****************************************************************************

* *

* Define the maximum size for words in the dictionary. *

* *

*****************************************************************************/

#define SPELL_SIZE 31

/*****************************************************************************

* *

* --------------------------- Public Interface --------------------------- *

* *

*****************************************************************************/

int spell(char (*dictionary)[SPELL_SIZE], int size, const char *word);

#endif

Example 12.10. Implementation of a Function for Spell Checking

/*****************************************************************************

* *

* -------------------------------- spell.c ------------------------------- *

* *

*****************************************************************************/

#include

#include "search.h"

#include "spell.h"

/*****************************************************************************

* *

* ------------------------------ compare_str ----------------------------- *

* *

*****************************************************************************/

static int compare_str(const void *str1, const void *str2) {

int retval;

if ((retval = strcmp((const char *)str1, (const char *)str2)) > 0)

return 1;

else if (retval < 0)

return -1;

else

return 0;

}

/*****************************************************************************

* *

* --------------------------------- spell -------------------------------- *

* *

*****************************************************************************/

int spell(char (*dictionary)[SPELL_SIZE], int size, const char *word) {

/*****************************************************************************

* *

* Look up the word. *

* *

*****************************************************************************/

if (bisearch(dictionary, word, size, SPELL_SIZE, compare_str) >= 0)

return 1;

else

return 0;

}

Questions and Answers


Q: Suppose we need to sort all of the customer records for a worldwide investment firm by name. The data is so large it cannot be fit into memory all at once. Which sorting algorithm should we use?

A: Merge sort. Aside from running efficiently in O (n lg n) time, the predictable way that merge sort divides and merges the data lets us easily manage the data ourselves to efficiently bring it in and out of secondary storage.

Q: Suppose we are maintaining a list of sorted elements in a user interface. The list is relatively small and the elements are being entered by a user one at a time. Which sorting algorithm should we use?

A: Insertion sort. The runtime complexity of insertion sort when inserting a single element into a list that is already sorted is O (n).

Q: Suppose we need to sort 10 million 80-character strings representing DNA information from a biological study. Which sorting algorithm should we use?

A: Radix sort. However, precisely how radix sort performs in relation to other sorting algorithms depends on the radix value we choose and our space constraints. An important consideration in selecting radix sort is that the elements in the data are a fixed size and can be broken into integer pieces.

Q: Suppose we need to sort 10,000 C structures containing information about the flight schedule for an airline. Which sorting algorithm should

Return Main Page Previous Page Next Page

®Online Book Reader