473,385 Members | 1,942 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,385 software developers and data experts.

Selection Sort

Please give me code of selectionSort.
Aug 30 '07 #1
7 7725
Selection sort as in the algorithm?
or sorting a list?
Aug 30 '07 #2
kadghar
1,295 Expert 1GB
this will sort a numeric array with more than 1 elements

Expand|Select|Wrap|Line Numbers
  1. dim i as integer
  2. dim j as integer
  3. dim tmp as double
  4. For i = lbound(array) To ubound(array) -1
  5.     For j = i + 1 To ubound(array)
  6.         If array(j) < array(i) Then
  7.             tmp = array(i)
  8.             array(i) = array(j)
  9.             array(j) = tmp
  10.         End If
  11.     Next
  12. Next
hope this helps
Aug 30 '07 #3
Killer42
8,435 Expert 8TB
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.
Aug 31 '07 #4
Killer42
8,435 Expert 8TB
In the meantime, this may be of interest.
Aug 31 '07 #5
kadghar
1,295 Expert 1GB
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.
Aug 31 '07 #6
Killer42
8,435 Expert 8TB
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.
Sep 2 '07 #7
Robbie
180 100+
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.

Expand|Select|Wrap|Line Numbers
  1. Public Sub SortListBox(ListToSort As ListBox)
  2.     'Sorts items in a listbox by numerical order of the number in the text of each item.
  3.     'In other words, all the text of the items must be numbers.
  4.     'They must also all be > -1 !
  5.  
  6.     Dim SortHighestNumberSoFar As Long
  7.     Dim SortHighestIndexSoFar As Long
  8.     Dim Temp As Long    'The counter for the main outer loop
  9.     Dim Temp2 As Long   'The counter for the inner loop which finds the maximum so far
  10.     Dim Temp3 As String 'Holds text mid-way while swapping the text of an item in the list
  11.  
  12.     For Temp = 1 To ListToSort.ListCount
  13.     'We go through each item in the list
  14.     '(Main loop)
  15.         SortHighestNumberSoFar = -1
  16.         SortHighestIndexSoFar = -1
  17.  
  18.         For Temp2 = 0 To ListToSort.ListCount - Temp
  19.         '...and for each item in the list, we go through from that
  20.         'item upwards until the end of the list(again).
  21.         '(Each item's loop)
  22.         'We stop at [number of items] take away [where we are in the
  23.         'main loop (take away 1)] so that we don't include items we've
  24.         'already sorted! (And have put at the end!)
  25.  
  26.             If Val(ListToSort.List(Temp2)) > SortHighestNumberSoFar Then
  27.             'If we've found a new highest number, remember this new
  28.             'highest number and the index of the item in the list
  29.             'which contains it (in other words, what Temp2 is)
  30.                 SortHighestNumberSoFar = Val(ListToSort.List(Temp2))
  31.                 SortHighestIndexSoFar = Temp2
  32.             End If
  33.  
  34.         Next Temp2
  35.  
  36.         'Now the loop for finding the highest so far has finished
  37.         '(Temp2's), we are left with the highest number and its index.
  38.         'We'll do it by copying the number at Temp, to 'Temp take away 1',
  39.         'and everything which was at 'Temp take away 1', to where this one
  40.         'was (in other words, swapping them around). This means we'll need
  41.         'to store what we're copying mid-way (in Temp3).
  42.  
  43.         'SortHighestIndexSoFar = copying FROM
  44.         'Temp-1 = copying TO
  45.         'Which means...
  46.         'Count-(Temp-1)'s stuff  -->  Temp3 (which is a slot itself)
  47.         'SortHighestIndexSoFar's stuff  -->  Count-(Temp-1)'s slot
  48.         'Temp3's stored stuff  -->  SortHighestIndexSoFar
  49.  
  50.  
  51.             Temp3 = ListToSort.List(ListToSort.ListCount - Temp)
  52.             ListToSort.List(ListToSort.ListCount - Temp) = ListToSort.List(SortHighestIndexSoFar)
  53.             ListToSort.List(SortHighestIndexSoFar) = Temp3
  54.  
  55.         'Now we can look for the highest number down to just before
  56.         'that highest one, so we find the next highest and put THAT
  57.         'just before this one, and so on...
  58.         'So we'll be left with it starting at the lowest (at the
  59.         'top of the list), and highest at the bottom of the list.
  60.     Next Temp
  61.  
  62.  
  63. End Sub
  64.  
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
Sep 3 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
1
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...
1
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...
12
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...
1
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!) ...
1
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...
1
mfshake
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...
5
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...
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: 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$) { } ...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
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
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...
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...

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.