What algorithm is that?

Asked

Viewed 259 times

3

Knowing that: [] denotes an array [A|B] extracts the first element of the array in A and the rest of the array in B [X, Y] ++ [Z] creates a concatenated array [X, Y, Z] [ X || X <- [1, 2, 3, 4], X > 2] returns the array [3, 4] (read: create a array with the following values [1, 2, 3, 4] which are larger than 2)

What is the name of this algorithm?

bla([A|B]) -> bla([ X || X <- B, X < A]) ++ [A] ++ bla([ X || X <- B, X >= A]);
bla([]) -> [].
  • 2

    This is real java?

  • this is a problem related to java, I did not understand very well what I should answer in "What is the name of this algorithm?" but this problem is more connected to logic than to java

  • 2

    @diegofm I think this missing "context", the question should be part of a whole thing, the way this really has no way to answer. Celina if you can confirm if there is such a context could help

  • Celina there is more information about this exercise, like the context or something else that can make it clearer?

  • no, there is no information, only this context was passed.

  • maybe the shape I wrote is a little confusing. [ ] denotes an array. [A|B] extracts the first element of the array in A and the rest of the array in B. [X, Y] ++ [Z] creates a concatenated array [X, Y, Z]. [ X || X <- [1, 2, 3, 4], X > 2] returns array [3, 4]. (read: create array with values contained in array [1, 2, 3, 4] that are greater than 2)

  • This looks like a pseudo-code, had some exercise about how this code style works?

  • No, this question is a Challenge, where it contains steps, these steps have exercises of each genre, I solved one of html code, another of programming in java and this one here. The exercises are given without any guidance, just been given what this in this question (Knowing that [] ...).

  • @Celinashigetomi but the teacher gave no guidance at any time of pseudo code style, type || means one thing, <- means something else and etc?

  • then, I had no guidance on this exercise, but in my academic life I see them as operators (my vision according to the java syntax). | must be "or". a<b = less than b. ++ ="increment" and so on (according to java syntax). But I might be looking at the expressions wrong.

  • I thought, to start: let’s create an array with values contained in array [1, 2, 3, 4] that are greater than 2. So, would I use only values 3 and 4? And then put 3 in array A and 4 in B?

  • 2

    This sounds like a sort algorithm. Something like mergesort or quicksort.

  • Friend, you solved my problem, I typed "quicksort" and it directed me to the next challenge. I would never guess this, it was just a name. My God! Thank you

  • Accurate guess, rs.

  • 3

    That sounds like job interview challenge to me

  • 1

    @Guilhermenascimento This code is Erlang. I put it working on ideone: http://ideone.com/miMswx - In fact it makes a quicksort.

  • @Victorstafusa a great addition.

Show 12 more comments

1 answer

2


As far as I can tell, this algorithm is Quick Sort.

My implementation in Haskell is quite similar to yours, although the statement did not make it clear in what language you are working. An example of recursive code (Haskell language):

quicksort [] = []
quicksort (x:xs) = quicksort menor ++ (x : quicksort maior)
  where menor = [y | y <- xs, y <= x]
    maior = [y | y <- xs, y > x]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.