Hi....
I'm a new member .

I've a code , but there is a part I didn't undestand it.

Can u help me to undestand it?

I am writing the part I didn't understand it by red color:

import javax.swing.JOptionPane;

    public class SortingArray

 {

     public static void main(String[] argus){

         int i,j,temp;


         int[] intArray = new int[7];


         for(i = 0; i < intArray.length; i++){

             intArray[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter the number for location " + i));

            }

            System.out.println("Array content before sorting ");

            for (i = 0 ; i< intArray.length;i++)

            System.out.print(intArray[i] + " ");

            System.out.println("");
   

            for (i = intArray.length-1; i>=0 ; i--)

            for (j = 0 ; j < i ;j++)

            if (intArray[j] > intArray[j+1]){

                temp = intArray[j];

                intArray[j] = intArray[j+1];

                intArray[j+1] = temp;}
        

                System.out.println(" Array content after sorting");

                for(i=0 ; i < intArray.length;i++)

                System.out.print(intArray[i] +"");

            }

        }

Thank u for helping me

Recommended Answers

All 7 Replies

I've a code , but there is a part I didn't undestand it.

Can u help me to undestand it?

I am writing the part I didn't understand it by red color:

The red part sorts the integer array intArray and then prints the sorted contents... Anything more specific you want to know about the algorithm? Then google for "bubble sort".

This is the exact bubble sort, but there are more effective sorting algorithms... quick sort, for example. And the red part shows element swapping. And outputs final results...

Thank u for helping me.
I knew this is sorting , but I didn't know how we wrote it .

Can u explain for me that, please?

Thank u.

Thank u for helping me.
I knew this is sorting , but I didn't know how we wrote it .

Can u explain for me that, please?

Thank u.

You have the code right in front of you, just read it. Trace the loops if necessary to see how the elements are being compared and sorted. Add more println output to show you what is going on in the inner loop. You'll learn more if you walk through and understand it on your own.

Tank u.

But, really I tried to understand it. But, I did not.

for (i = intArray.length-1; i>=0 ; i--) { // sort at end of list

            for (j = 0 ; j < i ;j++) { // "bubble" the largest value to the end

                if (intArray[j] > intArray[j+1]) { // compare each element to the next one

                    temp = intArray[j]; // swap the elements by first setting a temp variable

                    intArray[j] = intArray[j+1]; // then copying one to the other

                    intArray[j+1] = temp; // and recopying the temp
               }
        }
                   // print out the results
                   System.out.println(" Array content after sorting");

                   for(i=0 ; i < intArray.length;i++) { // for each element

                             System.out.print(intArray[i] +""); // print it!

                 }
       }

Thank u very much Mr.darkagn,

I'll never foget this for u. Really u help me.

Thanks a lot..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.