selection sort best/worst case
Author |
Message |
chilli
|
Posted: Sat Jun 14, 2008 6:13 am Post subject: selection sort best/worst case |
|
|
hi,
i have an array of these numbers:
38 71 14 29 65 7 81 44 75 27
can somebody tell me how the worst and best case look like ? i would say
bc:
7 14 27 29 38 44 65 71 81 95
wc:
95 81 71 65 44 38 29 27 14 7
is that correct? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Epic Tissue
|
Posted: Sat Jun 14, 2008 6:34 am Post subject: Re: selection sort best/worst case |
|
|
This might be slightly more noticed in the Java Help Forum.
If you wanted to sort ascending the worst case would be descending and vise versa. Or are you asking how to do it?
Mod edit: Moved to Java Help. |
|
|
|
|
|
chilli
|
Posted: Sat Jun 14, 2008 7:35 am Post subject: Re: selection sort best/worst case |
|
|
i would like t know how to implement the worst case and the best case in java of these numbers ? |
|
|
|
|
|
Zeroth
|
Posted: Sat Jun 14, 2008 8:49 am Post subject: Re: selection sort best/worst case |
|
|
Worst/best case has nothing to do with the implementation. They are merely descriptions of what the worst performance for an algorithm might be. Or conversely, best performance, since the algorithm behaves differently towards different arrangements of arrays. To give an analogy, when a runner has a bad day, its because of various conditions, not because he ran differently. The same for algorithms. Best/worst case describe how the algorithm behaves, and gives an idea on how much time it would take on two different extremes of data input.
So, with that said, there is only one implementation you need to do with selection sort. I'm not going to provide any code, since that would not let you learn at all. But here's some pseudocode, straight from wikipedia:
A is the set of elements to sort, n is the number of elements in A (the array starts at index 0)
code: |
for i ← 0 to n-2 do
min ← i
for j ← (i + 1) to n-1 do
if A[j] < A[min]
min ← j
swap A[i] and A[min]
|
I hope that helps! |
|
|
|
|
|
|
|