Qsort for a particular range in C? - c

I have to q sort an array but for a given range for ex.
Given array
4 5 3 7 2 1
and then i have the range 2 & 5 this means i have to sort from index 2 all the way through 5.
Resultant array
4 5 2 3 7 1
I know we can set one bound
like
qsort(array,4,sizeof(int),compa)
this will sort the array till 3rd index but will always start from index 0. I want to start the first bound by a desired value. Any Suggestions??

Just pass address to the middle of the array.
qsort(array + 2, 5 - 2, sizeof(int), compa);

Related

How to find minimum moves to set all elements in array to 0?

I encountered a problem of counting minimum moves to set all elements in array to 0. An array consist of integer values from 0 to 6 (including 6) and the conditions are:
It is possible to change one or more subsequent values in one step (move) by the same value e.g. change values from index 5 to 8 (so indexes 5, 6, 7, 8) by adding 2 but it is forbidden to change indexes 5, 6, 8 (without 7, so they are not subsequent) by adding 2 in one step (I would have to change 5, 6 firstly and then in next step 8 to do it correctly).
Array consist of values from 0 to 6 e.g. when value is 5 and you add 3, it will be set on 1 because 6 is maximum and with value higher than 6 it ,,goes from start (0)" or if value is 1 and you substract 2, finally it will be set on 6 because 0 is minimum and with value lower than 0 it ,,goes from end (6)". In other words -1 will be equal to 6, -2 equal to 5, 8 equal to 1.
I have to write algorithm, which solves described problem and have no idea how should I start.
I tried to count occurrences of every number and then in first step set values to 0 in array from first occurrence to last but using this approach in some cases answer is wrong. For example array with values {1,3,2,2,2}. Step 1 - set 2 to 0 {1,3,0,0,0} and then in two separated moves set 1 and 3 to 0 what finally gives 3 moves. Valid answer is 2 moves because in first step values from 3 to last 2 should be decreased by 2 - {1,1,0,0,0} and then in next move decrease first two values by 1 - so 2 moves.

How to compare and add elements of an array

I'd like to take a single array lets say 3x5 as follows
1 3 5
1 3 5
2 8 6
4 5 7
4 5 8
and write a code that will create a new array that adds the third column together with the previous number if the numbers in the first and second columns equal the numbers in the row below it.
since the first two values in row 1 and 2, then add the third elements in row 1 and 2 together
so the output from the array above should look like this
1 3 10
2 8 6
4 5 15
The function accumarray(subs,val) accumulate elements of vector val using the subscripts subs. So we can use this function to sum the elements in the third column having the same value in the first and second column. We can use unique(...,'rows') to determine which pairs of value are unique.
%Example data
A = [1 3 5,
1 3 5,
2 3 6,
4 5 7,
4 5 7]
%Get the unique pair of value based on the first two column (uni) and the associated index.
[uni,~,sub] = unique(A(:,1:2),'rows');
%Create the result using accumarray.
R = [uni, accumarray(sub,A(:,3))]
If the orders matters the script would be a little bit more complex:
%Get the unique pair of value based on the first two column (uni) and the associated index.
[uni,~,sub] = unique(A(:,1:2),'rows');
%Locate the consecutive similar row with this small tricks
dsub = diff([0;sub])~=0;
%Create the adjusted index
subo = cumsum(dsub);
%Create the new array
R = [uni(sub(dsub),:), accumarray(subo,A(:,3))]
Or you can get an identical result with a for loop:
R = A(1,:)
for ii = 2:length(A)
if all(A(ii-1,1:2)==A(ii,1:2))
R(end,3) = R(end,3)+A(ii,3)
else
R(end+1,:) = A(ii,:)
end
end
Benchmark:
With an array A of size 100000x3 on the mathworks live editor:
The for loop take about 5.5s (no pre-allocation, so it's pretty slow)
The vectorized method take about 0.012s

EXCEL VBA: Removing rows from an array and adding those rows to another array

I am working in EXCEL VBA.
I have a 2 dimensional array (for this example, let's say its a 5 x 5 one-based array). This is my raw data (Array "A"):
6 7 7 8 5
9 9 9 9 7
1 3 6 9 3
7 3 2 9 9
4 9 6 5 2
I also have a separate array whose row space mirrors that of the first (e.g., a 5 x 3 one-based array). The 1st column of this array is the row number of the raw data (A). This is my meta data (Array "B"):
1 0 0
2 1 0
3 0 0
4 0 0
5 1 0
For every occurrence of "1" in the 2nd column of the meta data array (B), I need to remove the corresponding row from my raw data array (A) AND add that row to a third array (Array "C")(which will not contain any data at the beginning of this process). Therefore, in this example, I need to remove rows 2 & 5 from Array A and place them in Array C.
I also need to copy the 1st column of the Array B (the original row numbers of Array A) to both arrays A & C so that after some further processing I can re-combine the results and return the data to its original order.
I'm not sure how best to go about this. Any suggestions?
Thanks!

The meaning of target value of Leetcode Search in Rotated Sorted Array

The original problem is like this:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
The link is here https://oj.leetcode.com/problems/search-in-rotated-sorted-array/
I don't know the meaning of the 'target value' here. Is it the value we want to find or something else? Why it is given to me?
Is it the value we want to find or something else?
Yes, for example, if you have rotated array:
4 5 6 7 0 1 2
and you are given number 6, you should return 2 - the index of 6 in the array (assuming indexes start from 0). If you are given number 8, which doesn't occur in the array - return -1.

Matlab : Matrix indexing Logic

i am doing very simple Matrix indexing examples . where code is as give below
>> A=[ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ]
A =
1 2 3 4
5 6 7 8
9 10 11 12
>> A(end, end-2)
ans =
10
>> A(2:end, end:-2:1)
ans =
8 6
12 10
here i am bit confused . when i use A(end, end-2) it takes difference of two till first column and when there is just one column left there is no further processing , but when i use A(2:end, end:-2:1) it takes 6 10 but how does it print 8 12 while there is just one column left and we have to take difference of two from right to left , Pleas someone explain this simple point
The selection A(end, end-2) reads: take elements in last row of A that appear in column 4(end)-2=2.
The selection A(2:end, end:-2:1) similarly reads: take elements in rows 2 to 4(end) and starting from last column going backwards in jumps of two, i.e. 4 then 2.
To check the indexing, simply substitute the end with size(A,1) or size(A,2) if respectively found in the row and col position.
First the general stuff: end is just a placeholder for an index, namely the last position in a given array dimension. For instance, for an arbitrary array A(end,1) will pick the last element in column 1, and A(1,end) will pick the last element in the first row.
In your example, A(end, end-2) picks an element in the last row two columns before the last one.
To interpret a statement such as
A(2:end, end:-2:1)
it might help to substitute end with the actual index of the last row/column elements, so this is equivalent to
A(2:3, 4:-2:1)
Furthermore 4:-2:1 is equivalent to the list 4,2 since we are instructing to make the list starting at 4, decreasing in steps of 2, up to (minimum) 1. So this is equivalent to
A([2 3],[4 2])
Finally, the following combination of indices is implied by A([2 3],[4 2]):
A(2,4) A(2,2)
A(3,4) A(3,2)

Resources