Specific Bubble Sort Process - sorting

I'm reviewing for an exam, one of the practice questions is as follows:
give the contents of the array after two iterations of the bubble sort (assume the lowest values are selected first to the left of the array
43 16 99 12 48 14 62
The given answer is:
12 14 43 16 99 48 62
I have been reviewing my notes trying to figure out why this is the correct answer, but I have no idea why. I have found examples of the bubble sort on google and wikipedia and while those make sense to me, they are also very simple, this is more difficult.
Can someone please explain how 12 14 43 16 99 48 62 is the answer?

I puzzled about this for a minute because indeed, it was hard to see, but once you realize how they're doing it, it's simple enough. Still, it's dumb.
We're sorting so that the lowest numbers are on the left, but we're iterating from the right. So the very first test is comparing 14 and 62, and not swapping; then comparing 48 and 14, and swapping; then 12 and 14, and doing nothing, etc. Once you get to the left end, go back to the right end and do a second pass, and you'll end up with the given solution.

Ok, this does not give the same answer as your teacher, but this is (I hope) a clear explanation of a bubble sort:
Because a picture is often a lot better than words: http://www.algolist.net/Algorithms/Sorting/Bubble_sort
In your case, after the first iteration:
43 > 16 => 16 43 99 12 48 14 62
43 < 99 => 16 43 99 12 48 14 62
99 > 12 => 16 43 12 99 48 14 62
99 > 48 => 16 43 12 48 99 14 62
99 > 14 => 16 43 12 48 14 99 62
99 > 62 => 16 43 12 48 14 62 99
Second Iteration:
16 < 43 => 16 43 12 48 14 62 99
43 > 12 => 16 12 43 48 14 62 99
43 < 48 => 16 12 43 48 14 62 99
48 > 14 => 16 12 43 14 48 62 99
48 < 62 => 16 12 43 14 48 62 99
62 < 99 => 16 12 43 14 48 62 99

Related

Equidistant Points in a Matrix

I have a matrix of floating point values. Relative to a given origin (x and y index, point "0"), I would like to obtain the indices of equidistant points, starting with the nearest points ("1") and up to a specific number ("12" in this animated example):
The distance is the slant range between a point and point 0. For example, point "4" has the distance sqrt(2^2+1^2) = sqrt(5) = 2.24.
Does anyone know a corresponding algorithm to obtain these indices in an effective way?
Rephrasing the question, you want to enumerate the points by increasing euclidean distance to the center.
Here are two answers on https://math.stackexchange.com to this problem how-to-enumerate-2d-integer-coordinates-ordered-by-euclidean-distance and algorithm-for-enumerating-grid-points-by-distance-from-given-point
Basically:
use symmetry to consider only point with 0 <= x <= y;
note that for a given x points will be enumerated with increasing y;
use a priority queue to keep the next candidate for each vertical line.
With n the last index you generate, the time complexity will be O(n log n) and the space complexity O(sqrt(n)).
NB: to avoid floating point computation, consider the squared distance, which doesn't change the order of your points.
Here some python code implementing this idea:
import heapq
def yield_all_quadrant(x, y):
s = set([(x, y), (-x, y), (x, -y), (-x, -y),
(y, x), (-y, x), (y, -x), (-y, -x)])
for u, v in sorted(s):
yield u, v
def indices(X, Y):
q = [(0, 0, 0)]
d_current = 0
index = 0
while True:
d, x, y = heapq.heappop(q)
if d > d_current:
index += 1
d_current = d
for u, v in yield_all_quadrant(x, y):
yield (X + u,Y + v), index
if not y:
heapq.heappush(q, (d + 2*x + 1, (x+1), 0))
if y < x:
heapq.heappush(q, (d + 2*y + 1, x, y+1))
and used for example in a small function to fill a grid
import itertools
def fill_grid(size, center):
grid = [[0]*size for _ in range(size)]
def clip(e):
coord, index = e
return all(0 <= c < size for c in coord)
for (x,y), i in itertools.islice(filter(clip, indices(*center)), 0, size**2):
grid[x][y] = i
return grid
and the result
print('\n'.join(' '.join('%2d'%i for i in gi) for gi in fill_grid(20, (8,8))))
54 48 43 39 35 33 31 30 29 30 31 33 35 39 43 48 54 59 67 74
48 42 38 34 30 27 26 24 23 24 26 27 30 34 38 42 48 55 62 69
43 38 32 28 25 22 20 19 18 19 20 22 25 28 32 38 43 50 56 64
39 34 28 24 21 17 15 14 13 14 15 17 21 24 28 34 39 46 53 60
35 30 25 21 16 13 12 10 9 10 12 13 16 21 25 30 35 41 49 57
33 27 22 17 13 11 8 7 6 7 8 11 13 17 22 27 33 40 47 55
31 26 20 15 12 8 5 4 3 4 5 8 12 15 20 26 31 38 45 53
30 24 19 14 10 7 4 2 1 2 4 7 10 14 19 24 30 37 44 52
29 23 18 13 9 6 3 1 0 1 3 6 9 13 18 23 29 36 43 51
30 24 19 14 10 7 4 2 1 2 4 7 10 14 19 24 30 37 44 52
31 26 20 15 12 8 5 4 3 4 5 8 12 15 20 26 31 38 45 53
33 27 22 17 13 11 8 7 6 7 8 11 13 17 22 27 33 40 47 55
35 30 25 21 16 13 12 10 9 10 12 13 16 21 25 30 35 41 49 57
39 34 28 24 21 17 15 14 13 14 15 17 21 24 28 34 39 46 53 60
43 38 32 28 25 22 20 19 18 19 20 22 25 28 32 38 43 50 56 64
48 42 38 34 30 27 26 24 23 24 26 27 30 34 38 42 48 55 62 69
54 48 43 39 35 33 31 30 29 30 31 33 35 39 43 48 54 59 67 74
59 55 50 46 41 40 38 37 36 37 38 40 41 46 50 55 59 66 73 80
67 62 56 53 49 47 45 44 43 44 45 47 49 53 56 62 67 73 79 85
74 69 64 60 57 55 53 52 51 52 53 55 57 60 64 69 74 80 85 93

In sorting quicksort if there are more than 2 same numbers..How can we sort?

If there are more than two same elements in Quicksort and pivot is also the same number...How can we sort it?
Eg: 23,19,45,21,90,5,93,45,31,45
In this array consider 45
Using Hoare like partition scheme, 45 as pivot, start with
0 1 2 3 4 5 6 7 8 9 indices
23 19 45 21 90 5 93 45 31 45 data
scan from left for >= pivot, scan from right for <= pivot
ll rr
23 19 45 21 90 5 93 45 31 45
23 19 45 21 90 5 93 45 31 45 swap (both are 45, so no change)
ll rr
23 19 45 21 90 5 93 45 31 45
23 19 45 21 31 5 93 45 90 45 swap
ll rr
23 19 45 21 31 5 93 45 90 45
23 19 45 21 31 5 45 93 90 45 swap
rr ll
23 19 45 21 31 5 45 93 90 45 rr and ll crossed, rr = 6
The next two calls will be quicksort(0,6), quicksort(7, 9)
How can we sort quicksort(7,9) ?
0 1 2 3 4 5 6 7 8 9 indices
93 90 45 again assume pivot is 45
ll rr
93 90 45
45 90 93 swap
rr ll
45 90 93 rr and ll crossed, rr = 7
The next two calls will be quicksort(7,7), quicksort(8, 9)

Is there any library or code for DES which take 7 byte key? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Do we have any library or any mechanism where we can use true 7 byte key for DES instead of 8 byte key. I need it for keys analysis in DES and 8 byte key requirement for associated library is creating problem in getting actual keys analysis.
As explained in http://en.wikipedia.org/wiki/Data_Encryption_Standard, the 8-byte key for DES is just a 56-bit key in 8 bytes with odd parity check added:
The key is nominally stored or transmitted as 8 bytes, each with odd parity. According to ANSI X3.92-1981, section 3.5:
One bit in each 8-bit byte of the KEY may be utilized for error detection in key generation, distribution, and storage. Bits 8, 16,..., 64 are for use in ensuring that each byte is of odd parity.
(end quote)
So given an 8-byte key I can generate a 7-byte key by dropping the parity check bits and reformatting, and given a 7-byte key I can generate an 8-byte key by reformatting and adding parity check bits. It should therefore be easy to produce wrappers to make a library for one key format look like a library for another - or are you having some other problem I haven't noticed?
In DES a key is comprised of 8 bytes with the LSB a parity bit, a nice and regular structure. There's an implication there for the relationship between input bytes and Permuted Choice 1, which loads the two 28 bit C and D Registers.
In parlance of the NBS standard a Permuted Choice is a selection permutation not using all the values of a greater whole.
Historically any DES implementation that used 7 bytes wasn't compatible where there are several FIPS/Nist pubs specifying key/ciphertext/plaintext triplets.
If you have a need for a 56 bit number representing a key there's an implication you're using or storing them in a tabular fashion. Otherwise you could simply guarantee the parity bit is either correct or say '0'. 'Packed' keys are only of interest for saving storage space.
All that said and done if you need a 56 bit number representing a key you could represent 8 bytes of key in a 64 bit value and after identifying the parity bit locations with respect to endian-ness, shift the 64 bit array value obliterating the parity bits one at a time in 8 operations on a 64 bit machine, leaving 56 'effective' key bits.
On a 32 bit or smaller machine you'd also have to track byte position to keep track of bit shift offsets and would have to deal with bits moving between bytes to pack bits into 7 bytes.
For a 32 bit machine you could pack two 32 bit values into 28 bits in opposite directions then merge the proper 4 bits of the second one into the first followed by shifting the second value down 8 bits.
For a big-endian bit in byte numbering system 1 to 8 with bit 8 the LSB (from the standard)
Bits 1-4 go to the C register, with only four bit 4 values from 8 successive bytes of an input key (described as an input array of bits). The D block shares bit 4 and uses bits 5-7 to derived 28 bits:
The bigger issue here may be the ability to communicate any interesting finding in terms of keys useful to those dealing with an 8 byte key representation. It may be handy to have an inverse function available as well.
There's also a relationship between round keys and the C and D concatenated block, shown in Carl Meyers and Stephen Metyas book "Cryptography, A New Dimension in Computer Security', subtitled 'A Guide for the Design and Implementation of Secure Systems', Wiley Interscience, 1982, ISBN-0-471- 04892-5.
I recreated the table using a derivative of the original BSD libcrypt source. The significance of this is that C and D bits don't mix in the two 24 bit values derived from Permuted Choice 2 (which is visible in selected key (KS) in round 16 of the table).
Bit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
KS
1 15 18 12 25 2 6 4 1 16 7 22 11 24 20 13 5 27 9 17 8 28 21 14 3
2 16 19 13 26 3 7 5 2 17 8 23 12 25 21 14 6 28 10 18 9 1 22 15 4
3 18 21 15 28 5 9 7 4 19 10 25 14 27 23 16 8 2 12 20 11 3 24 17 6
4 20 23 17 2 7 11 9 6 21 12 27 16 1 25 18 10 4 14 22 13 5 26 19 8
5 22 25 19 4 9 13 11 8 23 14 1 18 3 27 20 12 6 16 24 15 7 28 21 10
6 24 27 21 6 11 15 13 10 25 16 3 20 5 1 22 14 8 18 26 17 9 2 23 12
7 26 1 23 8 13 17 15 12 27 18 5 22 7 3 24 16 10 20 28 19 11 4 25 14
8 28 3 25 10 15 19 17 14 1 20 7 24 9 5 26 18 12 22 2 21 13 6 27 16
9 1 4 26 11 16 20 18 15 2 21 8 25 10 6 27 19 13 23 3 22 14 7 28 17
10 3 6 28 13 18 22 20 17 4 23 10 27 12 8 1 21 15 25 5 24 16 9 2 19
11 5 8 2 15 20 24 22 19 6 25 12 1 14 10 3 23 17 27 7 26 18 11 4 21
12 7 10 4 17 22 26 24 21 8 27 14 3 16 12 5 25 19 1 9 28 20 13 6 23
13 9 12 6 19 24 28 26 23 10 1 16 5 18 14 7 27 21 3 11 2 22 15 8 25
14 11 14 8 21 26 2 28 25 12 3 18 7 20 16 9 1 23 5 13 4 24 17 10 27
15 13 16 10 23 28 4 2 27 14 5 20 9 22 18 11 3 25 7 15 6 26 19 12 1
16 14 17 11 24 1 5 3 28 15 6 21 10 23 19 12 4 26 8 16 7 27 20 13 2
Bit 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
KS
1 42 53 32 38 48 56 31 41 52 46 34 49 45 50 40 29 35 54 47 43 51 37 30 33
2 43 54 33 39 49 29 32 42 53 47 35 50 46 51 41 30 36 55 48 44 52 38 31 34
3 45 56 35 41 51 31 34 44 55 49 37 52 48 53 43 32 38 29 50 46 54 40 33 36
4 47 30 37 43 53 33 36 46 29 51 39 54 50 55 45 34 40 31 52 48 56 42 35 38
5 49 32 39 45 55 35 38 48 31 53 41 56 52 29 47 36 42 33 54 50 30 44 37 40
6 51 34 41 47 29 37 40 50 33 55 43 30 54 31 49 38 44 35 56 52 32 46 39 42
7 53 36 43 49 31 39 42 52 35 29 45 32 56 33 51 40 46 37 30 54 34 48 41 44
8 55 38 45 51 33 41 44 54 37 31 47 34 30 35 53 42 48 39 32 56 36 50 43 46
9 56 39 46 52 34 42 45 55 38 32 48 35 31 36 54 43 49 40 33 29 37 51 44 47
10 30 41 48 54 36 44 47 29 40 34 50 37 33 38 56 45 51 42 35 31 39 53 46 49
11 32 43 50 56 38 46 49 31 42 36 52 39 35 40 30 47 53 44 37 33 41 55 48 51
12 34 45 52 30 40 48 51 33 44 38 54 41 37 42 32 49 55 46 39 35 43 29 50 53
13 36 47 54 32 42 50 53 35 46 40 56 43 39 44 34 51 29 48 41 37 45 31 52 55
14 38 49 56 34 44 52 55 37 48 42 30 45 41 46 36 53 31 50 43 39 47 33 54 29
15 40 51 30 36 46 54 29 39 50 44 32 47 43 48 38 55 33 52 45 41 49 35 56 31
16 41 52 31 37 47 55 30 40 51 45 33 48 44 49 39 56 34 53 46 42 50 36 29 32
This almost says your 56 bit number should be concatenated from the C and D Register values so there's a discernible relationship with round keys while allowing you to index based on C and/or D values.
I tried editing pyDes library code I just took 7 byte key and converted back to 64 bits by padding '0' to every multiple of 8th positions of bits. so the analysis on 7 byte-key actually required for algorithm hopefully is gained and the the parity bits are taken as 0 (that is not associated to my key now) :) . please comment if my key still is not solely for DES algorithm..

Undefined method '[]' nilLNilClass for Adding columns in multiarrays

I am trying to solve the Projecteuler #11 but I am running into an error when I'm trying to create a function to calculate the multiplication of every 4 numbers in a column. I am getting an error:
Project11.rb:59:in `sumvertical': undefined method `[]' for nil:NilClass (NoMeth
odError)
I feel like there is something I am easily overlooking here. I appreciate the help!
#project #11 http://projecteuler.net/problem=11
grid="08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"
grid=grid.split()
grid=grid.collect {|s| s.to_i}
multiarray=[]
i = 0
e = 19
until e > 400
multiarray << grid[i..e]
i+= 20
e+= 20
end
def sumhorizontal(x) #checks sum of all horizontal 4 elements
sum = 0
x.each {|a|
i=0
e=3
while e < a.length
if a[i..e].inject(:*) > sum
sum = a[i..e].inject(:*)
i += 1
e += 1
else
i += 1
e += 1
end
end
}
return sum
end
def sumvertical(x)
sum = 0
i=0
e=0
while e < x.length #Will break once the end point is longer than the length of an array
until i > 20 #Checks the first column
if x[i][e]*x[i+1][e]*x[i+2][e]*x[i+3][e] > sum #Error is here
sum = x[i][e]*x[i+1][e]*x[i+2][e]*x[i+3][e]
i += 1
else
i += 1
end
end
e += 1 #once you are out of the until statement, it increases e by 1 to check the next column
i = 0 #resets i so it can go back to the zero
end
return sum
end
print sumvertical(multiarray)
The grid has 20 rows. Your loop is actually trying to reach all the way to a 24rd row; that's because it goes through 21 iterations (i starts at 0, and goes until it equals 21), and each iteration reaches 3 beyond the current value of i (when you call x[i+3]). When i is 17, your code will break, because x[i+3][e] is trying to index into the 21st row of x. i+3 is 20, but the highest available index is 19. So what happens is, x[20] returns nil, and then the [] method is called on nil, which generates your error.
Also, the standard library has a transpose method that you can call on your array. If you use it, you just need one method (sumhorizontal). You can get the column sums with sumhorizontal(multiarray.transpose).
One more thing... it looks like you're coming from a procedural language. Ruby has an extensive standard library and coding constructs that can save you a lot of time and keystrokes. There is typically no need to iterate with while loops and index variables in Ruby. sumhorizontal, for instance, can be written like this (it should really be called producthorizontal, though if you're trying to solve Project Euler #11:
def sumhorizontal(x)
x.map { |r| r.each_slice(4).map { |s| s.reduce(:*) }.max }.max
end
Good luck with the rest of your Ruby learning journey!

Fixed Object Id for System Objects and Small Integers in Ruby

Why do system objects like nil, true or false have a fixed object id in Ruby. Also I tried printing out the object ids of numbers, they are the same and follow an odd number sequence pattern. Any explanation for this?
[nil,true,false].each { |o| print o.object_id, ' '}
4 2 0 => [nil, true, false]
>> (0..50).each { |i| print i.object_id, ' ' }
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 => 0..50
The following two links explain the concept behind Ruby's object IDs:
http://www.oreillynet.com/ruby/blog/2006/01/the_ruby_value_1.html
http://www.oreillynet.com/ruby/blog/2006/02/ruby_values_and_object_ids.html
The object ID is calculated from the objects value plus some additional information. From that calculation you can derive the values you are seeing in your examples.

Resources