-
December 16th, 2014, 12:23 PM
#1
Specify how many times elements of array will be compared [Bubble sort]
Hi there,
I have to specify how many times will be compared the two array elements in Bubble sort.
Code:
Array[1,...n]
1. for i=1 to n-1
2. do for j=1 to n-i
3. do if A[j]>A[j+1]
4. then swap A[j] with A[j+1]
I wonder how I can specify that how many times if statement (in line 3)will be used.
It depends how many data we have(array length), how much it is not sorted, how big data is e.g. -1000 to 1000.Is not it?
What I have done?
I simulated this situation in Java.
1000 times I filled the array with (random.nextInt) data 0 to 9, -9 to 9, 0 to 99, -99 to 99 to 10 and 100 array length.
I simulated 10 times each situation.
Results:
array length = 10
Data Average compare
0 to 9 20
-9 to 9 21
0 to 99 22
-99 to 99 22
array length = 100
Data Average compare
0 to 9 2203
-9 to 9 2342
0 to 99 2452
-99 to 99 2461
So when the array length is 10 average=(near)length*2 but when array length is 100 average=(near)length*2,3
Should it help me or this information is worthless?
-
December 16th, 2014, 02:10 PM
#2
Re: Specify how many times elements of array will be compared [Bubble sort]
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 16th, 2014, 02:48 PM
#3
Re: Specify how many times elements of array will be compared [Bubble sort]
Ok thank you very much. Can I ask you something? Why they write that time only depends on array length not data. I thought that numbers 5 4 8 7 will be more helpful than 1 -500-1 278 They do not explain why
-
December 16th, 2014, 11:33 PM
#4
Re: Specify how many times elements of array will be compared [Bubble sort]
Originally Posted by Snape
Can I ask you something? Why they write that time only depends on array length not data. I thought that numbers 5 4 8 7 will be more helpful than 1 -500-1 278 They do not explain why
You're looking at a version of bubble sort that has a fixed number of iterations based on the number of elements. Therefore, even if the elements are nearly sorted, the same number of iterations will be made. What would be different is that there will be fewer swaps, but there will be the same number of comparisons since you do the same number of comparisons per iteration.
If you used a version of bubble sort that terminates when it detects that no swaps have been made on the more recent iteration, then indeed the algorithm will perform better on a nearly sorted array of elements. However, compared to the algorithm you showed, it will still have the same performance in the worst case.
-
December 17th, 2014, 10:49 AM
#5
Re: Specify how many times elements of array will be compared [Bubble sort]
Ok thank you. Now I get it
-
December 18th, 2014, 03:37 AM
#6
Re: Specify how many times elements of array will be compared [Bubble sort]
Originally Posted by Snape
So when the array length is 10 average=(near)length*2 but when array length is 100 average=(near)length*2,3
Should it help me or this information is worthless?
Your test shows that the algrithm has O(N^2) complexity indeed.
When you make the array 10 times bigger the test takes 10^2 = 100 times longer.
This trend should stick. For example if you make the array 25 times larger (array length = 250) the test will take 25^2 = 625 times longer.
Note that complexity measures are not exact, they show the overall trend. But it's a valuable hint in practice as to how your program will scale when the the input load is increased (like for example a much bigger array is used).
Tags for this Thread
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
|