Sorting algorithm essay

The Array class is highly similar with the one which you have learnt in text book (Chapter 2). It now has some more methods. The randomly() method is used to generate an array of random long integer values. Public void randomly(into implements) { // create a random machine Random random = new Random(); unless = implements; for (into I = O; I < nElems ; i++) // assign a random long integer value / to current element of the array a[i] ? aRandom. extLong() % 100000000; The method uses Random class in package java. util, so you need to import it first before using. After creating a random machine, you can generate random long integer values by calling its nextLong() method. You can refer to http://download. oracle. com/javase/6/docs/api/ for more information about class Random. c. To generate an array of random long integer values, you first create an Array object and then call its randomlnit() method as following (in SortingApp. java): int maxSize = 1 0000; // array size Array arr; // reference to array arr = new Array(maxSize); // create the array arr. andomlnit(maxSize); // generate random array's elements d. The above code segment creates an array of 1 0000 random elements. Then, you need to apply some sorting methods on it. To call it, simply use the Array object (named arr as above) and call its bubblesort() method (newly added code segment is in bold): int maxSize = 10000; // array size arr. randomlnit(maxSize); // generate random array's elements arr. bubbleSort(); // bubble sort them e. Now you can sort an array of 10000 random values. You will need to measure time (in milli-seconds) which bubble sorting method takes to run.

The static method now in class Timeless is used for this purpose. It returns number of mill-seconds since epoch. You can refer to http:// download. Oracle. Com/Java’s/6/docs/API/ for more information about how to use Calendar class. Public static long now() Calendar cal = Calendar. Assistance(); Date currently = cal. Getting(); return currently. Getting(); f. The code in Sorting is now modified to measure time needed by Bubble sorting method. into maximizes = 1 0000; // array size or new Array(maximizes); // create the array ear. Indolent(maximizes); // generate random array’s elements long starriest, meantime; // get time just before running sorting starriest = Timeless. Now(); // get time just after running sorting meantime = Timeless. Now(); // time needed in mill-seconds duration = meantime – starriest; ” + duration + “ms”); g. Until now, hopefully you can understand the way we measure running time of Bubble sort. Your task now is to write selections() and insertions() method in class Array. If you forget them, you can refer to Chapter 3 in textbook.

Note: because after sorting by bubble sort, the array is changed. Thus you need to create a copy of the original array to apply for each sorting method. Copy constructor Array (Array array) can be used to create a copy of the array. To check algorithms code correct or not, you can use display() method. It prints out content of an array. H. Play around your program with different maximizes values and record their running time into a table as mentioned above.

Leave a Reply

Your email address will not be published. Required fields are marked *