title: Efficient Way to take first n sorted elements in Spark Partitions
body: <p>I have created an RDD from array in Spark. I want to take n smallest elements from on each partition.  I have sorted iterator at each partition every time and take first n elements and replaces them with elements of arr1. The way i have done is </p>
  
  <pre><code> var arr = (1 to 50000).toArray
   val n = 50
   val iterations = 100  
   val r = new Random() 
   val arr1 = Array.fill(n)(r.nextInt(10)) 
  
   val rdd = sc.parallelize(arr,3)
   rdd.mapPartitionsWithIndex{(index , it) =&gt;  
   it=it.sortWith(_&lt;_)
  
    for(i&lt;- 0 until n){
     it(i) = arr1(i)   
    }
    it
   }
  </code></pre>
  
  <p>I want to ask is there any efficient way to perform same task in Scala</p>