Question

How to make a thread that sorts 100k random integers in java language Thread #1 -...

  • How to make a thread that sorts 100k random integers in java language
  • Thread #1 - IntegerSorter class - sorts 100k random integers (values ranging from 0-100,000)
0 0
Add a comment Improve this question Transcribed image text
Answer #1
package HomeworkLib1;
import java.util.Random;
public class Thread1 extends Thread{
        int array[];
        Thread1(){                                                                                                      //Constructor that creates the object
                array = new int[100000];                                                                //and initializes the array with random nos
                Random rand = new Random();
                for(int i=0; i<100000; i++) {
                        array[i] = rand.nextInt(100000);
                }
        }
        public void run() {                                                                                     //Sort the array in thread
                System.out.println("Sorting in thread...");
                sort(this.array);
        }
        public static void main(String[] args) throws Exception{
                Thread1 t1 = new Thread1();                                                             //Creating thread
                System.out.println("First 100 elements unsorted :");
                for(int i=0; i<100; i++) {                                                           //Displaying first 100 numbers from random array
                        System.out.print(t1.array[i] + " ");
                        if((i+1) %10 == 0)
                                System.out.println();
                }
                System.out.println();
                t1.start();                                                                                             //Starting the thread to sort the array
                Thread.sleep(500);                                                                              //Force the main thread to sleep so that 
                                                                                                                                //thread sorting is completed
                System.out.println("\nFirst 100 elements sorted :");
                for(int i=0; i<100; i++) {                                                           //Printing 100 sorted elements.
                        System.out.print(t1.array[i] + " ");
                        if((i+1) %10 == 0)
                                System.out.println();
                }
        }
        public static void sort(int arr[])                                                      //Insertion sort
    { 
        int n = arr.length; 
        for (int i = 1; i < n; ++i) { 
            int key = arr[i]; 
            int j = i - 1; 
  
            /* Move elements of arr[0..i-1], that are 
               greater than key, to one position ahead 
               of their current position */
            while (j >= 0 && arr[j] > key) { 
                arr[j + 1] = arr[j]; 
                j = j - 1; 
            } 
            arr[j + 1] = key; 
        } 
    } 
}

Add a comment
Know the answer?
Add Answer to:
How to make a thread that sorts 100k random integers in java language Thread #1 -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT