Sign up ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

There is an array of integer values to be sorted.During the sorting process, the places of two numbers can be interchanged. Each interchange has a cost, which is the sum of the two numbers involved.

You must write a program that determines the minimal cost to sort the sequence of numbers.

share|improve this question

closed as off-topic by Martin Büttner, Jakube, es1024, Timtech, ProgramFOX Mar 7 at 16:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions without an objective primary winning criterion are off-topic, as they make it impossible to indisputably decide which entry should win." – Martin Büttner, Jakube, es1024, Timtech, ProgramFOX
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
First of all please read the faq. Your puzzle should have an objective primary winning criterion. Have a look around to see many well-specified questions. You may also read the comments below your last puzzle as those also address the same questions. –  Howard May 6 '12 at 8:03
2  
We *must*? You should change site... –  rubik May 6 '12 at 12:04
    
Are the integers equally distributed, or normal distributed, or somehow else? How many of them and how big? Do you have some sample input, something like for i in {1..100}; do echo $((RANDOM%256)); done for example. –  user unknown May 27 '12 at 21:20
    
They are not equally distrubuted –  Bhavik Ambani May 28 '12 at 4:31

1 Answer 1

up vote 7 down vote accepted

Scala:

def minimalCostToSortArray (daArray: Array[Int]) = 0

Why is the cost always zero? Because I don't change numbers at all:

def arrsort (a: Array[Int], pos:Int = 0): Array[Int] = {
  if (pos >= a.length -1) a else { 
    if (a(pos) > a(pos+1)) {
      val diff = a(pos) - a(pos+1)
      a (pos) -= diff
      a (pos+1) += diff
      arrsort (a)
    } else arrsort (a, pos+1) 
  } 
}
share|improve this answer
3  
For the challenge to make sense at all, I'd interpret it as saying that the only operation we're allowed to use to modify the array is swapping two numbers. Still, +1 for thinking outside the box (and -1 for cheating, which leaves the total at ±0). –  Ilmari Karonen May 6 '12 at 22:34
1  
I read ... the places can be interchanged as optional, but more so, as badly prepared question. I understand my answer more as a criticism on a badly prepared question. –  user unknown May 6 '12 at 22:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.