sort number in ascending

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    sort number in ascending

    Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array [1] from 12, 64, 8, 128 etc.
    3 12
    4 64
    7 8
    10 128
    ...


    I just wanna sort and number out them :
    1 8
    2 12
    3 64
    ....

    And my work...(I have tried to use bubbleSort() that I copied from people's ..)

    Code:
    public int[] bubbleSort(int array[]) {
    boolean swappedOnPrevRun = true;
    while(swappedOnPrevRun)
    {
     swappedOnPrevRun = false;
     for(int i = 0; i < array.length - 1; i++){
     if(array[i] > array[i + 1])
    {
     swappedOnPrevRun = true;		 	 	
    int temp = array[i];	
    array[i] = array[i + 1];	
    array[i + 1] = temp;		 	 			}
    	 	 		}
    	 	 	}
     return array;
    	 	 }
    
         private static String[] readLines (String fileName, int m) throws IOException
         {
    
         	List<String> lines = new ArrayList<String>();
    
         	String lineFRST = "";
         	Boolean readable = true;
    
            BufferedReader in1 = new BufferedReader (new FileReader( m + "File.txt"));
    
    
         	while (readable)
    		{
    		  lineFRST = in1.readLine();
              	  if (lineFRST != null)
    	  {
    	 lines.add(lineFRST);
    	     readable = true;
    	  }
    		   else
    	    readable = false;
    		}
    
      return lines.toArray(new String[lines.size()]);
    
         }
    
         public static void main(String[] args) throws IOException
        {
    
            PrintWriter printWriter = null;
            BufferedWriter bufferWriter = null;
    
       for (int m=1; m<=40; m++)  
    	   {
    	     String[] lines = readLines (m + "Out.txt", m);  //read all lines from your file
    
             printWriter = new PrintWriter(new BufferedWriter(new FileWriter(m +"SORTED.txt", true)));
    
             for(int i = 0; i < lines.length; i++)
             {
                String[] theline = lines[i].split(" ");
    
                /*String[] num = theline[0];
                  String[] toSort = theline[1];*/
                    
                // need to convert string to int first....               
    
                printWriter.println( (i+1) + " : " + lines[i]);
            }
        \\      bubbleSort ();  \\need to parse the int
    
            printWriter.close();
      }
    
         }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at the article "generic heap sort algorithm" in the Java Articles section.

    kind regards,

    Jos

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by JosAH
      Have a look at the article "generic heap sort algorithm" in the Java Articles section.

      kind regards,

      Jos
      Jos, assist me on array syntax..what's wrong with my syntax here..
      I will study about the article..thank you very much
      Code:
      for(int i = 0; i < lines.length; i++)
               {
                  String[] theline = lines[i].split(" ");
      
                  String[] testcase = theline[i];
      		    String[] eod = theline[i];
      
                  printWriter.println( (i+1) + " : " + lines[i]);
              }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by shana07
        Jos, assist me on array syntax..what's wrong with my syntax here..
        I will study about the article..thank you very much
        Code:
        for(int i = 0; i < lines.length; i++)
                 {
                    String[] theline = lines[i].split(" ");
        
                    String[] testcase = theline[i];
        		    String[] eod = theline[i];
        
                    printWriter.println( (i+1) + " : " + lines[i]);
                }
        "lines" is an array of Strings and so is "theline" but "theline[ i ]" is just one
        single String and you can't cast a single String to another String array where
        you want to initialize variable "testcase".

        kind regards,

        Jos

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by JosAH
          "lines" is an array of Strings and so is "theline" but "theline[ i ]" is just one
          single String and you can't cast a single String to another String array where
          you want to initialize variable "testcase".

          kind regards,

          Jos
          The article is about sorting name, please assist me on sorting integer ...
          What do you think about the buble sort method as above ?

          Comment

          • shana07
            Contributor
            • Jan 2007
            • 280

            #6
            Originally posted by shana07
            The article is about sorting name, please assist me on sorting integer ...
            What do you think about the buble sort method as above ?
            I'm sorry.I think I almost get there..I use this
            Code:
            String[] lines = readLines (m + "Input.txt", m);  //read all lines from your file
             printWriter = new PrintWriter(new BufferedWriter(new FileWriter(m +"SORTED.txt", true)));
             Arrays.sort (lines);
            
              for( i = 0; i < lines.length; i++)
               {
                    printWriter.println("lines["+(i+1) +"] = "+lines[i]);
            
               }
            But it's not yet correct order...please see below example..assist me anyone.
            lines[1] = 100
            lines[2] = 100
            lines[3] = 104
            lines[4] = 108
            lines[5] = 112
            lines[6] = 112
            lines[7] = 112
            lines[8] = 112
            lines[9] = 12
            lines[10] = 12
            lines[11] = 12

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by shana07
              The article is about sorting name, please assist me on sorting integer ...
              What do you think about the buble sort method as above ?
              Nope, bubble sort is a silly thing and nope, that article isn't about sorting names;
              it's about anything that happens to be Sortable. Please read that article again
              and try to understand what object oriented programming is all about.
              The examples just showed two arrays of Strings that need to be sorted. It could
              as easily have sorted two arrays of Integers or whatever; as long as that 'whatever'
              implements the Sortable interface.

              kind regards,

              Jos

              Comment

              Working...