By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

sorting arrays in ansi C

P: 3
i m having problems with sorting arrays related to each others.
i m reading the inputs from a file named input.txt and it is structured like
30 67
12 799
1 555
...
and its length i not defined it may varies. I think i can transform these inputs into two array. For ex:
arrayA={30,12,1,...}
arrayB={12,799,555,...}

and when i sort arrayB like arrayB={799,555,12,...} (from big one to small one) i want arrayA like arrayA={12,1,30,...} (i do not want to change the order.. I want to do that in ansi C.. in the forum there are some codes I've seen but they are not for ansi C i think.

if anyone can help me , I would be so pleased (if you give some examples of codes, I would be so so pleased :) ) thanks for your attention and help.

---
bkisa -
Jun 8 '07 #1
Share this Question
Share on Google+
7 Replies


ilikepython
Expert 100+
P: 844
i m having problems with sorting arrays related to each others.
i m reading the inputs from a file named input.txt and it is structured like
30 67
12 799
1 555
...
and its length i not defined it may varies. I think i can transform these inputs into two array. For ex:
arrayA={30,12,1,...}
arrayB={12,799,555,...}

and when i sort arrayB like arrayB={799,555,12,...} (from big one to small one) i want arrayA like arrayA={12,1,30,...} (i do not want to change the order.. I want to do that in ansi C.. in the forum there are some codes I've seen but they are not for ansi C i think.

if anyone can help me , I would be so pleased (if you give some examples of codes, I would be so so pleased :) ) thanks for your attention and help.

---
bkisa -
Let me see if I know what you need:
You have a pair of numbers (arrayA, and arrayB) and you want to sort them using the integers in arrayB, but you need the integers in arrayA to follow their corresponding integer in arrayB. Right?
Jun 8 '07 #2

P: 3
Let me see if I know what you need:
You have a pair of numbers (arrayA, and arrayB) and you want to sort them using the integers in arrayB, but you need the integers in arrayA to follow their corresponding integer in arrayB. Right?
yes. I have a pair of numbers.for ex:
arrayA={765,112,444,999}
arrayB={33,12,9,77}
equivalent of the 765 (in arrayA) is 33 (in arrayB) and equivalent of the 112 (in arrayA) is 12 (in arrayB), and so on. When I change the order of the arrayB(ascending, descending or .... ) I do not want to change the equivalents. For ex:
when i change arrayB like
arrayB={77,33,12,9}
i want
arrayA={999,765,112,444}

again 77--> 999 , 33 -->765 ...
I want to that in ansi C :) thanks a lot..
best regards
--
bkisa-
Jun 8 '07 #3

ilikepython
Expert 100+
P: 844
yes. I have a pair of numbers.for ex:
arrayA={765,112,444,999}
arrayB={33,12,9,77}
equivalent of the 765 (in arrayA) is 33 (in arrayB) and equivalent of the 112 (in arrayA) is 12 (in arrayB), and so on. When I change the order of the arrayB(ascending, descending or .... ) I do not want to change the equivalents. For ex:
when i change arrayB like
arrayB={77,33,12,9}
i want
arrayA={999,765,112,444}

again 77--> 999 , 33 -->765 ...
I want to that in ansi C :) thanks a lot..
best regards
--
bkisa-
How are you sorting the array? If you have a function then you could modify that, but if not you could use an STL map.
Jun 8 '07 #4

P: 3
How are you sorting the array? If you have a function then you could modify that, but if not you could use an STL map.
I did not have a function to sort the arrayB :) but most probably i will sort the numbers from the biggest to the smallest.. Would you mind writing some code that sorts the arrayB from the biggest to the smallest and also does not changes the equivalents.... poffss I am on a razor :S thank you
(i m working with ansiC just i want to mention :)) )
best regards
---
bkisa-
Jun 8 '07 #5

ilikepython
Expert 100+
P: 844
I did not have a function to sort the arrayB :) but most probably i will sort the numbers from the biggest to the smallest.. Would you mind writing some code that sorts the arrayB from the biggest to the smallest and also does not changes the equivalents.... poffss I am on a razor :S thank you
(i m working with ansiC just i want to mention :)) )
best regards
---
bkisa-
Yea, since you are using just C, you won't be able to use maps but you can begin by writing a sorting function. Is speed a factor? If not you can use simple algorithms like the selection sort and the Bubble sort
However, if you need speed, you could look at more complicated algorithms on Google.
Jun 9 '07 #6

Expert 10K+
P: 11,448
Yea, since you are using just C, you won't be able to use maps but you can begin by writing a sorting function. Is speed a factor? If not you can use simple algorithms like the selection sort and the Bubble sort
However, if you need speed, you could look at more complicated algorithms on Google.
What is the range and type of those numbers in both array A and array B? If
both fit in an unsigned short there's an extremely cheap hack for this.

kind regards,

Jos

edit: I apologize; I replied to the wrong person. The question is still valid though.
Jun 9 '07 #7

P: 2
There are many sort algo available ready to use in the net.. Search google on quicksort, combsort and bublesort algo.. And i think there are also a sort procedure in crt library.

Get PellesC Compiler

Trimitra Media Advertising
Jun 9 '07 #8

Post your reply

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