|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|