• Post Reply Bookmark Topic Watch Topic
  • New Topic

Explanation of program  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the program of ascending Order --

import java.util.Scanner;
class AscendingOrder
{
public static void main(String args[])
{
int j,temp;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the no. of Integers to pick largest");
int num = scan.nextInt();
int a[] = new int[num];
System.out.println("Enter "+num+" no.s : ");
for(int i = 0;i < num ; i++)
a[i] = scan.nextInt();

for(int i =0 ; i < num -1 ; i++)
{
for(j= 0; j < num-i-1; j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println("Sorted no. are ");
for (int i = 0; i < num; i++)
System.out.println(a[i]);
}
}

I am such a douche i got stuck in

for(int i =0 ; i < num -1 ; i++)
{
for(j= 0; j < num-i-1; j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}

this part of program. This is the important part that take place for sorting ..
Where i got stuck is the operation, I took some values and tried to solve on notebook so
considering num = 5
for(int i = 0; i<4;i++)
{
for(int j = 0;j<3-i;j++)
{
if()... then comparing values and swapping ..

Once i = 0 (0<4, true )and j goes upto 0,1,2 but fails at j = 3 because 3<3-i (where i =0) will be false
it again goes to i loop making i = 1 (1<4, true )and j will be 2 so, 2<2...?? I don't know if some one will understand what i tried explaining from my side.

Also what this expression means -- int a[] = new int[num]; ?
I will be thankful if some one can give me correct explanation of this how its working ?
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't forget to UseCodeTags next time - it would make this code more readable.

This program suppose to sort an array of integers in ascending order. Not sure 100% it does the job.
Be careful with this code, some bad practices are used. Since you want to iterate through whole array, you need to iterate like this:
Also, variable name should be chosen more descriptive. Moreover, "[]" should be placed right after the data type, rather than reference variable (Java convention says that, and it actually makes more sense, since it tells you, that array will hold int primitives).
After "for" loop there are missing "{" opening and "}" closing curly braces - makes bad code indentation and it is error prone. Class name is not descriptive also. Likely there are more problems.

If it is not your code, i'd suggest you to throw it away as a bad example for analysing and learning and look for another one, more better - write yours.

 
suraj pandhi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I will use Code tags next time, Sorry I didn't knew..
And yes its not my code i want to work at operations that take inside so just picked up this program to analyse it.
Sometimes i screw up during operations that actually take place inside program, that unable me to increase my programming level
Or if someone can tell me book or where i can easily help myself for knowing operations. ?
For helping myself in operations i just thought of solving problems that can clarify me how inside operations work .. and may be help me to increase my programming level.

I screw up when programs get lengthy and implementing concepts get difficult.
So any Java doctor available for me.. this would be great relief for me..
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing: you might want to sort arrays again. So move the code where you swap two elements into a swapTwoElementsInArray(int[], int, int) method. And use that from a bubbleSort(int[]) method. Those two method might well be static methods in a utility class. That code ought not to be in your main method because that prevents you from reusing it.
 
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj pandhi wrote:Thanks for your reply. I will use Code tags next time, Sorry I didn't knew..
And yes its not my code i want to work at operations that take inside so just picked up this program to analyse it.
Sometimes i screw up during operations that actually take place inside program, that unable me to increase my programming level
Or if someone can tell me book or where i can easily help myself for knowing operations. ?


CodingBat is an easy place to build up your coding skills.

For helping myself in operations i just thought of solving problems that can clarify me how inside operations work .. and may be help me to increase my programming level.

I screw up when programs get lengthy and implementing concepts get difficult.
So any Java doctor available for me.. this would be great relief for me..


Becoming more skilled at lengthy programs is just a matter of time. Keep coding and analyzing code and it will come to you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!