Question

How do I create an almost sorted array with the size of 1,000 using numbers 1-10,000...

How do I create an almost sorted array with the size of 1,000 using numbers 1-10,000 in JAVA. Almost sorted means the last two elements are swapped or every 10th element is random. For example my random array looks like this:            

            int[] ranArr1000 = new int [1000];
           for (int i = 0; i < ranArr1000.length; i++) {
               ranArr1000[i] = (int)(Math.random() * 10000 + 1);
           }

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Arrays;
import java.util.Random;

public class AlmostSorted {

    public static void main(String[] args) {
        Random random = new Random();
        int[] arr = new int[1000];
        // create array
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(10000) + 1;
        }
        // sort the array
        Arrays.sort(arr);
        // make array almost sorted
        for (int i = 0; i < arr.length; i += 10) {
            arr[i] = random.nextInt(10000) + 1;
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
How do I create an almost sorted array with the size of 1,000 using numbers 1-10,000...
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
  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    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...

  • Create a program with threads that looks through a vary large array (100,000,000 elements) to find...

    Create a program with threads that looks through a vary large array (100,000,000 elements) to find the smallest number in that array. You should track the current lowest value seen in a single, shared value. You should also track the time taken, comparing the amount of time for 1, 2, 4 and more threads. Fairly precise timing can be obtained by using the System.nanoTime() method. For example, it's taking my poor computer over 1 second to fill my array with...

  • Write a program in which you create an array of random real numbers with a size...

    Write a program in which you create an array of random real numbers with a size of 10001. The values should fall between 900 and 1000. Using the provided statistics library, calculate all statistical values represented by the functions in the library and output the results. The median value can only be correctly found after the array is sorted. After sorting using the selection sort method, search for the median value using both the linear search and binary search methods....

  • Hello, Using HTML/Javascript, can somebody help me create an array and fill with random numbers 1-100,...

    Hello, Using HTML/Javascript, can somebody help me create an array and fill with random numbers 1-100, per the number entered in the "Create Array" box of my html, and have it display when the "Create Array" box is clicked? Here is the HTML for this portion: Thank you! <table style="width: 850px; height: 154px;" border="0"> <tbody> <tr> <td>Enter Array Size:<input style="width: 50px;" type="textbox" value="100" /><input type="button" value="Create Array" /></td> </tr> This is what the HTML looks like: Enter Array Size:100 Create...

  • PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is,...

    PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array. Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should...

  • How do i do a random generated numbers for an array that genrates nothing but char...

    How do i do a random generated numbers for an array that genrates nothing but char by using ascii from 33 - 126 example of a function i have to make the array full of char. This way does not work and need help. void displayArray(char arr[], int size) { srand(time(NULL)); for(int i = 0; i < size; i++) { arr[i] = ( (rand() % 33) + 'a'); cout << arr[i] << " "; } }

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • Java 1. Create a 1D array of 4096 Unique random integer numbers of 1 to 5...

    Java 1. Create a 1D array of 4096 Unique random integer numbers of 1 to 5 digits. Of those numbers give me the following information: - Mean , Mode, Median, Range. - Five times, ask the user for a number within the range (from above) and record the time to find the number in the range. (Loop count) 2. Using a any sort algorithm, build sorted 2D array of random numbers (1 TO 5 digits ) with the x direction...

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