|
-
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?
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
|