HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

Is there a better way to return values with merge sort?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mergereturnwithwaybettervaluessortthere

Problem

I have been attempting to learn Scala lately and so have produced a couple of different sorting algorithms to build up my basics.

I think I have been lucky with the methods returning exactly what I have wanted, but for MergeSort I have implemented recursive methods that could return one of two different values and could logically be placed into two different values of the same type (as I believe Scala does)

In order to get my program to sort properly I have put a temp variable in order to grab the returning value and then be able to use it.

Is there a better way to return the values that will not need me to create these temporary values or should I have been doing something else?

Example:

def merge(left: ArrayBuffer[Int], right: ArrayBuffer[Int]): ArrayBuffer[Int]


returns an ArrayBuffer, how does the scala language figure out which value to put the returned value into or does it need a variable on the returning end to capture the value that is being returned?

merge(left, right)


This does nothing while,

var tempResult = merge(left, right)


Solves the problem.

Am I doing something wrong in the first instance (due to my experience with single parameter methods returning to the proper variable), or is this the standard way to return the values, by returning to a variable?

MergeSortClass.scala

import scala.collection.mutable.ArrayBuffer
class MergeSortClass {

def mergeSort(array: ArrayBuffer[Int]): ArrayBuffer[Int] = {

if (array.size <= 1) {

  array

} else {

  var left = ArrayBuffer[Int]()
  var right = ArrayBuffer[Int]()
  var middle = array.size / 2

  for (i <- 0 until middle) {

    left += array(i)

  }

  for (i <- middle until array.size) {

    right += array(i)

  }


//-->>>TheseValues Below

```
var leftTemp = mergeSort(left);
var rightTemp = mergeSort(right);

var result = merge(leftTemp, rightTemp)

result

}

}

def merge(left: ArrayBuffer[Int], right: ArrayBuffer[Int]): ArrayBuffer[Int] = {

var result

Solution

The expression merge(x,y) will always return a value. val r = merge(x,y) stores the value so that you can use it again.

You could write your code

var leftTemp = mergeSort(left);
var rightTemp = mergeSort(right);

var result = merge(leftTemp, rightTemp)

result


as

merge(mergeSort(left),mergeSort(right))


It is a matter of preference if you wish to use leftTemp and rightTemp like this, but the use of result, and then returning it on the next line is not idiomatic Scala.

You must assign values to an identifier when you want to use them more than once (as you do with temp in your final example). But as I said above you may want to do this for other reasons such as readability and keeping down line lengths.

Code Snippets

var leftTemp = mergeSort(left);
var rightTemp = mergeSort(right);

var result = merge(leftTemp, rightTemp)

result
merge(mergeSort(left),mergeSort(right))

Context

StackExchange Code Review Q#48305, answer score: 2

Revisions (0)

No revisions yet.