Please give me code of selectionSort.
7 7725
Selection sort as in the algorithm?
or sorting a list?
this will sort a numeric array with more than 1 elements - dim i as integer
-
dim j as integer
-
dim tmp as double
-
For i = lbound(array) To ubound(array) -1
-
For j = i + 1 To ubound(array)
-
If array(j) < array(i) Then
-
tmp = array(i)
-
array(i) = array(j)
-
array(j) = tmp
-
End If
-
Next
-
Next
hope this helps
From the look of it, that's a bubble sort. Probably the simplest, but slowest, "real" sort algorithm that can be written.
I have a program (it came with QuickBasic 4.5, I think) which demonstrates a lot of different sort techniques, including the selection sort. I'll see whether I can find it and post it.
Have you tried using the search box up the top of the page? We may have already covered this before.
In the meantime, this may be of interest.
From the look of it, that's a bubble sort. Probably the simplest, but slowest, "real" sort algorithm that can be written.
Yeap, it's a bubble sort, i thought i could be of some use.
In the meantime, this may be of interest.
i'll take a look, thanks Killer.
Just so you know, I haven't forgotten. Still looking for the old sort code.
I did find a link to a similar type of thing on the web. Check this out. Some very interesting stuff there, including what is probably the slowest possible sort. It switches the data to a completely random sequence each time, and tests to see whether it's sorted yet. :)
I think the source is in Java, but it's still pretty interesting.
Kadghar, I made a sort sub a while ago. You say yours is a bubble sort... is this too?
From all I can see it looks like yours, but sort of... reversed. -
Public Sub SortListBox(ListToSort As ListBox)
-
'Sorts items in a listbox by numerical order of the number in the text of each item.
-
'In other words, all the text of the items must be numbers.
-
'They must also all be > -1 !
-
-
Dim SortHighestNumberSoFar As Long
-
Dim SortHighestIndexSoFar As Long
-
Dim Temp As Long 'The counter for the main outer loop
-
Dim Temp2 As Long 'The counter for the inner loop which finds the maximum so far
-
Dim Temp3 As String 'Holds text mid-way while swapping the text of an item in the list
-
-
For Temp = 1 To ListToSort.ListCount
-
'We go through each item in the list
-
'(Main loop)
-
SortHighestNumberSoFar = -1
-
SortHighestIndexSoFar = -1
-
-
For Temp2 = 0 To ListToSort.ListCount - Temp
-
'...and for each item in the list, we go through from that
-
'item upwards until the end of the list(again).
-
'(Each item's loop)
-
'We stop at [number of items] take away [where we are in the
-
'main loop (take away 1)] so that we don't include items we've
-
'already sorted! (And have put at the end!)
-
-
If Val(ListToSort.List(Temp2)) > SortHighestNumberSoFar Then
-
'If we've found a new highest number, remember this new
-
'highest number and the index of the item in the list
-
'which contains it (in other words, what Temp2 is)
-
SortHighestNumberSoFar = Val(ListToSort.List(Temp2))
-
SortHighestIndexSoFar = Temp2
-
End If
-
-
Next Temp2
-
-
'Now the loop for finding the highest so far has finished
-
'(Temp2's), we are left with the highest number and its index.
-
'We'll do it by copying the number at Temp, to 'Temp take away 1',
-
'and everything which was at 'Temp take away 1', to where this one
-
'was (in other words, swapping them around). This means we'll need
-
'to store what we're copying mid-way (in Temp3).
-
-
'SortHighestIndexSoFar = copying FROM
-
'Temp-1 = copying TO
-
'Which means...
-
'Count-(Temp-1)'s stuff --> Temp3 (which is a slot itself)
-
'SortHighestIndexSoFar's stuff --> Count-(Temp-1)'s slot
-
'Temp3's stored stuff --> SortHighestIndexSoFar
-
-
-
Temp3 = ListToSort.List(ListToSort.ListCount - Temp)
-
ListToSort.List(ListToSort.ListCount - Temp) = ListToSort.List(SortHighestIndexSoFar)
-
ListToSort.List(SortHighestIndexSoFar) = Temp3
-
-
'Now we can look for the highest number down to just before
-
'that highest one, so we find the next highest and put THAT
-
'just before this one, and so on...
-
'So we'll be left with it starting at the lowest (at the
-
'top of the list), and highest at the bottom of the list.
-
Next Temp
-
-
-
End Sub
-
You give it a ListBox and it sorts all the items.
For people who are interested, I made a demo program of it which you can get here: http://www.rocketsoft.gm-school.uni....ort%20List.zip
Hope it helps someone! =D
Sign in to post your reply or Sign up for a free account.
Similar topics
by: shaveta |
last post by:
pls help me to write a program in c that reads n elements of type float, stores them in a linked list ,sort it in the descending order using selection sort methods, and finally output the sorted...
|
by: gemacjr1201 |
last post by:
I need hints for an algorithm using string compare in a selection sort.
I will be sorting lastnames and first namesalphbetically. if the selection sort encounters the same last name it then goes to...
|
by: ShaveDave27 |
last post by:
Hi,
I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday).
Now...
|
by: ShaveDave27 |
last post by:
HI,
I've created a Comparable interface class Person. Which has variables First_name, surname, month(Of birth), day(Of birth) , and birthday(which i have created from month and day). I have a...
|
by: neyugncul |
last post by:
Okay, I have this homework assignment which is to sort a 2D array of characters using selection sort.
I've written the selection sort but I can't seem to get it working. (it's not sorting!)
...
|
by: fatimaballes |
last post by:
how to apply bubble and selection sort in assembly?this is for my subject IT103Lab..
were using notepad++ for encoding then amke everything appear in the command prompt..
another thing...
our...
|
by: mfshake |
last post by:
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i...
|
by: Laya Maheshwari |
last post by:
The function works fine if the "small" I have chosen, ie, a, is *not* the smallest element in the array. It swaps and sorts just fine. But when the "small", ie, a, is the smallest element in the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
| |