Pointers and Dynamic Memory Allocation

Hello,
I need some help sorting the values i have stored in my pointer array

Last edited on
Try this link: http://www.algolist.net/Algorithms/Sorting/Bubble_sort
Second hit on Google search for 'bubble sort' ;)
Bubble sort is probably one of the worst sorting algorithms ever, and it's not even that much easier than better ones.
I think it's a fair assumption that @johnhuge is trying to implement a bubble sort :)
And he's not that far away from getting it to work.
Yes, but if he isn't required to implement bubble sort it wouldn't hurt doing it better, now would it?
thanks for the replay but what i am having trouble is that I can't use square brackets any where in the program, for instance list[]. I have to write it like this *(list+i), like i did above in the code. so how would i go about doing the bubble sort. If someone could give me an example that would help.

thanks for the replay but what i am having trouble is that I can't use square brackets any where in the program, for instance list[].


What? Did you run over the dog of your prof or something? That sounds like a pretty useless requirement to me.
It it a horrible restriction for the program i have to write. That is the reason I am having trouble. So if anyone could give me an example from the code i have provided that will help me greatly.
*(list + i)
is the same as
list[i]
Last edited on
hanst99
Yes, but if he isn't required to implement bubble sort it wouldn't hurt doing it better, now would it?

If you're two or three lines away from successfully implementing a bubble sort, I'd have said the best thing would be to fix that first, then move onto something more advanced.
keineahnugh thanks for the replay. I know that *(list+i) is same as list[i]. But in the sort function how would i add x variable that i did for the loop. Also I can't use bool because i am writing this program in C.

Thanks for the reply keineahnung. would the sort function look something like this

[
Last edited on
Hi
For bool just substitute an int. If it's 0 it's false, anything else is true. That's all an 'if' statement checks for.
Your function header's now wrong - before it was correct: void sort( int* list, int count )
All you've got to do is copy that function I linked to, swap bool for int and then make some slight alterations to the pointer stuff.
I'd suggest you copy and paste it into your program and get it working first, then make the alterations, one line at a time, re-compile and confirm it still works between each change.
So for example, this line:
if (arr[i] > arr[i + 1]) {
is going to become:
if ( *(arr + i) >
etc.
Note in that function that you don't just loop through the list once. That's not how bubble sort works. You need to go through it over and over again until the lower values (or upper, depending on how you're sorting) 'bubble' up to the top. That's why the for loop's enclosed within a while loop. The answer's all in that link.
Can't really say much else without writing it for you ;)

Keineahnung thanks for the replay. I tired to do what you said but I can't get it working can you show me how i would write it.
@johnhuge
Why have you removed all the code from your previous posts in this thread?
Are you also required to use an array? Otherwise you could just plop the data into a vector and use it's sort function :/
Topic archived. No new replies allowed.