|
-
Nov 7th, 2002, 10:44 PM
#1
Thread Starter
Lively Member
Multi-Dimensional Array Sorting
I'm tryring to sort a [300][2] array based on the data held in positon 2 of the array.
This is the routine I have come up with but it's a no go. Any help would be great.
PHP Code:
for (i=1;i>=MAXARRAY-1;i++) {
for(i2=0;i2<MAXARRAY;i2++) {
if(iTF[i2][1] > iTF[i2 + 1][1]) {
iTemp = iTF[i2][0];
iTemp1 = iTF[i2][1];
iTF[i2][0] = iTF[i2 + 1][0];
iTF[i2][1] = iTF[i2 + 1][1];
iTF[i2 + 1][0] = iTemp;
iTF[i2 + 1][1] = iTemp1;
}
}
}
-
Nov 8th, 2002, 08:25 AM
#2
I assume this array is of type int
C:
Code:
#include <stdlib.h>
intl qshelp(void *p1, void *p2)
{
int i1 = *(((int*)p1)+1);
int i2 = *(((int*)p2)+1);
if(i1 < i2) return -1;
if(i1 == i2) return 0;
return 1;
}
// sort the array:
qsort(array, 300, qshelp);
C++:
Code:
#include <algorithm>
using std::sort;
class pred_s2dar {
bool operator()(int *ar1, int*ar2) {
return (ar1[1] < ar2[1]);
}
};
// sort the array:
sort(array, array+300, pred_s2dar());
The advantage is that both methods use the quicksort algorithm, which is much faster than bubble sort.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 09:26 AM
#3
Thread Starter
Lively Member
That's all fine and dandy, but I want to know what is actually wrong with my bubble sort code. My program win't even go into the routine, it first line is wrong in someway i believe.
Thx,
Steve
-
Nov 8th, 2002, 09:35 AM
#4
i=1;i>=MAXARRAY-1;i++
You need a < here.
And C/C++ arrays begin idexing with 0, so you have to change your code to watch for that.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 09:55 AM
#5
Monday Morning Lunatic
I thought std::sort used a merge sort?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 8th, 2002, 10:03 AM
#6
I don't know. MSDN says nothing about theirs and I don't have access to a C++ standard. It might depend on the implementation. VC++7's sort takes O(N log N), maybe you can figure it out...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 10:18 AM
#7
Frenzied Member
Code:
// sort the array:
qsort(array, 300,sizeof(int), qshelp);
qsort has 4 arguments.
-
Nov 8th, 2002, 10:28 AM
#8
Frenzied Member
Oh FWIW - consider using builtin routines for sorting. Like the ones CB mentioned.
Bubble sorts are great for classwork, but get you nowhere in the real world. We have to sort our 'ad hoc' data extractions that routinely have 30-60 million records in each of 12 files. If we used the bubble sort, the ad hoc for January 2002 would just now be finishing....
We use the HPUX sort package. Even then, it takes quite a while and uses 20GB+ of disk. When we have yearend files, they get sorted on the mainframe.
-
Nov 8th, 2002, 08:11 PM
#9
Yes, bubble sort is very easy to understand and implement, but it has the worst performance of all sorting algorithms I know.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 9th, 2002, 06:01 AM
#10
Monday Morning Lunatic
It beats quick sort on mostly-sorted data. For example, you have a dataset that's unsorted, use quick sort or something to get it into order, then you can use a bubble sort once you've made any changes you want to keep it sorted.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 11th, 2003, 05:24 PM
#11
Frenzied Member
You are right, Parksie... It also depends on how many items you have to sort. That is why there is even the knut rule for the Shell Sort.
Anyway... I was here again searching for code for translating the Automaton program. (Yes, I have translated the class State so far and now I am translating the class Automaton) and I need to sort a vector of strings. And yes, I found "algorithm.h" but the example i found was using the size of the string to sort it. How can I sort it alphabetically?
I tried the following:
PHP Code:
#include <string>
#include <vector>
#include <algorithm>
// compare strings by length, descending order
bool compare(string const& x, string const& y) {
return x < y; //x.size() > y.size();
}
void SortVector(vector<string> V){
sort(V.begin(), V.end(), compare);
}
What am I doing wrong? I don't want to program the Flagged-Bubble Sort... I already programmed MID, InStr and all those functions I would use on VB...
We miss you, friend...  Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
-
May 11th, 2003, 05:54 PM
#12
Frenzied Member
Oops.. I found one post by Parksie that showed me that...
But now my question is... Why if I make it a procedure it won't sort it but when I do it just like that it sorts it?
Example:
PHP Code:
#include <string>
#include <vector>
#include <algorithm>
void SortVector(vector<string> V){
sort(V.begin(), V.end());
}
void main(){
vector<string> States;
States.insert(States.end(), "s5");
States.insert(States.end(), "s4");
States.insert(States.end(), "s3");
States.insert(States.end(), "s2");
//This is an example, so don't think I had it this way.
//I just did not want to add the GetArrows code.
SortVector(States); //This won't sort it
sort(States.begin(), States.end()); //This will work
}
So why doesn't it work if it is called like I did? I mean, it is easier for me to just give the vector I want to sort it and then sort it with a procedure like that. But I had to do it the other way... I am just curious why wouldn't this approach work... 
PS: Which is the most comfortable way for you guys to see the code?
We miss you, friend...  Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
-
May 12th, 2003, 06:55 AM
#13
Monday Morning Lunatic
Look very carefully at your argument list for SortVector 
Also, why add another function just to "rename" sort() ?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 12th, 2003, 07:24 AM
#14
Actually, less<> with strings should sort lexicographically...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 12th, 2003, 03:44 PM
#15
Frenzied Member
True, Parksie it should be:
Code:
void SortVector(std::vector<string> V){
sort(V.begin(), V.end());
}
But I am not using it... Thank you anyway..
We miss you, friend...  Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
-
May 12th, 2003, 03:50 PM
#16
Monday Morning Lunatic
Heh, nope. Still wrong. Try:
Code:
void SortVector(std::vector<string> &V){
sort(V.begin(), V.end());
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 12th, 2003, 06:18 PM
#17
Frenzied Member
I will, but why...? Is it like "ByRef" and "ByVal" that is in VB?
We miss you, friend...  Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
-
May 12th, 2003, 06:24 PM
#18
Monday Morning Lunatic
Basically, yes.
The & tells it to pass that argument by reference, hence when you do something to V, it acts on the original V. If you had it as const, then you would not be able to change it, and the effect would be of pass-by-value, except without the copying overhead (hence why const references are used so much in C++).
You can use * to pass by pointer (references do not exist in C, so pointers are the only way to achieve this same effect). This requires "dereferencing" that pointer before use, with the * operator that I'm sure you're familiar with. You can use const in C++ with this as well.
No modifiers passes by value, and will copy the entire class/structure/type before the function starts. Alterations are permitted, but are fairly useless most of the time because they don't affect the caller.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 12th, 2003, 06:33 PM
#19
Frenzied Member
Oh... Well, thank you.
Now I understand. I thought something like that might be happening. Please be patient to me... My C++ teacher was an ass and he never taught us anything. So all I know is because of practice. Not like on VB that I could take the book and grasp it quickly (It is because I used to work with Basic when I was in junior High and they did not teach us C nor C++ )
We miss you, friend...  Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
-
May 13th, 2003, 06:12 PM
#20
Addicted Member
what does lexicographically mean?
-
May 13th, 2003, 06:18 PM
#21
Monday Morning Lunatic
Dictionary-style, basically.
Lexicon == dictionary. The 'lex' prefix is what determines it, I think.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 14th, 2003, 01:24 AM
#22
latin lex, legis (f.)
1) a set form of words , contract, covenant, agreement
2) a law, rule
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|