Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    8

    Unanswered: 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

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    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 on the Tiger 800 or the Norton

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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.

  4. #4
    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.

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    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 on the Tiger 800 or the Norton

  6. #6
    Join Date
    Mar 2009
    Posts
    8
    Hello!
    I got the bubble sort to sort list of names now. thanks.

  7. #7
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •