Hello, I have to write a dry like sorting, maybe it's similar to Radix sort and it's not homework because I made it The problem is myself and please if anabody can help me) The problem is that I have the array int x [] = new int [] 4,5,3,2,1}; Write this in binary form 5 -0101 4- 0100 3-0011 2-0010 1-0001 I want to sort this element by using btular opera or by checking each bit and if there is less exchange then someone can help me For example 5 and 4 first check the correct 0 0 == 0, then also 1 point 1 = 1 in the 1 index will survive 0 = 0 and the last one 1> 0 means that the first element is bigger then the second is the exchange
Paraphasing:
I have a question similar to Radix Doors need to create.
Suppose I have an array:
int x [] = new int [] {4, 5, 3, 2, 1};
Or, in binary format:
5-0101 4-0100 3-0011 2-0010 1- 0001
I Use these elements to sort or check each bit and exchange it (if less). For example, consider 5 and 4:
5 left in the binary or The most important bit (MSB) is 0, as is the MSB4. Since
0 == 0
is in progress. The next two bits (0 again 1) are also equivalent. Finally, at least 5 or less important bit (LSB) is 1, while 4's LSB is 0, it indicates that two values should be exchanged.
k
to a int x
To get, you can do the following:
int bit = (x> & gt; k) & amp; 1;
Here's a test:
int xs [] = {4,5,3,2,1}; For (int k = 0; k & lt; 4; k ++) {for (int x: xs) {int bit = (x> & gt; k) & amp; 1; System.out.format ("|% s |", bit); } System.out.println (); }
This print (with annotation)
x = 4 5 3 2 1 k = | 0 | | 1 | | 1 | | 0 | | 1 | 0 | 0 | | 0 | | 1 | | 1 | | 0 | 1 | 1 | | 1 | | 0 | | 0 | | 0 | 2 | 0 | | 0 | | 0 | | 0 | | 0 | 3
Here is a very tricky bit, i.e. a int
on beat 31. When it is 1
, it is a negative number symbol. You already want to implement an implementation that only works for positive int
, and then can add support for the negative number.
Comments
Post a Comment