ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
trying to optimize code here, and no using recursion.
hereis one way,
Code:
myfunc() {
int a;
if (a==0) {
stmt1;
stmt3;
stmt2;
}
else {
stmt1;
stmt4;
stmt2;
}
}
heres's another:
Code:
myfunc() {
int a;
stmt1;
if (a==0) {
stmt3;
}
else {
stmt4;
}
stmt2;
}
now, from the above, stmt3 and stmt4 are blocks inside a loop - something like:
Code:
for (start; condition; incr/decr) {
if (var i > var j {} // this could become if (var i < var j) {}
}
the first one involves duplication of code (i would have a lot of lines which are just the same, except for maybe one conditional stmt), but it involves only one check at the beginnning, to decide which of the conditionals will be taken inside the for loop depending upon the value of 'a'.
the second one involves no duplication of code, but everytime the loop is true, a check is made to see the value of 'a'.
is there an alternate way for this where i can achieve a balance between efficiency in code-size and statements executed by my code?
ok, heres a sample code. sorting in either ascending/descending.
Code:
// ascending
if (sort_order == 0) {
for (i = num_items - 1; i >= 0; i--) {
for (j = 1; j <= i ; j++) {
if (dataset[j-1] > dataset[j])
swap (&dataset[j-1], &dataset[j]);
}
}
}
// descending
else {
for (i = num_items - 1; i >= 0; i--) {
for (j = 1; j <= i ; j++) {
if (dataset[j-1] < dataset[j])
swap (&dataset[j-1], &dataset[j]);
}
}
}
this is what i meant by the first algo i wrote above. the code is duplicated because of 1 conditional. i could have not duplicated by putting the check for sort_order inside the loop, but that is not good, as it will lead to n checks.
im sure there must be some other way to do it.
is there an alternate way for this where i can achieve a balance between efficiency in code-size and statements executed by my code?
Ah, I see what you're saying. The only thing different between the two is the greater-than or less-than comparison. You're comparing (j) with (j-1) every time, so what you could do is replace the (j-1) with, say, (j+x) where x is either +1 or -1, depending on what sort order you want to do. That way, you can just use one kind of comparison. In one case (when x is +1), you'd be comparing (j) with the next-higher index; in the other (when x is -1), you'd be comparing with the next-lower index.
You'd have to do some other small manipulations to avoid off-by-one errors, to make sure j starts out and finishes with the right values each time.
Did that make sense?
edit: Also, if you're hoping to make this an efficient sort algorithm, I'd recommend using something other than bubble-sort. That's one of the slowest ways to sort! Do a google search on sorting, for other ideas.
hey, thanks.
well, not complete sense to me (maybe im a bit tired), but I'm failing to see how the array index manipulation can help in modifying the conditional to check the values at those array indices. its not really a comparison between a higher/lower index, as it is between the values between consecutive indices.
is that a tired question on my behalf? thanks man.
yes, that is bubblesort. i just posted that here cos its the shortest. im lookin at O(nlogn) sorting algos now (done with n^2)
well, its really late here now, and i should head home. please write back with your suggestion on this.
wish y'all a merry christmas. see you day after now.
as long as j begins and ends with the correct index (j must be one less in the second example, to avoid going out of bounds). In both cases, you're asking "is the current value greater than the next value?" The +1 in the second example is the value of "x" I suggested; you just make x = -1 in order to get the correct behavior for the descending sort.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.