Please give me code of selectionSort.
Selection Sort
Collapse
X
-
Tags: None
-
this will sort a numeric array with more than 1 elements
[CODE=vb]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[/CODE]
hope this helpsComment
-
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.Comment
-
Originally posted by Killer42From the look of it, that's a bubble sort. Probably the simplest, but slowest, "real" sort algorithm that can be written.
Originally posted by Killer42In the meantime, this may be of interest.Comment
-
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.Comment
-
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.
[code=vb]
Public Sub SortListBox(Lis tToSort 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 SortHighestNumb erSoFar As Long
Dim SortHighestInde xSoFar 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.List Count
'We go through each item in the list
'(Main loop)
SortHighestNumb erSoFar = -1
SortHighestInde xSoFar = -1
For Temp2 = 0 To ListToSort.List Count - 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)) > SortHighestNumb erSoFar 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)
SortHighestNumb erSoFar = Val(ListToSort. List(Temp2))
SortHighestInde xSoFar = 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).
'SortHighestInd exSoFar = copying FROM
'Temp-1 = copying TO
'Which means...
'Count-(Temp-1)'s stuff --> Temp3 (which is a slot itself)
'SortHighestInd exSoFar's stuff --> Count-(Temp-1)'s slot
'Temp3's stored stuff --> SortHighestInde xSoFar
Temp3 = ListToSort.List (ListToSort.Lis tCount - Temp)
ListToSort.List (ListToSort.Lis tCount - Temp) = ListToSort.List (SortHighestInd exSoFar)
ListToSort.List (SortHighestInd exSoFar) = 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
[/code]
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:
Hope it helps someone! =DComment
Comment