Results 1 to 2 of 2
Thread: trouble with sorting! T^T
 03072009, 11:22 PM #1Member
 Join Date
 Jan 2009
 Posts
 90
 Rep Power
 0
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
 03092009, 12:44 PM #2Member
 Join Date
 Feb 2009
 Posts
 96
 Rep Power
 0
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

[SOLVED] trouble in sorting...please help
By jacline in forum New To JavaReplies: 8Last Post: 03082009, 04:44 PM 
sorting
By jot321 in forum New To JavaReplies: 18Last Post: 10022008, 10:30 AM 
Sorting CachedRowset
By Sayed in forum Advanced JavaReplies: 0Last Post: 07182008, 12:14 PM 
Help with Sorting Program
By rhm54 in forum New To JavaReplies: 3Last Post: 01252008, 10:08 PM 
sorting JTable
By mansi_3001 in forum Advanced JavaReplies: 3Last Post: 08102007, 06:29 PM
Bookmarks