Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bubble Sort 2

Status
Not open for further replies.

Greeno

MIS
Mar 15, 2001
7
GB
Can anyone please tell me how to do a bubble sort in VB.
Happy to try and help you.
 
This is sample C code, but hopefully you should be able to translate it easily. It's basically just assignment statements and nested loops.

Hope this helps.


void bubble_sort(long thearray[], int sizeofarray)
{
int i,j;
long temp;

for(i=sizeofarray; i>=0; --i) // For each spot in the array
{
// (starting at the top)
for(j=0; j<i; j++) // Walk through the array to that
{
// spot
if(thearray[j] > thearray[j+1]) // At each location, compare the
{
// two adjacent elements
temp = thearray[j]; // Swapp elements as needed
thearray[j] = thearray[j+1];
thearray[j+1] = temp;
}
}
}
return;
}
 
Have a look at
Its an article written by Franceso Balena: Get the Most Out of Your Arrays. It discusses the most common sort algorithms with guidance when and where to use them. VB source code embedded in the article.

As an alternative you may consult a book written by Dan Fox:
Pure Visual Basic
SAMS Publishing
ISBN 0-672-31598-X
Chapter 24 discusses Array Techniques, primarily Sorting. It too contains the Source code embedded. However this source code can be downloaded as follows:
Go to After entering the ISBN you will be presented with a page where you can download all the listings from this book in a chapter-by-chapter zip file. Remember Chapter 24!

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Something I found out not too long ago, and thought was great, so I'll share.

I was always under the impression that recordsets could only be created from data in a database. I was wrong.

You can create your very own recordsets (with as many fields as you want), and stuff them with data that you might have normally put into an array.

The beautiful thing is that you can do away with bubble sorts if you think you can use this method, because you can now take full advantage of all the sorting (and plethora of other useful functions ) that come for free with the ADO Recordset.

Obviously, a recordset is only a 2x2 matrix, and so if you needed a large multi-multi-dimensional array, then you couldn't use this method, and there are lots of other areas where this simply wouldn't be the most efficient way to do things.

BUT!! When you can do it, it's great, and saves mucho time programming those sort routines and such.

ALL HAIL THE RECORDSET!
:)
Paul Prewett
 
Bubblesorts are incredibly slow, unless you have very few items. Please go and find hash sorts and insertion sorts. They are a bit harder to set up, but are much quicker. I cant find offhand where i used it, it was some while ago. I would think it would be quicker, if harder, than creating a file, sorting it and reading the file as in the ADO example. Smart idea though
 
link9,

&quot;Obviously, a recordset is only a 2x2 matrix&quot;

I'm pretty sure this is not correct. I have tried searching here (but so far have failed) to find a post where an ADO recordset was created with numerous fields, and the recordset was populated from a CSV file by reading in thre CSV file, assigning the elements to the ADO RecordSet and just doing the .Update on the ADO.RecordSet object.

I was impressed w/ the simplicity of it all and tried it out. Seemed to work JUST fine. Since MyApp was strictly a VB w/ API based record maniipulation of BTRieve files, I 'lost' the procedure I creted and now I can't find the POST!


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top