1

所以这是代码:

object Main {
def main(args: Array[String]){
  var myArray = Array(5,2,7,6,8,1,15,5/*,9,10,56*/)
  var startSorting = new Starter(myArray)
  startSorting.start
  startSorting ! Begin
  var i = 0
  for( i <- 0 to myArray.length - 1){
   println(myArray(i))
  }
}
 }

 import scala.actors.Actor
 import scala.actors.Actor._

 abstract class SortArray 
 case object Sort extends SortArray
 case object FinishedSubArraySorting extends SortArray
 case object Begin extends SortArray

 class Starter(toBeSorted :Array[Int]) extends Actor{
   def act(){
     var first:Array[Int] = Array()
     var second:Array[Int] = Array()
    loop{
      react{
    case Begin =>
      var SortActor = new MergeSort(toBeSorted)
      SortActor.start
      SortActor ! Sort
    case sortedArray :Array[Int] =>
         var i = 0
         println("Sortat:")
         for( i <- 0 to sortedArray.length - 1){
             println(sortedArray(i))
         }
      }
    }
   }
 }

 class MergeSort(toBeSorted :Array[Int]) extends Actor{
   def act(){
  var finishedSorting = 0
  var thisArray = toBeSorted
  var first:Array[Int] = Array();
  var second:Array[Int] = Array();
  var sortedSubArrays = 0;
  loop{
    react{
      case Sort =>
        if(thisArray.length == 1){
          finishedSorting = 1
          sender ! thisArray
          exit('stop)
        }else{
          first = thisArray.slice(0,thisArray.length/2)
          second = thisArray.slice(thisArray.length/2,thisArray.length)
          var firstSort = new MergeSort(first)
          var secondSort = new MergeSort(second)
          firstSort.start
          secondSort.start
          firstSort ! Sort
          secondSort ! Sort
        }
      case subSortedArray:Array[Int] =>
        sortedSubArrays = sortedSubArrays + 1
        if(sortedSubArrays == 1){
            first = subSortedArray
        }else{
          second = subSortedArray
          thisArray = merge(first,second)
          finishedSorting = 1
          sender ! thisArray
          exit('stop)
        }
    }
  }
   }
   def merge(firstArray :Array[Int],secondArray :Array[Int]):Array[Int] = {

var result:Array[Int] = new Array[Int](firstArray.length + secondArray.length)
var i = 0
var j = 0
var k = 0
while(i < firstArray.length && j < secondArray.length){
  if(firstArray(i) <= secondArray(j)){
    result(k) = firstArray(i)
    i = i + 1
  }else{
    result(k) = secondArray(j)
    j = j + 1
  }
  k = k + 1
}
while (i < firstArray.length)
{
    result(k) = firstArray(i)
    i = i + 1
    k = k + 1
}
while (j < secondArray.length)
{
    result(k) = secondArray(j)
    j = j + 1
    k = k + 1
}
return result
 }
}

它使用 2 个参与者:一个用于开始和结束输出,另一个将具有多个实例来合并排序数组和子数组。首先调用一个实例!Sort,以便将数组一分为二,并为每个子数组创建两个参与者,然后也调用一个实例,然后调用我前面提到的两个参与者中的每一个的结果排序子数组。

这段代码的问题在于它只能在最低级别(最大长度为 2 的子数组)进行排序和合并,然后由于某些原因,参与者调用的发送者与调用它们的发送者不同,因此不再发生任何事情。

4

1 回答 1

1

您的问题是,当 MergeSort 演员从他们的孩子那里收到子列表时,他们会将合并的列表发送给发件人 - 这将是他们的孩子而不是父母。

我向 MergeSort 添加了一个父字段,并将发送更改为使用此父字段 - 这是修改后的代码

object Main extends App {

  var myArray = Array(5,2,7,6,8,1,15,5/*,9,10,56*/)
  var startSorting = new Starter(myArray)
  startSorting.start
  startSorting ! Begin
  var i = 0
  for( i <- 0 to myArray.length - 1){
   println(myArray(i))
  }

 }

 import scala.actors.Actor
 import scala.actors.Actor._

 abstract class SortArray 
 case object Sort extends SortArray
 case object FinishedSubArraySorting extends SortArray
 case object Begin extends SortArray

 class Starter(toBeSorted :Array[Int]) extends Actor{
   def act(){
     var first:Array[Int] = Array()
     var second:Array[Int] = Array()
    loop{
      react{
    case Begin =>
      var SortActor = new MergeSort(toBeSorted,self)
      SortActor.start
      SortActor ! Sort
    case sortedArray :Array[Int] =>
         var i = 0
         println("Sortat:")
         for( i <- 0 to sortedArray.length - 1){
             println(">"+sortedArray(i))
         }
      }
    }
   }
 }

 class MergeSort(toBeSorted :Array[Int],parent:Actor) extends Actor{
   def act(){
  var finishedSorting = 0
  var thisArray = toBeSorted
  var first:Array[Int] = Array();
  var second:Array[Int] = Array();
  var sortedSubArrays = 0;
  loop{
    react{
      case Sort =>
        if(thisArray.length == 1){
          finishedSorting = 1
          println(this + " sending up "+thisArray.mkString("[",",","]"))
          parent ! thisArray
          exit('stop)
        }else{
          first = thisArray.slice(0,thisArray.length/2)
          second = thisArray.slice(thisArray.length/2,thisArray.length)
          var firstSort = new MergeSort(first,self)
          var secondSort = new MergeSort(second,self)
          firstSort.start
          secondSort.start
          firstSort ! Sort
          secondSort ! Sort
        }
      case subSortedArray:Array[Int] =>
        println(this + " received " + subSortedArray.mkString("[",",","]"))
        sortedSubArrays = sortedSubArrays + 1
        if(sortedSubArrays == 1){
            first = subSortedArray
        }else{
          second = subSortedArray
          thisArray = merge(first,second)
          finishedSorting = 1
          parent ! thisArray
          exit('stop)
        }
    }
  }
   }
   def merge(firstArray :Array[Int],secondArray :Array[Int]):Array[Int] = {

var result:Array[Int] = new Array[Int](firstArray.length + secondArray.length)
var i = 0
var j = 0
var k = 0
while(i < firstArray.length && j < secondArray.length){
  if(firstArray(i) <= secondArray(j)){
    result(k) = firstArray(i)
    i = i + 1
  }else{
    result(k) = secondArray(j)
    j = j + 1
  }
  k = k + 1
}
while (i < firstArray.length)
{
    result(k) = firstArray(i)
    i = i + 1
    k = k + 1
}
while (j < secondArray.length)
{
    result(k) = secondArray(j)
    j = j + 1
    k = k + 1
}
return result
 }
}
于 2013-06-04T14:16:50.310 回答