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

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 1954
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...@hotmail.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...@gmail.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*********@gmail.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*********@gmail.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*******@hotmail.com <ro*******@hotmail.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*******@hotmail.com <ro*******@hotmail.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*******@hotmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.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.SortStable( 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.netwrote in message
news:eQ**************@TK2MSFTNGP06.phx.gbl...
See:

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

<ro*******@hotmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.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
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...
22
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)...
16
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...
6
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...
25
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...
16
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:...
3
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...
5
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...
4
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.