I've read multiple articles on how Selection Sort and Insertion sort work, and believe I understand their implementations. Selection sort iterates over the unsorted numbers in the inner loop, whereas insertion sort iterates over the sorted numbers in the inner loop. From what I understand, that's basically the only difference.
My question lies in the scenario where you're posed an input array, lets say it's this one:
Input Array: 30, 70, 40, 60, 50
Now, you're given a further list where the iterations are shown:
30, 70, 40, 60, 50
30, 40, 70, 60, 50
30, 40, 50, 60, 70
30, 40, 50, 60, 70
How is one meant to identify whether Insertion Sort or Selection sort has been used based PURELY on this? There is no code given, nor are we required to write any code. We are only required to choose which algorithm has been used from a multiple choice list. (Yes, both appear in the list).
To be clear, this is not an assignment question. However, this is assisting me with revision for an exam.
Think about what happens in each of the algorithms: selection sort always selects the minimum of the unsorted elements and adds it to the end of the sorted elements; insertion sort always takes the first of the unsorted elements and inserts it in the correct place in the sorted list.
Selection sort:
Sorted | Unsorted
| 30 70 40 60 50
30 | 70 40 60 50 # selects 30, the minimum unsorted element
30 40 | 70 60 50 # selects 40
30 40 50 | 70 60 # selects 50
30 40 50 60 | 70 # selects 60
30 40 50 60 70 | # selects 70
Insertion sort:
Sorted | Unsorted
| 30 70 40 60 50
30 | 70 40 60 50 # inserts 30, the first unsorted element
30 70 | 40 60 50 # inserts 70
30 40 70 | 60 50 # inserts 40
30 40 60 70 | 50 # inserts 60
30 40 50 60 70 | # inserts 50
The arrays listed in each iteration would be the concatenation of the sorted and unsorted portions of the array. It looks like these iterations show neither selection sort nor insertion sort.
After speaking with the lecturer via email, I have a solution to this question. This is indeed a Selection Sort, with the elements therefore being swapped in place. (See https://en.wikipedia.org/wiki/Selection_sort).
Now, for the explanation:
Selection Sort:
Input Array: 30, 70, 40, 60, 50
Sorting:
30, 70, 40, 60, 50 // 30 is already sorted.
30, 40, 70, 60, 50 // Swap 40 and 70.
30, 40, 50, 60, 70 // Swap 70 and 50.
30, 40, 50, 60, 70 // Array is sorted.
Here's what it looks like for an insertion sort:
Input Array: 30, 70, 40, 60, 50
Sorting:
30, 70, 40, 60, 50 // 30 is inserted.
30, 70, 40, 60, 50 // 70 is inserted.
30, 40, 70, 60, 50 // 40 is inserted.
30, 40, 60, 70, 50 // 60 is inserted.
30, 40, 50, 60, 70 // 50 is inserted.
Array is now sorted.
I hope this helps anybody else that may come across a similar problem in the future while undertaking an algorithms course at college or university.
Related
This is following my previous question . Assuming that I finally got that cellarray:
C =
[10 20 30 40 50]
[10 20 30]
[10 20 30 40 50 60 70 80]
[10 20 30 40 50 60 70]
[Empty matrix 1x0]
[10 20 30 40]
[10]
How can I get to dump all these values in a single numeric array like this:
D = [10 20 30 40 50 10 20 30 10 20 30 40 50 60 70 80 10 20 30 40 50 60 70 10 20 30 40 10]
Thanks in advance for your input!!
You can use:
D = horzcat(C{:})
EDIT
Thanks to #thewaywewalk, even simpler:
D = [C{:}]
It's hard to know what terms to search for on stackoverflow for this problem. Say you have a target array of numbers like [100, 250, 400, 60]
I want to be able to score the closeness other arrays have to this target based on a threshold / error bars of say 10. So for example, the array:
[90, 240, 390, 50] would get a high score (or positive match result) because of the error bars.
The order matters, so
[60, 400, 250, 100] would get zero score (or negative match result)
The arrays can be different sizes so
[33, 77, 300, 110, 260, 410, 60, 99, 23] would get good score or positive match result.
A good way to think about the problem is to imagine these numbers are frequencies of musical notes like C,G,E,F and I'm trying to match a sequence of notes against a target.
Searching stackoverflow I'm not sure is this post will work, but it's close:
Compare difference between multiple numbers
Update 17th Jan 2015:
I failed to mention a scenario that might affect current answers. If the array has noise between those target numbers, I still want to find a positive match. For example [33, 77, 300, 110, 260, 300, 410, 40, 60, 99, 23].
I believe what you're looking for is sequence similarity.
You can read about them on this wikipedia page. Your case seems fit to local alignment category. There's some algorithm you can choose :
Needleman–Wunsch algorithm
Levenshtein distance
However, since these algorithms compare strings, you have to design your own scoring rule when inserting, deleting or comparing numbers.
Sounds like what you're looking for is the RMS error, where RMS is the square Root of the Mean Squared error. Let me illustrate by example. Assume the target array is [100, 250, 400, 60] and the array to be scored is [104, 240, 410, 55]
First compute the difference values, i.e. the errors
100 250 400 60
-104 -240 -410 -55
---- ---- ---- ---
-4 10 -10 5
Then square the errors to get 16 100 100 25. Compute the mean of the squared errors
(16 + 100 + 100 + 25) / 4 = 60.25
And finally, take the square root sqrt(60.25) = 7.76
When the arrays are different sizes, you can speed things up by only computing the RMS error if the first value is within a certain threshold, say +- 30. Using the example [33, 77, 300, 110, 260, 410, 60, 99, 23], there would only be two alignments to check, because with the other alignments the first number is more than 30 away from 100
33 77 300 110 260 410 60 99 23
100 250 400 60 --> RMS score = 178
100 250 400 60 --> RMS score = 8.7
Low score wins!
Please, put in R these datas:
S.names <- c("FXI", "XLB", "GLD", "IWM", "XLE", "XLF", "EWZ", "GDX", "XLK",
"TLT", "IYR", "QQQ", "SLV", "EWJ", "XLV", "DIA", "XHB", "EEM",
"USO", "VWO", "SPY", "EFA")
strike_vec <- structure(list(Strike = c(152, 43, 61, 11, 56, 37, 36, 159, 96,
74, 71, 27, 163, 128, 35, 44, 30, 40, 81, 19, 31, 48)), .Names = "Strike", row.names =
c("DIA", "EEM", "EFA", "EWJ", "EWZ", "FXI", "GDX", "GLD", "IWM", "IYR",
"QQQ", "SLV", "SPY", "TLT", "USO", "VWO", "XHB", "XLB", "XLE",
"XLF", "XLK", "XLV"), class = "data.frame")
As you can see, strike_vec row names are equal to S.names elements.
I would like to order strike_vec elements according to the order of S.names, that is associating each strike_vec element to the position in which you find the corresponding S.names row name.
The final result should be something like
> strike_vec.new
[,1]
FXI 37
XLB 40
GLD 159
IWM 96
...
...
...
where rownames(strike_vec.new) follows exactly the order of S.names.
How may I do?
Just use :
strike_vec[S.names,,drop=FALSE]
Which gives :
Strike
FXI 37
XLB 40
GLD 159
IWM 96
XLE 81
XLF 19
EWZ 56
GDX 36
XLK 31
TLT 128
IYR 74
QQQ 71
SLV 27
EWJ 11
XLV 48
DIA 152
XHB 30
EEM 43
USO 35
VWO 44
SPY 163
EFA 61
This works because if you index the rows of a data frame with a character vector, indexing will be based on row names.
Hello how would I get numbers randomly from a group of specific numbers using arrays in C? Say, I wanted to generate 50 numbers from a set of these numbers:
52 67 80 87 90 95
make an array A of size n to store arbitrary set eg, { 52, 67, 80, 87, 90, 95 }
save the length l of the fixed array eg, 6
make a random number r from 0 to length exclusive
item = A[r] where r = rand % l
a set of objects with keys: 12, 44, 13, 88, 23, 94, 11, 39, 20, 16, 5
Write the hash table where M=N=11 and collisions are handled using separate chaining.
h(x) = | 2x + 5 | mod M
So I did it with linear probing and got
11 39 20 5 16 44 88 12 23 13 94
which I am pretty sure is right, but how do you do it with separate chaining? I realize separate chaining uses linked lists, but how would the hash table look like?