Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Sorting {
- public static void main (String[] args){
- //Here, we initiate an array with the integers
- //1 through 10,000 in order.
- Random rgen = new Random();
- int[] intArray = new int[10000];
- for (int i=0; i<10000; i++) {
- intArray[i] = i+1;
- }
- //Here, we randomize the positions.
- for (int i=0; i<10000; i++) {
- int randomPosition = rgen.nextInt(10000);
- int temp = intArray[i];
- intArray[i] = intArray[randomPosition];
- intArray[randomPosition] = temp;
- }
- //Here, we check for the maximum in the
- //unsorted section of the list.
- int max;
- for (int i=9999; i>=0; i--) {
- max = i;
- for (int j=i-1; j>=0; j--)
- if (intArray[j]>intArray[max]) {
- max = j;
- }
- //Here, we swap the positions of the
- //maximum and the last value in the
- //unsorted section of the list.
- if (max != i) {
- int tmp = intArray[i];
- intArray[i] = intArray[max];
- intArray[max] = tmp;
- }
- }
- //Here, we output the result.
- for (int k=0; k<10000; k++) {
- System.out.println(intArray[k]);
- }
- }
- }
- //Here, we initiate an array with the integers
- //1 through 10,000 in order.
- Random rgen = new Random();
- int[] intArray = new int[10000];
- for (int i=0; i<10000; i++) {
- intArray[i] = i+1;
- }
- for (int i=0; i<10000; i++) {
- intArray[i] = i+1;
- }
- for (int i=1; i=<10000; i++) {
- intArray[i] = i;
- }
- int maxIndex = 0;
- for(int k = 1; k < intArray.length; k++)
- if(intArray[k] > intArray[maxIndex])
- maxIndex = k;
Add Comment
Please, Sign In to add comment