Java Array Sorting

Sorting Integer Array in Java

Page 1 of 1

3 Replies - 4968 Views - Last Post: 18 August 2010 - 03:10 PM Rate Topic: -----

#1 bymak87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-June 10

Java Array Sorting

Posted 18 August 2010 - 04:11 AM

I had to write a program that would generate a text file with 100 random integers based on an array. The only problem I am having now is sorting the array. I have tried writing a selection sort method, the arrays.sort method, and nothing is working. I have to have the numbers in ascending order 0-100. Any suggestions or hints would be greatly appreciated.
Output in the txt file: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 17 21 21 24 31 31 39 41 43 43 43 53 53 56 56 56 56 65 65 65 65 69 70 70 70 70 78 78 78 78 81 81 81 81 81 81 90 90 90 90 90 90 94 94 94 94 94 94 98

output in text file without a sort: 63 44 55 83 71 93 91 95 62 97 12 57 7 35 50 30 49 41 84 10 4 54 56 50 68 59 27 34 63 79 76 95 46 55 57 14 33 4 47 15 84 76 4 52 23 21 82 86 4 98 25 26 72 44 18 71 48 81 40 55 16 48 81 56 59 0 63 77 57 66 0 5 96 42 99 85 5 70 32 57 56 51 88 5 64 51 47 30 7 80 91 45 32 10 98 41 15 90 96 64

import java.util.Random; //import random number generator
import java.io.*;


public class NumGen {
	
	public static void main (String [] args)throws IOException { //creates an exception
		
		Random digit = new Random();//assigning the variable digit as a random number
		int list[]= new int[100];//array with 100 numbers
		File file = new File("Exercise8_19.txt");
		
		if (file.exists()){
			System.out.println("The file "+file.getName()+ " already exists.");
			System.out.println();
			
			
			}//ends if statement

		else {
			System.out.println("Your file has been written to " + (file.getName()+ "."));
			System.out.println();
		}
		try {
			   Writer output = null;
			   
			    File x = new File("Exercise8_19.txt");//name of txt file
			    output = new BufferedWriter(new FileWriter(x));
			  
			       
			for (int i=0; i<list.length; i++){//for loop to create 100 random numbers
				list[i] = (digit.nextInt (100));//set the array to the random numbers with a range of 0 - 100
			 
					 
			
			 output.write(list[i]+" ");//output is the array of random numbers separated by a space
			}//ends for loop
			SelectionSort.selectionSort(list);

			output.close();//ends output
			
			FileInputStream fstream = new FileInputStream("Exercise8_19.txt");
			 // Get the object of DataInputStream
			    DataInputStream in = new DataInputStream(fstream);
			        BufferedReader br = new BufferedReader(new InputStreamReader(in));
			    String strLine;
			    //Read File Line By Line
			    while ((strLine = br.readLine()) != null)   {
			      // Print the content on the console
			      System.out.println (strLine);
			    }
			    //Close the input stream
			    in.close();
		   }//ends try 
		   catch (IOException e) {// if there is no file
				System.err.println("The file was not saved.");//error message
				e.printStackTrace();
				}//ends catch
		
		   
		
	}//ends main
//Selction sort method
	  public static class SelectionSort{
	      static void selectionSort(int list[]){
	    	  for (int i=0; i<list.length-1; i++){
	    		  int currentMin = list[i];
	    		  int currentMinIndex = i;
	    		  
	    		  for( int j = i+1; j< list.length-1; j++){
	    			  if (currentMin > list[j]){
	    				  currentMin = list[j];
	    				  currentMinIndex= j;
	    			  }
	    		  }
	    		  if (currentMinIndex != i){
	    			  list[currentMinIndex]=list[i];
	    			  list[i]= currentMin;
	    			  
	    			  }
	    		  }
	    	  }
	  
	      }
	  
}//ends class



Is This A Good Question/Topic? 0
  • +

Replies To: Java Array Sorting

#2 bymak87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-June 10

Re: Java Array Sorting

Posted 18 August 2010 - 04:35 AM

Solved the problem. Nevermind.
Was This Post Helpful? 0
  • +
  • -

#3 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Array Sorting

Posted 18 August 2010 - 07:17 AM

for the benefit of everyone who is having a similar problem, please explain what exactly was solved. I suspect Arrays.sort() wasn't working because you didn't
 import java.util.Arrays;

This post has been edited by Luckless: 18 August 2010 - 07:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Java Array Sorting

Posted 18 August 2010 - 03:10 PM

Just make an array with your 100 random numbers, use Arrays.sort() on the int array, write the numbers to a file...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1