PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Re: splitting an array.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: splitting an array.

Réponse
 
LinkBack Outils de la discussion
Vieux 25/05/2008, 09h28   #1
MJ_India
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting an array.

On May 25, 3:21pm, pereges <Brol...@gmail.com> wrote:
> I've an array :
>
> {100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25}
>
> I want to split it into two different arrays such that every number <=
> 50 goes into left array and every number > 50 goes into right array.
> I've done some coding but I feel this code is very inefficient:
> . . .
> I'm really not comfortable with running similar for loops two times.
> Is this bad programming ?


1. Take startIndex = 0, endIndiex = sizeof(array) - 1;
2. Perform steps 2.1 and 2.2 in loop while startIndex < endIndex
2.1 if array[startIndex] <= 50, startIndex++
2.2 else exchange(array + startIndex, array + (endIndex++))
3. left = array, right = array + endIndex

Implementation is left to you. Moreover this is more an algorithm
question than a C question. I am afraid it was asked in wrong forum.
  Réponse avec citation
Vieux 25/05/2008, 12h27   #2
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting an array.

MJ_India <mail.mohitjain@gmail.com> writes:

> On May 25, 3:21Âpm, pereges <Brol...@gmail.com> wrote:
>> I want to split it into two different arrays such that every number <=
>> 50 goes into left array and every number > 50 goes into right array.

<snip>
> 1. Take startIndex = 0, endIndiex = sizeof(array) - 1;
> 2. Perform steps 2.1 and 2.2 in loop while startIndex < endIndex
> 2.1 if array[startIndex] <= 50, startIndex++
> 2.2 else exchange(array + startIndex, array + (endIndex++))


Presumably you intended to write endIndex--.

> 3. left = array, right = array + endIndex


--
Ben.
  Réponse avec citation
Vieux 26/05/2008, 12h08   #3
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting an array.

MJ_India wrote:
> On May 25, 3:21 pm, pereges <Brol...@gmail.com> wrote:
>> I've an array :
>>
>> {100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25}
>>
>> I want to split it into two different arrays such that every number
>> <= 50 goes into left array and every number > 50 goes into right
>> array. I've done some coding but I feel this code is very
>> inefficient: . . .
>> I'm really not comfortable with running similar for loops two times.
>> Is this bad programming ?

>
> 1. Take startIndex = 0, endIndiex = sizeof(array) - 1;
> 2. Perform steps 2.1 and 2.2 in loop while startIndex < endIndex
> 2.1 if array[startIndex] <= 50, startIndex++
> 2.2 else exchange(array + startIndex, array + (endIndex++))
> 3. left = array, right = array + endIndex
>
> Implementation is left to you. Moreover this is more an algorithm
> question than a C question. I am afraid it was asked in wrong forum.


As specified it seemed the OP wanted two new arrays from the original data.

In this case, because the sizes are initially unknown, then it does become a
C question: how to allocate arrays without doing an extra pass through the
data. These little details are important in C, and once sorted the solution
is likely to be one of the fastest.

Otherwise the solution is trivial in some languages. Here's one I've just
tried:

data:=(100,20, -45, -345, -2, 120, 64, 99, 20, 15, 0, 1, 25)

a:=b:=()
forall x in data do
(x<50 | a | b) &:=x
end

println "Left =",a
println "Right =",b

And many will do it in one line I'm sure ("K" probably in a single
expression).

(I also ignored the new array requirement in my other post, but I'd also
missed the fact that everyone else also made use of sorting. Never mind..)

--
Bartc



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10680 seconds with 11 queries