package sort;
import java.util.Random;
public class DemoSortingPerformacne {
public static void main(String[] args) {
System.out.println("ArraySize\tSelection sort\t quicksort\t time
taken");
int size=4;
for(int i=0;i<7;i++){
int
ar[]=getRandomNumberArray(size,3);
long startTime =
System.nanoTime();
doSelectionSort(ar);
long endTime =
System.nanoTime();
long timeElapsed
= endTime - startTime;
System.out.print(ar.length+" "+ timeElapsed+" in NanoSec for
Selection sort ");
startTime =
System.nanoTime();
sort(ar, 0,
ar.length-1);
endTime =
System.nanoTime();
timeElapsed =
endTime - startTime;
System.out.print(" "+ timeElapsed+" in NanoSec for Quick
sort");
size*=2;
System.out.println();
}
}
public static int[] getRandomNumberArray(int
arraySize ,int numberOfDigits ){
int ar[]=new int[arraySize];
Random rnd = new Random();
int n = numberOfDigits +
rnd.nextInt(numberOfDigits);
for(int
i=0;i<ar.length;i++){
ar[i]=numberOfDigits + rnd.nextInt(numberOfDigits);
}
return ar;
}
public static int[] doSelectionSort(int[]
placer){
for (int index=0;index<placer.length;index++)
{
int smallerNumber = placer[index];
//make current element as smallest
int smallerIn=-1; //initialize
smallest index as -1
int i=index; //define i out so that
it remain accessible out side of loop
for(;i<placer.length;i++){ //find smallest element
in ar[i.....n]
if(smallerNumber>placer[i]){
smallerNumber=placer[i];
smallerIn=i;
//remember index of smallest
}
}
if(smallerIn!=-1){ //if smallest found
int temp=placer[index]; //swap it
placer[index]=smallerNumber;
placer[smallerIn] = temp;
}
}
return placer;
}
public static int part(int ar[], int l, int h)
{
int p = ar[l]; //to first element for array as pivote
int j=h; //j as highest
int i = (h+1); //after highest index
while (j>l) //run loop j greater the low
{
if (ar[j]>= p) { //if jth element greater then
p
i--; //swap ith and jth element
int t = ar[i];
ar[i] = ar[j];
ar[j] = t;
}
j--; //go to lower element
}
int t = ar[i-1]; //swap pivot if it is not at right
place
ar[i-1] = ar[l];
ar[l] = t;
return i-1;
}
public static void sort(int ar[], int l, int h)
{
if (l < h)
{
//using divide and concor
int pi = part(ar, l, h); //find pivote index
sort(ar, l, pi-1); //sort array before pivot
sort(ar, pi+1, h); //after pivot
}
}
}
output
ArraySize Selection sort
quicksort time taken
4 3775 in NanoSec for Selection sort 6797 in NanoSec for Quick
sort
8 2265 in NanoSec for Selection sort 3776 in NanoSec for Quick
sort
16 4908 in NanoSec for Selection sort 14348 in NanoSec for Quick
sort
32 13970 in NanoSec for Selection sort 27563 in NanoSec for Quick
sort
64 45687 in NanoSec for Selection sort 91374 in NanoSec for Quick
sort
128 167268 in NanoSec for Selection sort 232212 in NanoSec for
Quick sort
256 654347 in NanoSec for Selection sort 137817 in NanoSec for
Quick sort
Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array...
Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, create a random integer array of size arraySize (15 OR LESS) with elements from the the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} (integers can appear multiple times in this array, has two accessor methods: public int getArraySize() that will...
Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. Create a second class called CompareArraysTest that contains...
Create a class called Reverse that reverses an array of integers. The class should have two method: - public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped. - private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time,...
Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer { // Please expand method main() to meet the lab requirements. // You have the following sorting methods available: // insertionSort(int[] a); // selectionSort(int[] a); // mergeSort(int[] a); // quickSort(int[] a); // The array will be in sorted order after the routines are called! ...
array function • Create a function called show2D. • This function should accept a two-dimensional array as parameter and display its contents that are odd numbers on the screen. • This function should work with any of the following arrays of different sizes: int hours[3][9]; int stamps[7][9]; int cities[15][9]; • Write a demo program to show how to use the function show2D. this is c++ program Task 2: Array functions • Create a function called show2D. • This function should...
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...
Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...
in java Create an Eclipse Java project that utilize "Selection Sort" to sort an unsorted integer array of size 20 of random numbers in the range 0-500 inclusive. The main method should display the unsorted array and sorted array after Selection Sort.
please help fill out the class menu create a prototype that will display information about housing data1. In this prototype you will use dynamic array data structures to store the data and compute metrics. For the requirements of the prototype, your client has given you a list of metrics and operations. The user interface will be a menu that displays this list and prompts the user to choose which option to execute.You will create two classes. First you will create...
Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...