Thread: C if you can solve this

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    Question C if you can solve this

    I have an array of 50 elements each element has a string with no more than 50 letters. i need to sort these alphabetically.

    what is an easy way of doing this.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Bubble sort could be one good way to do it

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Bubble sort could be one good way to do it
    Correction, bubble sort would be an easy way to do it. Bubble sort is never a good solution outside of the classroom

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    He wanted an easy way and bubble sort is an easy way so there for it is a good solution to his question.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >there for it is a good solution to his question
    That's debatable.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    Ok he have an array of 50 elements. How big speed difference do you think he would get if he used a highly sofisticated sort algoritm? Face it. Sometimes VB is better then C and some times bubble is better. To say that bubble sort is "never" a good solution outside the class room is alittle bit to strong. It all depends on the applicartion and when your project needs to be done. In lala land it is cool to spend tons of time on things that doesn't necesary matters but not in real life.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >In lala land it is cool to spend tons of time on things that
    >doesn't necesary matters but not in real life.
    Oh please, next you'll be trying to tell me that insertion or selection sort is sooo much harder to implement than bubble sort. Both are faster, and just as easy to write.

    And as long as we're talking about real life, let's think of it this way. Sorting is probably the most resource intensive operation you can perform in a program, programs usually have a very long lifespan and are continually pushed to handle more and more input, if you spent any time in the "real world" instead of the class room then you would understand that 50 today means 50000 tommorow. I'd rather be using an efficient sorting method from the start so that I don't have to deal with the embarassment of having to go back in and change the algorithm because the program was taking days to sort input. There's also potential for losing your job if that happens too.

    The only time you see bubble sort in "real life" is when novices who are fresh out of schools that teach nothing but bubble sort manage to sneak it by their supervisors.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    Right tool for the job is my motto. Don't hunt moscitos with torpedos. hey I am sure you could shave even more time if you used asm, I am sure your boss would love to see you do that.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Right tool for the job is my motto. Don't hunt moscitos with
    > torpedos. hey I am sure you could shave even more time if you
    > used asm, I am sure your boss would love to see you do that.

    There is no point in arguing with people who can never admit
    the remote possibility that there is a better way of doing things.
    Simply put, there are better ways to sort than with bubble sort.
    End of story.

    Your example is this:

    I can remove a tire from my car with a tire iron, and it will work.
    However, you cannot tell me that it'd be better than my air-
    ratchet. This is simply not true. Sure, with both I have to remove
    five lug nuts, so they're both doing the same thing; however
    I obviously have the advantage over you if I'm using an air-
    ratchet and you're stuck with a tire iron.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    There are plenty of ways that are better then bubble sort and that was only an easy suggestion from my side. Here are some other methods you can search for.

    Bubble sort (well it's first in the book C++ Unleashed)
    Insertion sort
    Selection sort
    Quick sort
    Merge sort
    Shell sort
    Heap sort

    Let me know if you want help with any one in particular.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Bubble sort (well it's first in the book C++ Unleashed)
    Actually, selection sort is the first in the Rapid Sorting Techniques chapter. Also, I do believe that after the introduction to bubble sort Dann says "Don't use bubble-sort. It's simply horrible."

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Dann?? It is written by Jesse Liberty and there isn't any special chapter about rapid sorting

  13. #13
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Indeed he does. I can't remember the exact quote, but words to that effect.

    To say that Bubble sort is a good answer is like saying that "void main ()" would be a good way to define his programs primary function, since it works, and I guess, since it requires less typing on return functions, you might say it's easier. Patoohey.

    Don't worry Prelude, casting perls before swine is just like a bubble sort, a waste of valuable resources.

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Code:
                    Worst case      Best case
    Bubble sort        O(n2)              O(n)
    Insertion sort     O(n2)              O(n)
    Selection sort     O(n2)              O(n2)
    Heap sort           O(nlog2n)       O(nlog2n)
    Merge sort         O(nlog2n)        O(nlog2n)
    Quik sort            O(n2)              O(nlog2n)
    Sheel sort          O(n(log2n)2)
    where n is elements in array

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Dann?? It is written by Jesse Liberty and there isn't any special chapter about rapid sorting
    Chapter 13: Rapid Sorting Techniques, by Dann Corbit

    We were talking about different books, I was thinking of C Unleashed, not C++ Unleashed. But my point still holds, bubble sort is lame and worthless.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve a maze recursive
    By BENCHMARKMAN in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2006, 11:33 PM
  2. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  3. How Do You Solve Problems
    By manofsteel972 in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 12-04-2004, 10:25 AM
  4. ^^ help me solve the error
    By skwei81 in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2003, 09:04 AM
  5. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM