Selection sort count number of swaps - sorting

http://www.cs.pitt.edu/~kirk/cs1501/animations/Sort1.html is this applet counting right? Selection sort for 5 4 3 2 1, I see 2 swaps, but the applet is counting 4 exchanges....

I guess it's a matter of definition. He is doing a swap in the end of every loop, even if he is swapping one element against itself. In his case, the swaps will be:
Original: 5 4 3 2 1
Swap pos 1 and 5: 1 4 3 2 5
Swap pos 2 and 4: 1 2 3 4 5
Swap pos 3 and 3: 1 2 3 4 5
Swap pos 4 and 4: 1 2 3 4 5
(No swap is done for the last element since that will always be in the correct place)
A simple if statement could be used to eliminate the two last swaps.

Related

Confusion about a practical example of FIFO Page Replacement Algorithm?

I'm doing some theoretical examples with different page replacement algorithms, in order to get a better understanding for when I actually write the code. I'm kind of confused about this example.
Given below is a physical memory with 4 tiles (4 sections?). The following pages are visited one after the other:
R = 1, 2, 3, 2, 4, 5, 3, 6, 1, 4, 2, 3, 1, 4
Run the FIFO page replacement algorithm on R with 4 tiles.
I know that when a page needs to be swapped in, the operating system swaps out the page which has been in the memory for the longest period of time. In practice I'll have:
Time 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 2 4 5 3 6 1 4 2 3 1 4
Tile 1 1 1 1 1 1 5 5
Tile 2 2 2 2 2 2 2
Tile 3 3 3 3 3 3
Tile 4 4 4 4
I'm not sure what happens at time = 8. I know that it won't replace page 5 and 4, but I'm not sure between 3 and 2. Since at time = 4 we have a 2, does it mean that page 3 will be replaced? Or is it that, since at time = 4, we already had 2 in the memory, therefore at time = 8 we replace 2?
FIFO (First In, First out) means here: If space is needed for a new entry, the oldest entry will be replaced. This is in contrast to LRU (Last recently Used), wherever the entry that has not been used for the longest time is replaced. Consider your memory with four tiles at time 5:
Tile Page Time of loading
1 1 1
2 2 2
3 3 3
4 4 5
At time 6, space for page 5 is needed, so you had to replace one of the pages in the memory. According to the FIFO principle, page 1 is replaced here:
Tile Page Time of loading
1 5 6
2 2 2
3 3 3
4 4 5
This event repeats itself at the time 8, the oldest page in memory will be replaced:
Tile Page Time of loading
1 5 6
2 6 8
3 3 3
4 4 5
So it is helpful here to write the time of creation while doing this assignment.

Quotient in R to hold position in a matrix

Let's assume these are our numbers and we are looking for mod for them
which we can find them using library(pracma)
> mod(c(1,4,23,13,8,9,11,27,32,2),7)
> [1] 1 4 2 6 1 2 4 6 4 2
I want to get a number to see where each number is coming from when it is a matrix?
1,1,4,2,2,2,2,4,5,1
For example; if this is an m by 7 matrix;
We know that it is on 2nd column but what row? 9 is 2nd row (2,2) but not quotient is 1, then 23 is 4th row (4,2) but quotient is 3. Finally, last element 2 is on (1,2).
I am looking for row position since I can use the mod as a column position.
I came out with this
b=c(1,4,23,13,7,9,11,27,32,2)
floor(b/7+1)
[1] 1 1 4 2 2 2 2 4 5 1

Deleting element and getting it's neighbours

I have got a sequence 1 2 3 4 5 6 ... n. Now, I am given a sequence of n deletions - each deletion is a number which I want to delete. I need to respond to each deletion with two numbers - of a left and right neighbour of deleted number (-1 if any doesn't exists).
E.g. I delete 2 - I respond 1 3, then I delete 3 I respond 1 4 , I delete 6 I respond 5 -1 etc.
I want to do it fast - linear of linear-logarithmic time complexity.
What data structure should I use? I guess the key to the solution is the fact that the sequence is sorted.
A doubly-linked list will do fine.
We will store the links in two arrays, prev and next, to allow O(1) access for deletions.
First, for every element and two sentinels at the ends, link it to the previous and next integers:
init ():
for cur := 0, 1, 2, ..., n, n+1:
prev[cur] := cur-1
next[cur] := cur+1
When you delete an element cur, update the links in O(1) like this:
remove (cur):
print (num (prev[cur]), " ", num (next[cur]), newline)
prev[next[cur]] := prev[cur]
next[prev[cur]] := next[cur]
Here, the num wrapper is inserted to print -1 for the sentinels:
num (cur):
if (cur == 0) or (cur == n+1):
return -1
else:
return cur
Here's how it works:
prev next
n = 6 prev/ print 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
/next ------------------- -------------------
init () -1 0 1 2 3 4 5 6 1 2 3 4 5 6 7 8
remove (2) 1 3 1 3 -1 0 1 3 4 5 6 1 3 4 5 6 7 8
remove (3) 1 4 1 4 -1 0 1 4 5 6 1 4 5 6 7 8
remove (6) 5 7 5 -1 -1 0 1 4 5 1 4 5 7 8
remove (1) 0 4 -1 4 -1 0 4 5 4 5 7 8
remove (5) 4 7 4 -1 -1 0 4 4 7 8
remove (4) 0 7 -1 -1 -1 0 7 8
Above, the portions not used anymore are blanked out for clarity.
The respective elements of the arrays still store the values printed above them, but we no longer access them.
As Jim Mischel rightly noted (thanks!), storing the list in two arrays instead of dynamically allocating the storage is crucial to make this O(1) per deletion.
You can use a binary search tree. Deleting from it is logarithmic. If you want to remove n elements and the number of total elements is m, then the complexity of removing n elements from it will be
nlogm

Selection Sort Algorithm swaps

I use Selection Sort to sort 100 reverse-sorted integers {100,99,98...1}. How many 'useless' swaps will our selection sort perform? A useless swap is where the array contents are unchanged because a value is swapped with itself.
To me the answer looks like zero? Because after each iteration of selection sort, I have the following: {1 99 98 .... 4 3 2 100}
{1 2 98 ..... 4 3 99 100} and so on
However, the answer is 50. How is that possible?
Consider the following list
5 4 3 2 1
1 4 3 2 5 //swap 1 with 5
1 2 3 4 5 //swap 2 with 4
1 2 3 4 5 //useless swap (3 with 3)
1 2 3 4 5 //useless swap (4 with 4)
now apply this on your list.
The last swap will be 50 and 51. Once this swap is done, all other elements are swapped. So swaps will be 1-100,2-99,3-98.....,48-53,49-52,50-51...
This completes the sorting. After 50 swaps, rest all elements are automatically sorted. So rest of the swaps are useless.

Permuting rows in an array to eliminate increasing subsequences

The following problem is taken from Problems on Algorithms (Problem 653):
You are given a n x 2 matrix of numbers. Find an O(n log n) algorithm that permutes the rows in the array such that that neither column of the array contains an increasing subsequence (that may not consist of contiguous array elements) longer than ⌈√n.⌉
I'm not sure how to solve this. I think that it might use some sort of divide-and-conquer recurrence, but I can't seem to find one.
Does anyone have any ideas how to solve this?
Heres's my solution.
1) Sort rows according to the first element from greatest to lowest.
1 6 5 1
3 3 -\ 3 3
2 4 -/ 2 4
5 1 1 6
2) Divide it into groups of ⌈√n⌉, and what is left(no more then ⌈√n⌉ groups)
5 1 5 1
3 3 -\ 3 3
2 4 -/
1 6 2 4
1 6
3) Sort rows in each group according to the second element from greatest to lowest
5 1 3 3
3 3 5 1
->
2 4 1 6
1 6 2 4
Proof of correctness:
Increasing subsequences in column 1 can happen only in single group(size is <= ⌈√n⌉),
No 2 elements of increasing subsequences in column 2 are in the same group(no more than ⌈√n⌉ groups)

Resources