If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > C++ bubble sort to sort names

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-09, 01:00
DaveMason DaveMason is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
C++ bubble sort to sort names

If you have an array of names.
Does anyone know if it is possible to sort names in ascending using bubble sort in C++?


thanks,
Dave
Reply With Quote
  #2 (permalink)  
Old 04-17-09, 03:54
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,243
yes
either write of rind a suitable sort routine

or consider stuffing them into a collection or other data storage mechanism which as a sort/key associated with each item.

or stuff the data into an array or the pointer to the data into an array that you then sort.

FWIW a comb sort is supposed to be significantly faster than bubble.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 04-17-09, 09:04
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,598
You could also just specify an ORDER BY clause on the SELECT statement that you use to retrieve the data so that you'd load it into your array in the order that you want it. This is a lot easier than reading the data then sorting it.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #4 (permalink)  
Old 04-17-09, 22:53
DaveMason DaveMason is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
thanks. the lecture in class only focuses sorting integers using bubble sort and now the assignment is to sort a list of names. i'm stuck. i guess, i'll google for some samples.

thanks again.
Reply With Quote
  #5 (permalink)  
Old 04-18-09, 02:18
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,243
so did you try to do a comparison between strings
essentially there is no difference between comparing strings, numbers or whatever, thats all that changes in your sort.
if you want to be a bit greasy use the existing sort mechanism and provide overloaded methods to handle strings.

therefore you have a sort routine that can handle both.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 04-20-09, 23:09
DaveMason DaveMason is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
Hello!
I got the bubble sort to sort list of names now. thanks.
Reply With Quote
  #7 (permalink)  
Old 04-24-09, 12:18
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
FYI: the bubble sort is quite efficient when N is small (less than 50 or so)
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On