PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Old November 8th, 2006, 04:57 PM
james65 james65 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: UK
Posts: 3 james65 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sort array without built in functions

Hi everyone.

I've been given a task to do by a friend who wants to know where I'm at with my php (as there is a possibility of a job).

In his words... You are are passed an array of 1000 different alphabetic strings) but unsorted. you are also passed a flag to indicate ascending or descending. Sort the array.

Obviously this would be a piece of cake using PHP's built in functions. However, I'm thinking that would not be much of a challenge. Maybe he wants me to do it without using these functions. Is that possible? Is there a simple way to do so without using asort() or whatever?

Many thanks

James

Reply With Quote
Old November 8th, 2006, 05:57 PM
instigator instigator is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Maryland
Posts: 1,159 instigator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 13 sec
Reputation Power: 9
RE: Sort array without built in functions

if he wants you to reinvent the wheel it's probably not going to be a good experience working for him, lol

just do things in the simplest, most straightforward way possible. there's nothing inherently good about challanging yourself to make things more complicated than they need to be imho.

there are a bunch of sorting algorithms out there:

http://en.wikipedia.org/wiki/Sorting_algorithm


Reply With Quote
Old November 8th, 2006, 06:02 PM
james65 james65 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: UK
Posts: 3 james65 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Sort array without built in functions

lol

My thoughts exactly. It's just that my answer will look something like this: asort($array, $flag). Not too impressive.

Reply With Quote
Old November 8th, 2006, 06:04 PM
instigator instigator is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Maryland
Posts: 1,159 instigator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 13 sec
Reputation Power: 9
RE: Sort array without built in functions

it's more impressive than 20 lines. a good coder does things in the most efficient way possible. i think of re-writing a particular function to work in the exact same way (i.e. without adding any features to it) as it did before is the cardinal sin of coding.

i posted a link to an article with sorting algorithms in the last post if you want to take a look at them.

Reply With Quote
Old November 8th, 2006, 06:05 PM
james65 james65 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: UK
Posts: 3 james65 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Sort array without built in functions

Agreed. Many thanks for your help.

Reply With Quote
Old November 8th, 2006, 06:08 PM
instigator instigator is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Maryland
Posts: 1,159 instigator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 13 sec
Reputation Power: 9
RE: Sort array without built in functions

no problem, let me know if you have any questions actually implementing one of the algorithms if you end up going the hard route

Reply With Quote
Old November 8th, 2006, 11:25 PM
Silver Silver is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 29 Silver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Sort array without built in functions

.........
asort ?????
You mean... there was a function for sorting array???
you mean for the past 3 years there was an easier way to sort arrays??????
well..... damn...

hehe... no actually never used the function much.
How instigator already mentioned, there are a few algorithms out there...
The thing is... What your friend wants you to do is to write EFFICIENT code.
Back in tech university there was a course I had... about half of the semester we were writing search and sort algorithms and then function.
The problem with most search and/or sort algorithms is memory-use.
If you have 1000 elements... and you have to sort them in order... what will you do, find the smallest, then find the second smallest... then find the third smallest ?
Most servers have a dead-time execution of 30 seconds. After 30 seconds your script is terminated prematurely, meaning server never lets it finish its execution because it is being a memory and resource hog.
That's what us developers supposed to learn how to do... but then again... most of my coworkers prefer 1000 pages loading in iframes with JS instead of writing a short and efficient ajax function to generate proper output.
if you want, I'll check the name of the book with all the search and sort algorithms that we used for the class (if I happen to still have it...)

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Sort array without built in functions


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap