Question

JAVA plz Program 0 (50%): Why – just why? One of the least useful sorts that...

JAVA plz

Program 0 (50%): Why – just why? One of the least useful sorts that professors never like to talk about (because it’s SOOO inefficient and never used in practice) has two phases: 1) it shuffles the numbers in the array and 2) checks to see if they are in ascending order. Why is this so bad? Because there are n! ways to order an array with n elements. For example, if there are 100 elements in the array, n == 100 and there are 100*99*98*97*…*5*4*3*2*1 different ways to arrange that! Ridiculous? Yes. That’s why you’re going to code it up. How do you shuffle an array? The easiest way is to traverse each element in the array with a loop, then pick a random element/cell to swap it with. For this assignment, you need to design (pseudocode) a program that 1) initializes and array of 5 elements with random values between 1-50 (i.e. no user input), 2) shuffles the array, 3) prints the array, 4) determines if the elements are in ascending (or non-decreasing) order and 5) repeats steps 2-4 until step 4 is true. Below is an edited sample output, only showing the last few lines of output. Hint: stay sane and use functions. Also, since you need to be able to visualize what’s going on, we’d recommend starting with printing the array to the screen. Note: the random number generator below

Sample run 1:

|34|44|28|33|29|

|33|28|29|34|44|

|34|29|44|33|28|

|33|44|28|34|29|

|34|28|29|33|44|

|33|29|44|34|28

| |34|44|28|33|29|

|33|28|29|34|44|

|34|29|44|33|28|

|33|44|28|34|29|

|34|28|29|33|44|

|33|29|44|34|28|

|34|44|28|33|29|

|33|28|29|34|44|

|34|29|44|33|28|

|33|44|28|34|29|

|34|28|29|33|44|

|33|29|44|34|28|

|34|44|28|33|29|

|33|28|29|34|44|

|34|29|44|33|28|

|33|29|28|44|34|

|44|29|34|28|33|

|28|29|33|34|44|

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// LazySort.java

public class LazySort {

      // method to check if an array is sorted (in asc order)

      static boolean isSorted(int arr[]) {

            // looping from index 1 to length-1

            for (int i = 1; i < arr.length; i++) {

                  // if previous element is bigger than current element, array is not

                  // in sorted order, returning false

                  if (arr[i - 1] > arr[i]) {

                        return false;

                  }

            }

            // if all numbers are in sorted order, returning true

            return true;

      }

      // method to print elements of an array

      static void printArray(int arr[]) {

            for (int i = 0; i < arr.length; i++) {

                  System.out.print("|" + arr[i]);

            }

            System.out.println("|");

      }

      // method to shuffle elements of an array

      static void shuffle(int arr[]) {

            // looping for array's length number of times

            for (int i = 0; i < arr.length; i++) {

                  // generating a random valid index between 0 and length-1

                  int randIndex = (int) (Math.random() * arr.length);

                  // swapping elements at indices randomIndex and i

                  int temp = arr[randIndex];

                  arr[randIndex] = arr[i];

                  arr[i] = temp;

            }

      }

      public static void main(String[] args) {

            // creating an array of 5 integers, and filling the values

            int arr[] = new int[5];

            for (int i = 0; i < arr.length; i++) {

                  // generating and assigning integer between 1 and 50 to current

                  // location

                  arr[i] = (int) (Math.random() * 50) + 1;

            }

            // printing array

            printArray(arr);

            // looping until the array is in sorted order

            while (!isSorted(arr)) {

                  // shuffling array

                  shuffle(arr);

                  // printing array

                  printArray(arr);

            }

      }

}

/*OUTPUT*/

|9|35|48|37|32|

|9|37|48|35|32|

|48|32|9|37|35|

|37|32|35|9|48|

|9|32|37|48|35|

|48|37|9|35|32|

|32|37|48|9|35|

|9|48|32|37|35|

|48|35|37|9|32|

|37|9|48|32|35|

|37|48|32|9|35|

|37|32|9|35|48|

|32|9|35|48|37|

|35|32|9|37|48|

|37|32|9|35|48|

|32|48|37|9|35|

|48|37|9|32|35|

|9|35|37|48|32|

|37|32|48|35|9|

|48|32|9|37|35|

|35|32|37|48|9|

|9|35|48|32|37|

|32|35|9|37|48|

|32|48|37|35|9|

|37|35|32|48|9|

|9|48|37|32|35|

|48|32|9|35|37|

|48|35|9|37|32|

|48|9|37|32|35|

|37|48|9|32|35|

|9|37|48|35|32|

|35|32|37|9|48|

|32|48|9|35|37|

|32|48|9|35|37|

|37|35|9|48|32|

|35|48|32|37|9|

|32|9|35|37|48|

|9|32|37|48|35|

|32|35|9|48|37|

|32|9|35|48|37|

|9|32|48|37|35|

|9|48|32|35|37|

|48|35|37|32|9|

|48|35|32|37|9|

|35|32|9|48|37|

|48|37|35|32|9|

|32|48|35|37|9|

|32|48|37|35|9|

|9|32|35|37|48|

Add a comment
Know the answer?
Add Answer to:
JAVA plz Program 0 (50%): Why – just why? One of the least useful sorts that...
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
  • Need in C# Program 0 (30%): Why – just why? One of the least useful sorts...

    Need in C# Program 0 (30%): Why – just why? One of the least useful sorts that professors never like to talk about (because it’s SOOO inefficient and never used in practice) has two phases: 1) it shuffles the numbers in the array and 2) checks to see if they are in ascending order. Why is this so bad? Because there are n! ways to order an array with n elements. For example, if there are 100 elements in the...

  • Please answer in Java for an immediate upvote :) Given the following array of 8 elements,...

    Please answer in Java for an immediate upvote :) Given the following array of 8 elements, trace the merge sort algorithm. Assume the array is to be sorted in ascending order.                        81          16          4        6             34          11          23        67 ANSWER: (Hint 6 – lines): Why don’t we select the median element of all the n-entries in the array to be our pivot point? ANSWER:

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • An m×n array A of real numbers is a Monge array if for all i,j,k, and l such that 1≤i<k≤m and ...

    An m×n array A of real numbers is a Monge array if for all i,j,k, and l such that 1≤i<k≤m and 1≤j<l≤n , we have >A[i,j]+a[k,l]≤A[i,l]+A[k,j]> In other words, whenever we pick two rows and two columns of a Monge array and consider the four elements at the intersections of the rows and columns, the sum of the upper-left and lower-right elements is less than or equal to the sum of the lower-left and upper-right elements. For example, the following...

  • C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with...

    C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be...

  • Written in Java Your job is to produce a program that sorts a list of numbers...

    Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...

  • Write a C or C++ program A6pc(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusiv...

    Write a C or C++ program A6pc(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string (‘A’<->’Z’, ‘B’<->’Y’, ‘C’<->’X’, etc). You should divide this conversion task among the n threads as evenly as possible. Print out the string both before...

  • c++. please show screenshots of output This project will continue to familiarize the student with using...

    c++. please show screenshots of output This project will continue to familiarize the student with using arrays. Store all the function proto-types in project11_funch and store all the functions in project11_func.cpp. Make sure you have proper header comments in each.h,.cpp file. When you run your program, your sample output should show your name. Your report should include the following: 1) project11_func.h 2) project11_func.cpp 3) project11.cpp 4) sample output Write the following functions: a) void fill_array_with_random_nums(int array(), int N): Fill array...

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
ADVERTISEMENT