473,394 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

my ascending sort algo got problem. can you please point it out ? thank you for reading


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 should
be 3,4,7,9


Jul 17 '05 #1
1 1733
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" <sm***@holly.wor.com> wrote in message
news:40**********@news.tm.net.my...

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 should be 3,4,7,9


Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: renster | last post by:
I have been looking at implementing the following but with a difference. http://forums.devshed.com/t47653/s.html This example uses a link which works with some code shown on the page. I have...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
6
by: Tamir Khason | last post by:
Let's say I have Point and I want to sort it circle way (e.g. 2;2.4;1,8;2,8,5;4;8,2;7,1;4 The algo for this is: if ((p.X<p.X & p.Y<Avg(p).Y & p.Y<Avg(p).Y) | (p.X>p.X & p.Y>Avg(p).Y &...
1
by: Mark | last post by:
Rather than have only one column sorted in a single direction in a listview. I would like to be able to sort on any column in alternating directions (ascending,descending) eg. First column click...
2
by: sunilkes | last post by:
I have a to only show date. I set the date to be in ascending order, but still 01/11/2006 shows first, and 31/10/2006 shows last..It is only ascending the date and not the complete date including...
11
by: dwelden | last post by:
I have successfully used the sort lambda construct described in http://mail.python.org/pipermail/python-list/2006-April/377443.html. However, how do I take it one step further such that some values...
3
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
6
by: Howie | last post by:
This must be a permissions issue. But I don't know how to fix it. I am in the middle of writing a database so I haven't even learned how to deal with security yet. Could someone tell me how to...
2
by: Gestorm | last post by:
Suppose we have an array a, the idea is: build another array, int next, for each 0<i<N, next = next position of a in the sorted array, if a is the max, then next is undefined. For example, let the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.