[Java] Sorting Numeric Array in Ascending order and Descending Order

Joined
Nov 24, 2004
Messages
147
Reaction score
2
public static void main(String[] args)
{
int[] numArray={28, 76, 2, 482, 15, 1250, 3200, 9, 720, 164};
numArray.Sort
for (int i=numArray.length; --i >=0;)
{
for (int j=0; j<1; j++)
{
if (numArray[j] > numArray[j+1])
{
int temp=numArray[j];
numArray[j]=numArray[j+1];
numArray[j+1]=temp;
}
}
}
for (int i=0;i < numArray.length;i++)
{

System.out.print(numArray+" ");

}
System.out.println()
}
}


Anyone can help?
 
luckily, i just had similar project my in CS class. here is our finish project. is not elegant, but it works.

public class SelectionSort
{
public static void swap(int[] sort, int a, int b)
{
int temp = sort[a];
sort[a] = sort;
sort = temp;
}

public static int findMin(int[] nums, int i)
{
int minloc = i;
int min = nums;
for(int j = i+1; j < nums.length; j++)
{
if (min > nums[j])
{
min = nums[j];
minloc = j;
}
}

return minloc;
}

public static void selectionSort(int[] nums)
{
for( int i = 0; i < nums.length-1; i++)
{
swap(nums,findMin(nums,i), i);
}


}


}
 
Back