| re: my ascending sort algo got problem. can you please point it out ? thank you for reading
Wil -
You had it sort of right - here is the bubble sort corrected.
public static void main (String args[])
{
int i, j;
int numElement = 4;
int arr[]={4,7,3,9};
int temp=-1;
int iptr=-1;
// Print out the initial array.
for (int y=0; y<4; y++)
{
System.out.println(arr[y]);
}
// Start the sort. Repeat number of elements less 1 times.
for(i=0; i<numElement;i++)
{
for (j=0; j<numElement-1; j++)
{
if (arr[j] < arr[j+1])
{
temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for (int y=0; y<4; y++)
{
System.out.println(arr[y]);
}
}
As a note, watch which elements you are switching. To make the sort
ascending or descinding, switch the < or > in the main comparison.
Hope this helps,
Fred.
"wil smiths" <smith@holly.wor.com> wrote in message
news:40a7b0b6$1_1@news.tm.net.my...[color=blue]
>
> int i,j,val=4;
> int arr[]={4,7,3,9};
> int temp=-1;
>
> int iptr=-1;
> for(i=0; i<=val-2;i++)
> {
>
> iptr=i;
> for(j=i+1;j<=val-1;j++)
> {
>
> if(arr[j]>arr[iptr])
>
>
> if(i !=iptr) {temp=arr[i];
> arr[i]=arr[iptr];
> arr[iptr]=temp;
>
> }
>
> }
>
>
>
>
>
>
> }
>
>
>
> for(int z=0; z< 4; z++) System.out.println(arr[z]); // result[/color]
should[color=blue]
> be 3,4,7,9
>
>
>
>
>
>[/color] |