0

"Each of the functions should be run with array inputs, of size 100, 1000 and 10000; where each
value in any array should be an integer from 1 – 1000 inclusive. Each sorting function should be
run on arrays of the following types: random numbers, sorted lists and almost sorted lists"

Below I have created three arrays.

First one fills Array of 10000 randomly with integers from 1-1000.

Second fills array of 10000 with integers from 1-10000. Third Shuffles array of 10000 which include integers from 1-10000.

My problem is I can't get my 2nd and 3rd Array of 10000 to only include values from 1-1000. Is is even possible? I'm new to this. Any help will be appreciated!!

int [] inputTenThousand = new int[10000];               // Random 10000
    for (int a = 0; a < inputTenThousand.length; a++) {
       inputTenThousand [a] = (int) (Math.random () * 1000);
    }



int [] inputTenThousand2 = new int[10000]               // Sorted 10000
    for (int a = 0; a < inputTenThousand2.length; a++) {
       inputTenThousand2[a] = a + 1;
    }




List<Integer> TenThousandList = new ArrayList<Integer>();
    for (int i = 1; i < 10001; i++) {
        TenThousandList.add(i);
    }
     Collections.shuffle(TenThousandList);
int[] inputTenThousand3 = new int[TenThousandList.size()];  // Almost Sorted 10000
    for (int i = 0; i < TenThousandList.size(); i++) {
       inputTenThousand3[i] = TenThousandList.get(i);
    }
    for (int i = 0; i < inputTenThousand3.length; i++) {            
       inputTenThousand3[i] = TenThousandList.get(i);   
    }
user3478869
  • 105
  • 2
  • 6
  • 12

1 Answers1

1

You can come very close using the code you already have, by just adding the modulo operator for the second and third lists. Adding elements "mod 1000" ensures that you have no values in the list greater than 1000. (You have to add one to the resulting values to shift the range up from 0-999 to 1-1000).

inputTenThousand2[a] = (a % 1000) + 1;

Of course, this doesn't preserve the sorted order you created originally, but once you generate these arrays, you'll notice a very clear pattern. Your array is now just the numbers 1-1000, repeated ten times. This makes picturing what the array would look like sorted very easy:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
 ...
 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000] 

So, we can just construct that nice sorted list in the first place:

int [] inputTenThousand2 = new int[10000];     // 10000 sorted integers
for (int v = 0; v < 1000; v++) { // loop from 0 to 999
    for (int i = 0; i < 10; i++) { 
        inputTenThousand2[(10*v) + i] = v + 1; // Set ten elements per value of the outer loop
    }
}

You can then copy this list into a third list and "slightly unsort" it for your third case!


Of course, depending on what you have access to (this looks like an assignment, so maybe you don't have sorting readily available), it's likely to be easier to just create the first list as you have it already, then copy it and sort it for the second case.

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • But the quote I added in my description (which is part of my project) doesn't it says array of size 10000 but 1-1000 integers. I take it as array should have 10000 integers but only from 1-1000 means there will be duplicates. Am i reading it wrong? – user3478869 Apr 01 '14 at 21:43
  • @user3478869 Yes, an array with 10,000 integers between `1` and `1000` will necessarily have duplicates. *On average*, each integer will be repeated ten times in a uniform random distribution. What my suggestion does is formalize that fact by creating a sorted array that conststs of ten 1s, ten 2s, ten 3s... all the way up to ten 1000s. – Henry Keiter Apr 01 '14 at 21:46
  • Thank you sir. That's my other question actually I don't know how would you have a "slightly unsort" array? that's why I just used shuffle. Which I know won't create slightly unsort array but don't know what else to do. – user3478869 Apr 01 '14 at 22:25
  • @user3478869 Glad I could help! As far as what constitutes an "almost sorted" array, you'll have to ask for clarification on what that means from wherever you're getting this task. Probably you can just copy the sorted array and swap any two non-equal elements, and that'll be good enough--that would count as "almost sorted" in my book, anyway! – Henry Keiter Apr 01 '14 at 22:29