View Single Post
Old 05-08-2003, 10:52 PM   #2 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
How about some pseudo code

And then you can just fill it in

We have vector unsorted, which is well, unsorted. It's of variable length and isn't empty (this is sounding like an AP problem)

Code:
bool done = false; //Controls when sort is done
int cur = 0; // Current space in sorted vector
int smallest = 0; // Base of comparison for sort


vector<char*> sorted; // Make a new vector of strings called sorted

sorted.resize(unsorted.length()); // Make it the same size as unsorted


while(!done) // Here's the sorting code
{
  //This loop finds the smallest item within the unsorted array
  for(i=0; i < unsorted.length(); i++)
  {
    if(unsorted[smallest] > unsorted[i])
    {
      smallest = i;
    }
  }
  //Once we have the smallest item we place it at the front of the sorted array
  sorted[cur] = unsorted[smallest];
  cur++; //move to the next element in the sorted array

  //Then we have to remove the item from the unsorted array
  for(i=smallest; i < unsorted.length(); i++)
  {
    unsorted[i] = unsorted[i++];
  }

  unsorted.resize(unsorted.length()-1);
  /*Make the vector smaller so it doesn't become filled with the same last value and doesn't loop forever*/

  if(!unsorted.length()) // If the unsorted array is empty then you're done
  {
    done = true;
  }
}
Things you should know

1) This sort is super inefficient.

2) My Pseudo code uses some pretty high level functionality for it's arrays (like resizing) and strings (like string less than greater than comparison). These are things that aren't implemented in some base classes so you still might need to write/find that.

3) I didn't test this (duh it's pseudo code) but I'm fairly positive it would work assuming it was filled in with proper code. It's solid in theory if not physically.

BTW, this question doesn't really belong here as it is in no way language specific. This probably should have gone in program design and methods. No points off though, at least not this time... (duh duh duunnn ominous music)
__________________
http://home.cwru.edu/~cak19

It's my homepage with odd little bits of javascript.
saline is offline   Reply With Quote