Hello, I have been taking a cs 2010 class and have finally gotten to start coding in Java. However, my professor cannot speak or write English very well so I am having to learn mostly by myself.
He recently sent us a project in which we have to:
1. Write a program that will use an array to store 10,000 randomly generated
numbers (ranging from 1 to 10,000 with no repeating numbers)
2. Using the selection sort algorithm to sort the number in ascending order, display the sorted sequence, each line showing just one number.
I have discovered that I will need to presumably generate the array with the 10,000 numbers, shuffle them, and then sort them.
However, I am completely lost on how to fill the array with the 10,000 numbers to begin with. I only know how to randomly fill them, but by doing that, I have repeating numbers.
Here is a snippet of what I have tried so far with no clue as to how to go from there. I thought I could write a while loop that would fill each one with (sum = 1+ sum), but I am not sure of the syntax or if that is even possible with arrays. Any help would be oh so helpful.
package array.sorter.project; import java.util.*; import java.util.Arrays; public class Sorting { //generating 10000 spots in the array public static void main(String args[]){ int[] tenthousand = new int[10000]; //loop to add 1 to x and set the new value of x to the array spot int sum = 0; while ( sum < 10000){ sum = sum + 1; Arrays.fill(tenthousand, sum); } for (int i = 0; i < tenthousand.length; i++) { int smallestNo = tenthousand[i]; int posWithSmallest = i; for (int j = i+1; j < tenthousand.length; j++) { int val = tenthousand[j]; if (val < smallestNo) { smallestNo = val; posWithSmallest = j; } } int tmp = tenthousand[i]; tenthousand[i] = smallestNo; tenthousand[posWithSmallest] = tmp; } for (int i = 0; i < tenthousand.length; i++) { System.out.println("Position " + i + " : " + tenthousand[i]); } } }
I apologize in advance in any of this is terribly written or just completely wrong. As I said, I have no clue.![]()