![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 4
| question abourt sort who can help? Code: void sort(int *p1, int p2)
{
............
}
Last edited by teisen; 07-02-2009 at 05:33 PM. |
| teisen is offline | |
| | #2 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Those "codes" are C++ style "comments" and this array sort looks like a very obfuscated implementation of bubble sort. Search these forums for good examples of coding bubble sort. |
| itCbitC is offline | |
| | #3 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| It better be obfuscated*. If I wasn't aware that this was just supposed to be a sort, I might have tried harder to figure it out. This is the line that bugs me: Code: y = temp+x || temp == ++x; This one just seems screwed up: Code: x = y + *p1 - *(p1+j+1); *obfuscated code is code written intentionally to be difficult or impossible for someone other than the author to understand. In this case it's mostly just the one letter variables.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 07-01-2009 at 09:41 PM. |
| MK27 is offline | |
| | #4 |
| Registered User Join Date: Jul 2009
Posts: 4
| thanks a lot for your answers,itCbitC and MK27 I only got code body actually, the function name "sort" was given by myself as i guessed that might be trying to implement array sort (bubble sort ). I have been particularly concerned about those "obfuscated code" like Code: x = y + *p1 - *(p1+j+1); y = temp+x || temp == ++x; Plus, as i have mentioned above, I want to know how much time complexity has been increased? Last edited by teisen; 07-01-2009 at 10:32 PM. |
| teisen is offline | |
| | #5 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Which means most of the material you will find should reflect this available "state of the art". All you have to do is learn the basic form of the algorithms. It might be a good idea to look at *both* pseudo code descriptions *and* actual C implementations. So pseudo-code for a modified bubblesort might be: Code: while (1) { // eg, an infinite loop
set flag to 0
for (i=0; i<the lenght of the array; i++) {
compare i to i+i;
if i+1 is less than i, swap the values and add 1 to flag
}
if the flag is still 0, break out of the loop because the array is sorted.
}
IMO bubblesort is the easiest and most "intuitive" -- that's what I did off the top of my head without knowing anything about sorting, and somebody told me it was a bubblesort. Other people have said the same thing about "selection sort". Insertion sort is pretty straight forward too. Merge sort is quite a bit trickier. And don't bother with "quick sort" until you understand merge sort.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #6 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
Not sure what you're complaining about here. *p1 = p1[0] just like *(p1+j+1) = p1[j+1]. No kidding. I'm here because I have completely failed at falling asleep and I still don't want to try to follow that code. Last edited by tabstop; 07-01-2009 at 10:32 PM. | |
| tabstop is offline | |
| | #7 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Code: 1 || puts("This won't happen!");
>>Not sure what you're complaining about here. *p1 = p1[0] I think I've "in one ear and out the other'd" this about 5 times now, probably because I perpetually use the subscripted form.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #8 | |
| Banned Join Date: Mar 2009
Posts: 533
| Quote:
Code: !1 && puts("This won't happen!");
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ Last edited by ಠ_ಠ; 07-01-2009 at 11:07 PM. | |
| ಠ_ಠ is offline | |
| | #9 | |
| Registered User Join Date: Jul 2009
Posts: 4
| Quote:
so do you mean x, y related code here like Code: x = y + *p1 - *(p1+j+1); y = x?++x:x-2; y = temp+x || temp == ++x; In addition, if i want to optimize this function and make it go faster, i will have nothing to do but choose other sort algorithm, like merge sort. right? | |
| teisen is offline | |
| | #10 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Code: for (; j<i; x++, z++)
{
if (*(int*)(p1+j) > *(int*)(p1+z))
{
// x = y + *p1 - *(p1+j+1); //1
temp = *(p1+j);
*(p1+j) = *(p1+j+1); //2
// y = x?++x:x-2;
*(p1+j+1) = temp;
t=1;
// if (x > y)
// y = temp+x || temp == ++x; //3
}
++j;
}
Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | ||
| MK27 is offline | |
| | #11 |
| Registered User Join Date: Jul 2009
Posts: 4
| >>MK27 thank you very much. ^.^) |
| teisen is offline | |
![]() |
| Tags |
| sort |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to sort a simple linked list using swap-sort method | JOCAAN | C Programming | 24 | 08-07-2009 11:48 AM |
| Straight Insertion Sort function problem | StaticKyle | C++ Programming | 6 | 05-12-2008 04:03 AM |
| Bubble Sort, Qucik Sort | insomniak | C Programming | 2 | 03-15-2003 04:54 PM |
| Radix Sort question | ripper079 | C++ Programming | 5 | 01-06-2003 06:58 AM |
| question about Quick Sort | Liberty4all | C++ Programming | 8 | 11-23-2002 10:38 AM |