# What is the invarient of this code

• October 15th, 2012, 12:43 PM
alloka
What is the invarient of this code
i made this code but i'm stuck in :
the fundamental operation in the described algorithm? How many operations does it take?

What is the invariant of this sorting algorithm?
the code :
Code java:

```public class Ass {   public static void swap(int [] x) { int i = 0 ; while (i<x.length){   for (int j = 0 ; j<x.length ; j++){ int b = 0 ; if ( x[j] > x[i] ) { b++ ; } if (b!=0) { int tmp = x[i] ; x[i]=x[j] ; x[j]= tmp ;   }           } i++ ; } } public static void main (String [] args) { int [] array = {5,7,3,6,9,4,2} ; swap(array) ; int m = array.length ; for (int k = 0 ; k<array.length ; k++) System.out.print(array[k] + " "); } }```
• October 15th, 2012, 12:50 PM
KevinWorkman
Re: What is the invarient of this code
That's not how this works. You're going to have to tell us. We won't do your homework for you.
• October 15th, 2012, 05:18 PM
Norm
Re: What is the invarient of this code
• October 15th, 2012, 05:32 PM
copeg
Re: What is the invarient of this code
@alloka, please be respectful of other members. I have removed your post which can be construed as offensive (and has been reported as such by other members). Continued postings such as the one deleted above will force further action. If you wish to receive help, I recommend providing as much information as possible regarding your problem - the link in my signature entitled "Getting Help" may help you along these lines.
• October 15th, 2012, 06:11 PM
alloka
Re: What is the invarient of this code
so is this forum supposed for helping people or telling others what to do ?? , i asked for something and someone come and tell me we will not answer your homework , and btw this isn't a homework .
• October 15th, 2012, 06:15 PM
alloka
Re: What is the invarient of this code
and thankx for helping btw , i will not ask here again , i don't ask so others can keep info from me .
• October 15th, 2012, 06:38 PM
copeg
Re: What is the invarient of this code
Quote:

Originally Posted by alloka
so is this forum supposed for helping people or telling others what to do ?? , i asked for something and someone come and tell me we will not answer your homework , and btw this isn't a homework .

Getting help is as much (if not more) an effort on your part as it is the effort of contributors (contributors who are unpaid volunteers). Your post asked 3 questions about code. What did you try to answer said questions? What do you think the answers are? Do you understand the definitions being used in the questions? Including this information brews much better discussion, and give you a much higher chance of receiving help. Not including any of this information is suggestive no attempt was made (whether one was made or not), and is suggestive one is trying to get answers to homework (whether or not this is the case). There are hundreds of posts here, each one competing for attention of contributors. Thus providing clear and concise context rather than "here, answer this" rise to the top of posts that get answered faster.
• October 15th, 2012, 07:29 PM
alloka
Re: What is the invarient of this code
i asked specificly for the invarint , and just to let you know i searched for it all over the internet and didn't get quite a way for getting the invariant and it wasn't explained to me before so i tried to get help but sadly i didn't expect you to be like this , if i know something and one asked for it i will answer with no doubt and teach him not to do what you did .
• October 15th, 2012, 08:06 PM
copeg
Re: What is the invarient of this code
Quote:

Originally Posted by alloka
i asked specificly for the invarint , and just to let you know i searched for it all over the internet and didn't get quite a way for getting the invariant and it wasn't explained to me before so i tried to get help but sadly i didn't expect you to be like this , if i know something and one asked for it i will answer with no doubt and teach him not to do what you did .

I have done nothing other remove an inflammatory post of yours and suggest you provide more information, and yet I get accused of something...so much for trying, which I am done doing.
• October 15th, 2012, 08:10 PM
Norm
Re: What is the invarient of this code
As was stated in post#2, we don't do OPs work for them. We try to help Ops solve their java programming problems. If you have a java programming problem please ask it.
• October 15th, 2012, 09:05 PM
alloka
Re: What is the invarient of this code
yeah at least teach me how to get the invariant ??
• October 15th, 2012, 09:59 PM
Norm
Re: What is the invarient of this code
Quote:

how to get the invariant
What does that have to do with java programming?

Have you done a search for invariant? What did you find?
• October 16th, 2012, 04:33 AM
JavaPF
Re: What is the invarient of this code
Please respect what is being said or you will be banned.
• October 16th, 2012, 11:53 AM
lightOfDay
Re: What is the invarient of this code
Gosh... I don't know what to think...

This is what I found about the invariant, and I don't know if it is applicable to your problem. The invariant is either an input that is unvaried by the algorithm, or a statement that IF it holds at the beginning, will also hold during and after the algorithm, at all points in time.

What are some inputs that are not changed by the algorithm? What statements are always true during the algorithm? Here's a doozy: What are some statements that ASSUMING they are true at the beginning, will always be true within the context of the algorithm?
• October 16th, 2012, 01:49 PM
helloworld922
Re: What is the invarient of this code
The invariant is part of the contract between the user and the code. It states how you are allowed to use that code, what assumptions are made, etc. In short it spells out what requirements must be made in order to ensure that the output is always "valid". For example, a Square object requires all sides to be the same length, there are exactly 4 sides, and each side is connected to at most 2 other sides orthogonally (with right angles). Violating any of these rules breaks the invariant and you no longer have a valid Square object. Further reading: Wikipedia - Class invariant

In the context of a method invariants usually refer to the requirements of the parameters passed in. One example is a sqrt method. This method is not allowed to take in negative numbers, so the invariant is that the parameter passed in must be non-negative.

Now it's your turn: Figure out what it is you want as a contract between your method and a user. Usually this is done before you have a complete method, but that shouldn't stop you from creating a contract. Presumably you want to sort an array. What restrictions are there on that array such as size constraints, element type constraints, element value, etc.? In the contract you can also specify what happens when a violation to this invariant is made (throw an exception, try to mediate with some other value, undefined behavior, etc.), but I don't think this is part of the invariant. You can also specify in a black-box or grey-box manner what outputs and/or side-effects the method has, though again I don't think this is part of the invariant.
• October 18th, 2012, 08:54 AM
KevinWorkman
Re: What is the invarient of this code
I have given this user a week-long ban for his abusive language and poor attitude.

Alloka, you are asking other people to do your homework for you (which is academic dishonesty and can get all of us, especially you, in trouble), and ignoring all the help you're getting. You're going to need to provide more input no matter where you go- nobody, on any website, is going to do your homework for you. And even if they did, it's not really helping you. You either want to learn this stuff, or you don't. That's up to you, but don't repay our attempts at helping you with foul language and a terrible attitude. You can try harder when your ban is up, and if this behavior continues, I'll ban you indefinitely.