NumSort
Home
My Work
Links
Photo Gallery
About Me

 

     

      /**

      ** Author: Walid Bahsow.

      ** E-mail: wabsnet@yahoo.com

      ** Friday 21th of March, 2003

      **/

      /* This is a program which reads an N number of elements from a file (where N is the first
      *  number in the file), and sorts them using the java built in sort method and then
      *  prints them on another file.
      */

      import java.io.*;
      import java.util.*;

      class NumSort {
          static StreamTokenizer in;
          static PrintWriter out;
          static int[] list;
          static int N;
   
          public static void main(String[] args)throws IOException {
              init();
              Arrays.sort(list); //sorts the list
              printer();
          } // end main

          // Reads in the numbers and puts them in the array list.

          static void init()throws IOException {
              in = new StreamTokenizer(new FileReader("numsort.in"));
              in.nextToken();
              N = (int)in.nval;
              list = new int[N];
              in.nextToken();
              for (int i = 0; i < list.length; i++) {
                  list[i] = (int) in.nval;
                  in.nextToken();
              }
          }

          // Prints the array in the file.

          static void printer() throws IOException {
              out = new PrintWriter (new FileWriter ("numsort.out"));
              for (int i = 0; i < list.length; i++) {
                  out.println(list[i]);
              }
              out.close();
          }

      } // End class NumSort

      Sample Input:

      10
      9
      2
      7
      6
      5
      8
      1
      3
      4
      0

      Output:

      0
      1
      2
      3
      4
      5
      6
      7
      8
      9


 

 

Home | My Work | Links | Photo Gallery | About Me | Simple Java Programs | Sign Guest Book | View Guest Book

This site was last updated Friday March 21, 2003