473,757 Members | 6,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting a list of integers

I have a list of integers (example sub set of data below), what is the
best method of sorting these in code?

1, 872
5, 1283
8, 343
9, 123

Jan 10 '07 #1
9 1980
If you've only got a small amount of data then the easiest way to
program it would be to use a bubble sort
Take a look at http://www.osix.net/modules/article/?id=158

Matt
On 10 Jan, 16:52, robin9...@hotma il.com wrote:
I have a list of integers (example sub set of data below), what is the
best method of sorting these in code?

1, 872
5, 1283
8, 343
9, 123
Jan 10 '07 #2

matt urbanowski wrote:
If you've only got a small amount of data then the easiest way to
program it would be to use a bubble sort
Take a look at http://www.osix.net/modules/article/?id=158

Matt

Bubble sort is O(n^2), isn't it? I'm not sure how Array.Sort is
implemented, but it's probably better than O(n^2).
To OP, put the numbers into an array and then call Array.Sort.

Jan 10 '07 #3

lord.zol...@gma il.com wrote:
Bubble sort is O(n^2), isn't it? I'm not sure how Array.Sort is
implemented, but it's probably better than O(n^2).
To OP, put the numbers into an array and then call Array.Sort.
I'd use Array.Sort as well. It uses the quicksort algorithm so it's
faster and most importantly it's already implemented for us.

Jan 10 '07 #4
<lo*********@gm ail.comschrieb:
>If you've only got a small amount of data then the easiest way to
program it would be to use a bubble sort
Take a look at http://www.osix.net/modules/article/?id=158

Bubble sort is O(n^2), isn't it?
That's true. Bubble sort is only a good choice for arrays which are already
mostly sorted.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 10 '07 #5
I thought that the array list option only allows one value to be set in
the array and still allowed to be sorted?

Herfried K. Wagner [MVP] wrote:
<lo*********@gm ail.comschrieb:
If you've only got a small amount of data then the easiest way to
program it would be to use a bubble sort
Take a look at http://www.osix.net/modules/article/?id=158
Bubble sort is O(n^2), isn't it?

That's true. Bubble sort is only a good choice for arrays which are already
mostly sorted.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Jan 11 '07 #6
On 2007-01-11, ro*******@hotma il.com <ro*******@hotm ail.comwrote:
I thought that the array list option only allows one value to be set in
the array and still allowed to be sorted?
You mean does it have to contain unique values? No. Array.Sort makes no such
requirement.

--
Tom Shelton
Jan 11 '07 #7
I mean if the rows have two values but then sorting on one of the
columns and not an amalgamation of the columns.

Tom Shelton wrote:
On 2007-01-11, ro*******@hotma il.com <ro*******@hotm ail.comwrote:
I thought that the array list option only allows one value to be set in
the array and still allowed to be sorted?

You mean does it have to contain unique values? No. Array.Sort makes no such
requirement.

--
Tom Shelton
Jan 12 '07 #8
See:

Advanced IComparer // Sorting on Multiple Values
http://sholliday.spaces.live.com/blog/

<ro*******@hotm ail.comwrote in message
news:11******** **************@ i39g2000hsf.goo glegroups.com.. .
I have a list of integers (example sub set of data below), what is the
best method of sorting these in code?

1, 872
5, 1283
8, 343
9, 123

Jan 12 '07 #9
Don't mean to toot my own horn... but I took a list at code referenced at
that link and frankly I wouldn't do it that way. For a couple of reasons
but in so far as I can tell by just reading the code it may not work
properly. Not saying it "doesn't" just that it might not.

Why? Because the QuickSort used in .Net is not a "stable sort". That means
that subsequent sorts of the same data are not guaranteed to maintain order
when two items are identical (i.e. already sorted.) A stable sort algorithm
wouldn't move two items if they are equal and a non-stable sort algorithm
would (and does). It gains speed this way but the result is if you sort one
column and then sort another column the first sort can change making them
unsorted again. It could be that the fellow accomodates this in his code
but I couldn't see it.

What I did to solve it (some years ago) is introduce a stable sort option
which insures that multiple calls maintain earlier sorts. Instead of having
to pass an array of values or run it through a special routine you simply
ask for something along the lines of: object.SortStab le( item ) on as many
items as you want.

The code would have to be adapted a bit but if anybody is interested you
should be able to find it here: http://www.searchcsla.com/Search.aspx where
it was archived. Enter "Sort" for the search criteria and you'll see a
couple of threads including "Stable Sort Solved" which is where I posted the
code.

Seems a shame to have the code go to waste if somebody can use it.

Tom
"sloan" <sl***@ipass.ne twrote in message
news:eQ******** ******@TK2MSFTN GP06.phx.gbl...
See:

Advanced IComparer // Sorting on Multiple Values
http://sholliday.spaces.live.com/blog/

<ro*******@hotm ail.comwrote in message
news:11******** **************@ i39g2000hsf.goo glegroups.com.. .
>I have a list of integers (example sub set of data below), what is the
best method of sorting these in code?

1, 872
5, 1283
8, 343
9, 123


Jan 12 '07 #10

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

Similar topics

13
4634
by: Paul | last post by:
Hi all I have a sorting problem, but my experience with Python is rather limited (3 days), so I am running this by the list first. I have a large database of 15GB, consisting of 10^8 entries of approximately 100 bytes each. I devised a relatively simple key map on my database, and I would like to order the database with respect to the key.
22
4164
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
16
3390
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then or switch-case c. you can use pointers only d. you cannot use any of the loops either.
6
1724
by: Michel | last post by:
Hi All, I need to loop through a sorted datatable. Looping is not a problem, but sorting is. The datacolumns on which I want to sort, exists of numbers, which causes the 10 to appear before the 2. This is the code I use to sort the table
25
2228
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort function that will merge like elements into a chain, so that they won't have to be recompared as many times? Thanks!
16
2766
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this: sort index, id 1000,1 1000,2 1000,3 1001,1 1001,2
3
6632
by: Harry Haller | last post by:
Hello, I want to implement a generic list which will be used to display 7 columns in a GridView. One should be able to sort, filter and page each of the 7 columns. Ideally the filter should be implemented simultaneously for multiple columns - but the data need only be sorted by a single column at a time. Sorting should be both ascending and descending. I'm currently using a DataView but it's far too slow, because there are a large...
5
3186
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
4
5325
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file (Sorts.java): import java.util.*; /**
0
10072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.