Question: Java program reflecting instructions given: Write a function called randomSort which performs a sort of an integer rarray by doing random swaps until it is

Java program reflecting instructions given:

Write a function called randomSort which performs a sort of an integer rarray by doing random swaps until it is in ascending order.

Use the method isSorted() to check if the array is sorted.

The function should return the number of swaps done.

For example a call of:

int a[]={10,3,1,4,9,23,13,101,3,2,1,9};

int count=randomSort(a);

System.out.printf("Count of %s is %d ",Arrays.toString(a), count);

would produce an output similar to:

Count of [1, 1, 2, 3, 3, 4, 9, 9, 10, 13, 23, 101] is 33999865

you can use this method to test if the array is sorted:

public static boolean isSorted(int a[]) { for (int i=0; i

if (a[i]>a[i+1]) return false;

return true;

}

Write a program which calls randomSort with arrays of random numbers of size 3-10. Produce a plot of the size of the array versus the number of swaps averaged over 10 runs.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!