0

I am trying to figure out how to sort a given array without using PHP's built in sort functions. I know that this is much harder but I am not allowed to use the sort functions. I know I have to use loops just not exactly sure how to go about it.

7
  • sounds like homework? What are the values? Please edit your question and add some example data Commented Nov 7, 2011 at 17:35
  • 4
    Might as well start with the simplest sort function there is: en.wikipedia.org/wiki/Bubble_sort Commented Nov 7, 2011 at 17:36
  • Is this a homework? If so, you should say that. Commented Nov 7, 2011 at 17:36
  • Since you say you're "not allowed" to use sort functions, I'm assuming this is some sort of homework assignment. Others might be willing to give you code, but my suggestion would be to try it yourself. You'll learn more. I will give you a hint, though. Imagine how you look through a dictionary or phone book. Does the word come before or after the page you're on? If so, move it up in the list. If not, move it down. Commented Nov 7, 2011 at 17:36
  • You could use the good old bubble-sort: phptutorialonline.com/php-bubble-sort.aspx Commented Nov 7, 2011 at 17:36

3 Answers 3

1

Here is a simple sort to get you started: http://en.wikipedia.org/wiki/Bubble_sort

If you want the names of different sorts: http://en.wikipedia.org/wiki/Sorting_algorithm

0

You might want to take this approach:

  • Create a new, empty array.
  • Inside a while loop, repeatedly look through the existing array. At each step, look for the next array element to be chosen.
  • When you identify it, delete it from the old array and add it to the new array.
  • Repeat until the original array is empty.
  • Return your new array.

Since this sounds like a homework exercise, I leave it to you to do the work of turning this outline into working code.

0

Might as well look into the fastest sort right away: Quicksort

As it seems like homework, make sure you implement it yourself and really understand what's happening (also make sure you understand why it's so fast and efficient). It's also a good introduction to divide-and-conquer approaches to problem solving.

The wikipedia pseudo code should help you get started, good luck!

4
  • 1
    It seems silly to place an emphasis on the efficiency of different sorting algorithms here. This is presumably an exercise to get students to think about how to use loops and how to give the computer instructions to follow; in practice if you wanted to sort things in PHP you would just use one of the standard library functions to do it. If there was a real need to sort lots of things and do it efficiently then to be honest you probably wouldn't use PHP at all. Commented Nov 7, 2011 at 18:05
  • Considering the whole point of sorting is to do it efficiently, I think it's actually a good idea to start trying to understand why one algorithm is better than an other as early as possible. Isn't the whole point of implementing it yourself to understand what's going on? Commented Nov 7, 2011 at 18:15
  • But again, if you were trying to teach how to do things efficiently, why use PHP? Commented Nov 7, 2011 at 18:29
  • 1
    Because it's the algorithm that matters, not the actual practical speed. In a learning context such as this one, the programming language is just a tool to solve the problem (sorting) hands on. Typically, a faster algorithm will run faster than an inherently slower one provided their implemented in the same language regardless of said language. Commented Nov 7, 2011 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.