confirmation of my Counting inversion experiment [duplicate] - algorithm

This question already has answers here:
Counting inversions in an array
(38 answers)
Closed 9 years ago.
I have written a mergesort(divide and conquer) algorithm and I want to use the following arrays to test if the inversion works efficiently..
So, i would just want to confirm the inversion for each of the following arrays .
1. {10,2,3,22,33,7,4,1,2} = 13
2. {4,5,6,1,2,3} = 9
3. {1,20,6,4,5} = 5
4. {3,1,2,0,4} = 5
are all these correct? I do know a question similar has been asked but I just want to confirm if my calculation was correct. with that, I can test my algorithm.Also, this is not an homework. I just want to be so so sure that I have the write inversion count so that i can test it against my code..

No.
Your output for the first case should be 22. Also this is not how you check the efficiency of your code. You should try to check in some competitive programming sites for similar kind of problems. For example there a problem in spoj for Counting inversion. Here is the link:
http://www.spoj.com/problems/INVCNT/
Try submitting it there.

Related

Stack implementation the Trollface way [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In my software engineering course, I encountered the following characteristic of a stack, condensed by me: What you push is what you pop. The fully axiomatic version I Uled here.
Being a natural-born troll, I immediately invented the Troll Stack. If it has more than 1 element already on it, pushing results in a random permutation of those elements. Promptly I got into an argument with the lecturers whether this nonsense implementation actually violates the axioms. I said no, the top element stays where it is. They said yes, somehow you can recursively apply the push-pop-axiom to get "deeper". Which I don't see. Who is right?
The violated axiom is pop(push(s,x)) = s. Take a stack s with n > 1 distinct entries. If you implement push such that push(s,x) is s'x with s' being a random permutation of s, then since pop is a function, you have a problem: how do you reverse random_permutation() such that pop(push(s,x)) = s? The preimage of s' might have been any of the n! > 1 permutations of s, and no matter which one you map to, there are n! - 1 > 0 other original permutations s'' for which pop(push(s'',x)) != s''.
In cases like this, which might be very easy to see for everybody but not for you (hence your usage of the "troll" word), it always helps to simply run the "program" on a piece of paper.
Write down what happens when you push and pop a few times, and you will see.
You should also be able to see how those axioms correspond very closely to the actual behaviour of your stack; they are not just there for fun, but they deeply (in multiple meanings of the word) specify the data structure with its methods. You could even view them as a "formal system" describing the ins and outs of stacks.
Note that it is still good for you to be sceptic; this leads to a) better insight and b) detection of errors your superiours make. In this case they are right, but there are cases where it can save you a lot of time (e.g. while searching the solution for the "MU" riddle in "Gödel, Escher, Bach", which would be an excellent read for you, I think).

How can I generate a random number between 0-5 in Swift? [duplicate]

This question already has an answer here:
How to generate a random number in a range (10...20) using Swift [duplicate]
(1 answer)
Closed 6 years ago.
I have only been coding in my off time for a couple of weeks now and am creating an app with a very basic "guess how many" concept. The user will enter a number between 0-5 and the app will tell the user whether they are right or wrong.
How would I generate a number between 0-5?
You can use any random method and performing modulo operator on it
let randomNumber = random() % 6

Can someone clarify me the differences between a Program and an Algorithm? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So, I have been doing a little research and searching around Google about Algorithms. I was getting the hang of it until I got a little deeper.
I understand that an Algorithm is defined as: a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer (Quoted from Dictionary). A Computer Program is defined as: is a sequence of instructions, written to perform a specified task on a computer (Quoted from Wikipedia)
An analogy I saw on a thread helped me a little bit:
Cake Algorithm:
--Get Ingredients
--Bake
--Serve
Cake Program:
--2fl of flour
--3 eggs
--Mix in pan
--etc.
As I saw the algorithm was more general
So basically how I began to think of a computer program was code that implements the Algorithm, in other words the Algorithm is a blueprint. For example, this is a simple Algorithm:
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum=num1+num2
Step 5: Display sum
Step 6: Stop
I am also aware after more google searches that Algorithms can be represented in pseudo-code:
if a>b
Display a is bigger than b #Simple Example, but you get the point
They can also be formed in real code (making the algorithm as you go) like this:
def foo():
#Blank Code for Algorithm/to be used later
So now this is where my question comes in, a lot of stackoverflow threads I see ask a user to explain/correct their algorithm. However, when I look at the algorithm instead of seeing something like the above examples I will see this:
// I know this isn't Python but that's not the point!
for (int i = 0; i < N; i++) {Console.Write('Hello World !');
}
No blank functions/empty code blocks, its all filled in and such
So now my questions:
Is the above code for example indeed an Algorithm? Or is it a program written that follows a Algorithm?
If it is considered an Algorithm, what is the difference between that being an Algorithm, and that code being a computer program?
If it is both, does that mean the two terms can be used interchangeably?
Any clarification to a beginner would be nice.
// I know this isn't Python but that's not the point!
for (int i = 0; i < N; i++) {
Console.Write('Hello World !');
}
Is the above code for example indeed an Algorithm? Or is it a program
written that follows a Algorithm?
Nope. If you were to remove the printing part and just add print hello world instead of Console.Write(), it could be an algorithm to print hello world N times.
If it is considered an Algorithm, what is the difference between that
being an Algorithm, and that code being a computer program?
An algorithm is language independent, it just shows how to do something, the language defines a stricter set of rules on how to implement something. A program is used to implement an algorithm considering the rules and syntax defined by a language .
If it is both, does that mean the two terms can be used
interchangeably?
Nope. An algorithm is not language specific whereas a program is always used in conjunction with a programming language.
Sample statement : Write a Java program to implement BubbleSort algorithm.
First of all welcome to StackOverflow and the world of coding.
The answers to your questions:
1.Is the above code for example indeed an Algorithm? Or is it a program written that follows a Algorithm?
The above code is a "program" . An algorithm is like you said the blueprint and the whole architecture itself.
The algorithm for the above code could be:
STEP 1: START
STEP 2: Print Hello World 5 times. (That's it actually)
STEP 3: END
You see the algorithm is something which describes a code in simple plain language. You can explain the blueprint of a program in pseudo code or a flowchart. You can't feed it to the PC without actually giving it the form the PC will accept. Therefore You need to convert it into a program using languages like C ,CPP, Python etc
2.If it is considered an Algorithm, what is the difference between that being an Algorithm, and that code being a computer program?
WELL that's is not the algorithm as described above. And the answer to remaining part is explained as above
3.If it is both, does that mean the two terms can be used interchangeably?
No, it shouldn't be because of the exact same reason stated above.
Hope You get it.
Cheers
Same difference as between a class and an instance object.

Is there a reason to use arithmetic expression n*(1/k) over n/k? [duplicate]

This question already has answers here:
Is Multiplying the Inverse Better or Worse?
(11 answers)
Closed 7 years ago.
Sometimes I encounter in arithmetic operations expression like this: n*(1/k).
Such expression can be presented in simpler manner: n/k.
I could imagine that in certain situations the former could be more descriptive if (1/k) represents well known ingredient but it is not always the case.
What about performance gains/losses? What about precision?
Is there any hidden reason that some developers use n*(1/k) form?
To me i find that using n*(1/k) will be having a lesser accurate answer because of the reason that when the control solves an produces the result of (1/k) the will be situations that may cause rounding off or trimming of the result that may lead to the loss of accuracy.And during the multiplication process the magnitude of the loss increases.Hence as far as i am concerned i will say n/k is better

Is there a way to use sort_by on a second criterion if the first is equal? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Sort strings and numbers in Ruby
I have an array of place objects, each with a name (unique) and number (non-unique). Is there a simple way to use sort_by to first sort by number and then (within each number) by name?
I know I can write a custom block for sort, but if this is possible, it'd be even easier!
Not sure if this is what you mean by "custom block" but it seems pretty simple to me:
places.sort_by { |place| [place.number, place.name] }

Resources