1

How can I sort a region in an array in Scala?

I have an array, say var DivA = new Array[Int](100005).

I filled the elements in DivA up to some position which is less than 100005. For example my array looks like Diva = {1,2,10,5,15,20}.

In C++ we have sort(DivA, DivA + N), but how can we sort arrays in Scala up to a certain index?

I tried Sorting.quickSort(DivA), but I don't know how to define the index up to which I want to sort the array, so that the statement above sorts the array, and the array looks like DivA={0,0,0,0,0}?

      var Diva=new Array[Int](10000);
      for(j<-15 to 0 by -1){
           DivA(posA)=j;
           posA=posA+1;
      }
      Sorting.quickSort(DivA);

      for(j<-0 to posA-1)
        {
          print(DivA(j));
          print(" ");
        }

4 Answers 4

2

If you want to sort a region in an array, you can use the sort method in java.util.Arrays:

java.util.Arrays.sort(DivA, 0, 15)

this method offers fromIndex and toIndex. Note that this method sorts in place. That's probably OK in your case, as the style you apply seems to be "imperative".

If performance matters, arrays will probably be faster than other collections. Other reasons for using arrays might be memory consumption or interoperability.

Otherwise you can probably design your data structures/algorithm in a different (more "functional") way by using idiomatic Scala collections.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use sorted or sortBy, depending on your needs. In case of simple Array[Int], sorted should be enough:

val arr = Array.iterate(15, 15)(_ - 1)
arr: Array[Int] = Array(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

val sortedArray = arr.sorted
sortedArray: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)

You can provide your own Ordering typeclass if you want some other kind of order.

You can find it in the documentation.

Although, as was noted in other answer, you should prefer immutable Vector unless you have good reason to switch to Array.

Comments

1

sorted() method is common for all collections, and Arrays in Scala are implicitly converted to a collection. So you can call sorted on Array[Int], eg:

scala> val p = (10 to 1 by -1).toArray
p: Array[Int] = Array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

scala> p.sorted
res5: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Comments

0

First, if you are programming in Scala, you should think about Vector or List because those are immutable which is prefferd way of programming in Scala...

And for Vector and List there is sorted() function which sorts an array

1 Comment

i read about list and vector but is there no any way to sort array of integer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.