Java Parallel Array Sorting Example
package
java8;
import
java.util.Arrays;
public class
ParallelArraySortDemo {
public static void
main(String[] args) {
int x[]=
{9,2,3,6,5,1};
System.out.println("Before
Sorting");
for(int s:x)
{
System.out.println(s+"
");
}
Arrays.parallelSort(x);
System.out.println("After
Sorting");
for(int s:x)
{
System.out.println(s+"
");
}
}}
o/p:-------------------------------------
Before Sorting
9
2
3
6
5
1
After Sorting
1
2
3
5
6
9
package java8;
import java.util.Arrays;
public class JavaParallelArraySortDemo1 {
public static void main(String[] args) {
int x[]= {9,7,8,3,4,5,2,1,6,-2};
System.out.println("--------before sorting---------------");
for(int s:x)
{
System.out.println(s);
}
// Sorting array elements parallel and passing start, end index
Arrays.parallelSort(x,0,5);//last index-1
System.out.println("--------after sorting (0 to 4 element sort only)---------------");
for(int s:x)
{
System.out.println(s);
}
}
}
--------before sorting---------------
9
7
8
3
4
5
2
1
6
-2
--------after sorting (0 to 4 element sort only)---------------
3
4
7
8
9
5
2
1
6
-2
No comments:
Post a Comment