Thread: How will this function help sort a struct array of characters?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    How will this function help sort a struct array of characters?

    Well out teacher told us to modify, this function to sort string names inside an array structure.
    Here is what he posted in the assigment.

    "The words are to be stored in a statically-declared array. You can assume that the maximum number of
    unique words that will be submitted is 200. The words are to be stored in the array in sorted order, and you
    are to use the array-based binary search method as described in our course notes to locate words in the array."


    Code:
    int binarySearch (int sortedArray[], int first, int last, int key) {
      //   Searches sortedArray[first]..sortedArray[last] for key.
      // returns: index of the matching element if it finds key,
      //         otherwise  -(index where it could be inserted)-1.
      // parameters:
      //   sortedArray in  array of sorted (ascending) values.
      //   first, last in  lower and upper subscript bounds
      //   key         in  value to search for.
      // returns:
      //   index of key, or -insertion_position -1 if key is not
      //                 in the array. This value can easily be
      //                 transformed into the position to insert it.
    
      while (first <= last) {
        int mid = (first + last) / 2;  // compute mid point.
        if (key > sortedArray[mid])
          first = mid + 1;  // repeat search in top half.
        else if (key < sortedArray[mid])
          last = mid - 1; // repeat search in bottom half.
        else
          return mid;     // found it. return position
        }
      return -(first + 1);    // failed to find key
    } // end binarySearch
    Will this really worked? because as far as I can understand it it only returns the location of the value, but it just doesn't sort a thing.

    I plan to declare my structure like this

    Code:
    Struct Words{
      string word;
      int line;
    }word[100];
    one hundred is the number of words and line keeps track of where the word was found.
    Last edited by newbc; 07-30-2011 at 10:31 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, the function is called "search" not "sort". It is the responsibility of whoever is inserting the data to make sure the sorting order is maintained. The hint is in the comment, about the return value when the word is not found.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of struct as function parameter
    By rotger in forum C Programming
    Replies: 12
    Last Post: 11-11-2010, 02:33 PM
  2. Array of Struct sort
    By fukki in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2010, 06:08 AM
  3. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  4. Passing array of struct as a function parameter
    By desmond5 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 11:32 AM
  5. passing array of type struct to function
    By nappaji in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 05:13 PM