471,336 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

sorting arrays in ansi C

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
7 5662
ilikepython
844 Expert 512MB
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
bkisa
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
844 Expert 512MB
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
bkisa
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
844 Expert 512MB
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
JosAH
11,448 Expert 8TB
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
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.

Similar topics

7 posts views Thread by Federico G. Babelis | last post: by
7 posts views Thread by Karin Jensen | last post: by
7 posts views Thread by Rennie deGraaf | last post: by
1 post views Thread by rir3760 | last post: by
23 posts views Thread by yatindran | last post: by
3 posts views Thread by SneakyElf | last post: by
77 posts views Thread by arnuld | last post: by
5 posts views Thread by lemlimlee | last post: by
1 post views Thread by arnuld | last post: by
reply views Thread by rosydwin | last post: by

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.