Hey! I'm working on this program that generates 100 random numbers and stores them in an array. This is the unsorted array. Then I use those same random numbers and try to make a sorted array. And it's supposed to be is ascending order like 2,4,6. Only 5 numbers per line though. I had a program earlier that worked but it used less numbers so I copy and pasted it here and it doesn't work. Only the bottom few numbers seem to be put in order. Anyone have an idea what I'm doing wrong? Code :

/**
 * Write a description of class Nicoleselsort here.
 * 
 * @author Nicole Daniels 
 * @version 1.0
 */
public class Nicoleselsort
{
     public static void main (String [] args) {
     int [] sel = new int [100];
     System.out.print("Unsorted");
     for (int i = 0; i < sel.length; i++){
       for (int l = 0; l < 100; l++)
       {
            //generates a random number
            double r = Math.random();
            double numb = (r * 100);
            int random = ((int)(numb));
            //sets the element at i to a random number
            sel[i] = random;
       }
     }
     for ( int i = 0; i < sel.length; i++)
     {
        if( i % 5 == 0)
        System.out.println("");
        System.out.print ( sel[i] + "\t");
     }
      System.out.println("");
      System.out.print("Sorted");
     for (int n = sel.length; n > 1; n--){
           //set n to the length of the array for comparison later on
         int max = 0;
         int temp = 0;
          for (int i = 0; i < n; i++){
            if (sel[i] > max)
            //scans the array and sets the largest number to max
             max = i;
         }
         //keeps scanning the array by decrementing array length by one and sets max
         temp = sel[max];
         sel[max] = sel[n-1];
         sel [n-1] = temp;
         if(n % 5 == 0)
         System.out.println("");
         System.out.print(temp + "\t");
      }
}
}

Output:
Unsorted
13 24 91 75 80
77 61 42 62 9
37 80 36 7 94
95 85 52 17 96
45 90 52 91 11
10 31 27 54 9
42 25 94 60 66
60 81 68 65 40
37 35 41 29 1
97 32 61 39 89
92 9 54 69 2
42 64 78 68 34
77 53 46 57 36
77 60 39 63 87
41 96 63 12 56
29 44 77 76 0
81 26 13 69 1
21 2 14 21 59
61 4 95 58 88
68 14 66 33 3
Sorted
95 88 81 77 76
96 87 68 66 69
77 63 63 61 77
59 60 68 58 78
57 64 56 69 53
54 92 89 61 46
97 44 41 42 41
40 39 39 65 68
37 81 36 60 35
66 34 60 33 94
32 42 29 29 54
27 31 26 25 91
52 90 21 21 45
96 52 17 85 95
94 14 13 14 36
12 80 11 37 10
9 9 9 62 42
7 61 77 80 4
75 3 91 2 2
24 1 1 13

sure I don't understaodd exactly what are you tried to archieve, sorry my magic ball is tired, only continue by reads comment

System.out.print("Sorted");

if I add

Arrays.sort(sel);

then outPut ...

import java.util.Arrays;

public class NicoleSelSort {

    public static void main(String[] args) {
        int[] sel = new int[100];
        System.out.print("Unsorted");
        for (int i = 0; i < sel.length; i++) {
            for (int l = 0; l < 100; l++) {
                double r = Math.random();//generates a random number
                double numb = (r * 100);
                int random = ((int) (numb));
                sel[i] = random;//sets the element at i to a random number
            }
        }
        for (int i = 0; i < sel.length; i++) {
            if (i % 5 == 0) {
                System.out.println("");
            }
            System.out.print(sel[i] + "\t");
        }
        System.out.println("");
        Arrays.sort(sel);
        System.out.print("Sorted");
        for (int n = sel.length; n > 1; n--) {
            int max = 0;//set n to the length of the array for comparison later on
            int temp = 0;
            for (int i = 0; i < n; i++) {
                if (sel[i] > max) { //scans the array and sets the largest number to max
                    max = i;
                }
            }
            temp = sel[max]; //keeps scanning the array by decrementing array length by one and sets max
            sel[max] = sel[n - 1];
            sel[n - 1] = temp;
            if (n % 5 == 0) {
                System.out.println("");
            }
            System.out.print(temp + "\t");
        }
    }

    private NicoleSelSort() {
    }
}

Ok thanks everyone! But I found the Arrays.sort method in the API and it helped me. But thanks for the help

This article has been dead for over six months. Start a new discussion instead.