Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 05-08-2003, 08:18 PM   #1 (permalink)
SkittlesAreYum
Registered User
 
Join Date: Apr 2003
Posts: 8
SkittlesAreYum is on a distinguished road
Need an alphabetic sort algorithm

Does anyone have a link to an algorithmthat will sort a vector or array alphabeically? Preferably with example code along with the algorithm. I already Googled for it, without much luck. Thanks much.
SkittlesAreYum is offline   Reply With Quote
Old 05-08-2003, 11: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
Old 05-08-2003, 11:59 PM   #3 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
its funny that your goggle searching didnt lead you here: http://codenewbie.com/forum/showthre...&threadid=1032

java makes its really easy to sort
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 05-09-2003, 12:01 AM   #4 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
uh, but that would be more like "Collections.sort(myVector);" i think...
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 05-09-2003, 12:02 AM   #5 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
awww....

I'm totally cursexoring that I didn't think of that. Of course Java has built in sorts, nutz
__________________
http://home.cwru.edu/~cak19

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

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
About Draw Line Algorithm , please! john_tran Program Design and Methods 10 11-11-2004 03:44 AM
sort arrays Apodysophilia Java 9 04-04-2003 07:18 AM


All times are GMT -8. The time now is 12:12 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting