import java.util.*;

class ParallelRankSort {
	public static void main(String[] args) {

		// Invalid command line arguments
		if (args.length != 2) {
			System.out.println("ParallelRankSort Use");
			System.out.println("Command line arguments: <problem_size> <number_of_threads>");
			System.out.println("<problem_size>: Number of elements of the array to be sorted.");
			System.out.println("<number_of_threads>: Number of threads to be used in the parallelization.");
			System.out.println("	0: uses the number of available processors as number of threads.");
			System.out.println("e.g. `java ParallelRankSort 10000 8` which will run 8 threads for an array of 10000 elements.");
			System.exit(0);
		}

		// Get command line arguments
		int elemQuantity = Integer.parseInt(args[0]);
		System.out.println("Problem size: " + elemQuantity);
		int threadNum = Integer.parseInt(args[1]);
		if(threadNum == 0) {
			threadNum = Runtime.getRuntime().availableProcessors();
		}
		System.out.println("Thread number: " + threadNum);

		// Initialize arrays
		int[] readArray = new int[elemQuantity];
		int[] resultArray = new int[elemQuantity];

		// Create initial un-sorted array and print it
		for(int i = 0; i < elemQuantity; i++) {
			readArray[i] = elemQuantity - i;
			resultArray[i] = 0;
			if (i % 10 == 0) {
				readArray[i] = 10;
			}
		}

		// // Print initial array
		// System.out.println("\nInitial array:");
		// for(int i = 0; i < elemQuantity; i++) {
		// 	System.out.print(resultArray[i] + " ");
		// 	if (i % 10 == 0) {
		// 		System.out.println();
		// 	}
		// }
		// System.out.println();

		// Start timer
		long start = System.currentTimeMillis();

		// Calculate blocks of operation for threads
		int divNum = elemQuantity / threadNum;

		// Start threads
		ParallelRankThread threads[] = new ParallelRankThread[threadNum];
		for(int i = 0, j = 0; i < threadNum; i++, j += divNum) {
			threads[i] = new ParallelRankThread(readArray, resultArray, threadNum, i);
			threads[i].start();
		}

		// Join threads
		try {
			for(int i = 0; i < threadNum; i++) {
				threads[i].join();
			}
		} catch (Exception e) {
			System.out.println(e);
		}

		// Stop timer
		long elapsedTimeMillis = System.currentTimeMillis() - start;
		float elapsedTimeSec = elapsedTimeMillis / 1000F;

		// // Print sorted array
		// System.out.println("\nFinal array:");
		// for(int i = 0; i < elemQuantity; i++) {
		// 	System.out.print(resultArray[i] + " ");
		// 	if (i % 10 == 0) {
		// 		System.out.println();
		// 	}
		// }
		// System.out.println();

		// Print duration
		System.out.println("Duration: " + elapsedTimeSec + "s");

  }
}

@@ -143,9 +143,9 @@
  != 
-2
+3
 ) %7B%0A
@@ -579,16 +579,150 @@
 rintln(%22
+%3Crandom%3E: Boolean for the use of random data. Use %60true%60 for true. Every other statement evaluates false.%22);%0A%09%09%09System.out.println(%22%5Cn
 e.g. %60ja
@@ -748,16 +748,21 @@
  10000 8
+ true
 %60 which 
@@ -802,16 +802,23 @@
 f 10000 
+random 
 elements
@@ -1164,16 +1164,109 @@
 eadNum);
+%0A%09%09Boolean random = Boolean.valueOf(args%5B2%5D);%0A%09%09System.out.println(%22Random data: %22 + random);
 %0A%0A%09%09// I
@@ -1371,16 +1371,17 @@
 tity%5D;%0A%0A
+%0A
 %09%09// Cre
@@ -1470,94 +1470,168 @@
 %0A%09%09%09
-readArray%5Bi%5D = elemQuantity - i;%0A%09%09%09resultArray%5Bi%5D = 0;%0A%09%09%09if (i %25 10 == 0) %7B%0A%09
+if (random) %7B%0A%09%09%09%09Random rand = new Random();%0A%09%09%09%09readArray%5Bi%5D = rand.nextInt(elemQuantity) + 1;%0A%09%09%09%7D else %7B%0A%09%09%09%09readArray%5Bi%5D = elemQuantity - i;%0A%09%09%09%7D%0A
 %09%09%09re
-ad
+sult
 Arra
@@ -1641,17 +1641,11 @@
 %5D = 
-1
 0;%0A
-%09%09%09%7D%0A
 %09%09%7D%0A

add functionality for random numbers