Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default trouble with sorting! T^T

    this program outputs
    for example
    before sorting
    4561
    2158
    2533
    after sorting(which is wrong)
    1541
    1521
    2332
    after sorting (correct version)
    6541
    8521
    5332

    i think the error occurs at the greed codes
    can someone help me on what do i need to change to make it correct.
    and can someone give me a hint on sorting a whole row by the first elements.
    THANKS!
    Java Code:
    import java.util.Scanner;
    
    public class sort2D
    {
    	public static void main (String [] args)
    	{
    
    	Scanner input = new Scanner(System.in);
    
    	System.out.println("Enter an integer for number of rows range from 3 to 10: ");
    	int m = input.nextInt();
    
    	while (m<3 || m>10)
    	{
    	System.out.println("Enter an integer for number of rows range from 3 to 10 again: ");
    		m = input.nextInt();
    	}
    
    	System.out.println("Enter an integer for number of columns range from 3 to 10: ");
    	int n = input.nextInt();
    
    	while (n<3 || n>10)
    	{
    	System.out.println("Enter an integer for number of columns range from 3 to 10 again: ");
    		n = input.nextInt();
    	}
    
    	int [] [] arr = new int [m] [n];
    
    System.out.println("Assign random numbers from 1 to 30 into a "+m+" x "+n+" array is:");
    ranArr(arr);
    
    System.out.println("After sorting each row in decending orders is: ");
    decRow(arr);
    
    
    
    }//end of main
    //generate random numbers
    public static void ranArr(int [][] arr)
    {
    	for (int i=0; i<arr.length; i++)
    	{
    		for (int j=0; j<arr[i].length; j++)
    		{
    			arr [i][j]= (int)((Math.random()*30)+1);
    
    				System.out.print(arr[i][j]+" ");
    		}
    		System.out.println();
    	}
    }
    //method for sorting numbers in decending order in each rows
    
    	public static void decRow(int[][] arr)
    	{
    
    		for (int f=0; f<arr.length; f++)
    		{
    		 for (int i =0; i < arr[f].length; i++)
    		 {
    		      // Find the maximum in the list[0..i]
    					int max = arr[f][i];
    					int maxloc = 0;
    		      for (int j = i; j < arr[f].length; j++)
    		      {
    		        if (arr[f][j]> max)
    		        {
    		          max = arr[f][j];
    		          maxloc = j;
    
    		        }
    		   	  }
    		   	   // Swap
    		   	[COLOR="SeaGreen"]   if (max != i)
    		   	   {
    		   	   int tmp = max;
    			   arr[f][maxloc] = arr[f][i];
    		                arr[f][i] = tmp;
    				}[/COLOR]
    		  }
    		 }
    //print array
    	for (int i=0; i<arr.length; i++)
    	{
    		for(int j=0; j<arr[i].length; j++)
    		{
    			System.out.print(arr[i][j]+" ");
    		}
    			System.out.println();
    	}
    
    
    	}
    
    
    	}//end of class

  2. #2
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    hi , you didin't make make a mistake in your logic at swapping .I think you were not find small mistake that you have made.
    see in your code , you printed your sorted array in the descRow()
    method,that was the mistake you did,

    public static void descRow(int[]][] arr){
    ------
    -----
    //print desc array
    for (int i=0; i<arr.length; i++)
    {
    for(int j=0; j<arr[i].length; j++)
    {
    System.out.print(arr[i][j]+" ");
    }
    System.out.println();
    }
    int printing loop the array "arr" don't know whether it is sorted one or
    it is a random array .
    do like this
    declare your method with return type like
    public static int[][] descRow(int[][] arr){
    ---
    ---
    ---
    return arr;
    }
    in the main function call descRow method like and
    int[][] descArray = descRow(arr):
    define a method to print above descArray
    public static void printArray(int[][] descArray){
    for (int i=0; i<arr.length; i++)
    {
    for(int j=0; j<arr[i].length; j++)
    {
    System.out.print(arr[i][j]+" ");
    }
    System.out.println();
    }
    then call it in the main function printArray(descArray);
    I think now you know the importance of methods .

Similar Threads

  1. [SOLVED] trouble in sorting...please help
    By jacline in forum New To Java
    Replies: 8
    Last Post: 03-08-2009, 05:44 PM
  2. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 11:30 AM
  3. Sorting CachedRowset
    By Sayed in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2008, 01:14 PM
  4. Help with Sorting Program
    By rhm54 in forum New To Java
    Replies: 3
    Last Post: 01-25-2008, 11:08 PM
  5. sorting JTable
    By mansi_3001 in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 07:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •