|
-
Apr 30th, 2002, 05:53 PM
#1
Thread Starter
Addicted Member
Bubble sort functionality
I've been reading some stuff about the bubble sort algorithm, and I have a question. Lets says for example you have 5 elements stored in an array: [7, 3, 8, 9, 4]. If you use the bubble sort algorithm, then it means that you are going to loop a total of 4 times (number of elements - 1): lets call this "loop 1", and inside this loop, there will be another loop ("loop 2"), which is in charge of detecting if the current element in the array is bigger then the one followed by it, and if it is, switch them.
I did the same process that the algorithm does on a piece of paper, and I got to the final solution (all elements sorted) the second time loop 1 was executed. What I mean to say is that all numbers were sorted, before the main loop had finished (before it looped through all 5 elements).
My question is: does the algorithm continue to excecute itself until the whole loop has finished? If so, then is it not wasting time-memory-resources, etc...??
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|