DEV Community

Neelakandan R
Neelakandan R

Posted on

4 4 4 4 2

program

Changing order of number

package pratice;

public class arr192837 {
    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] result = new int[arr.length];
        int left = 0;
        int rigth = arr.length - 1;
        for (int i = 0; i < arr.length; i++) {

            if (i % 2 == 0)
                result[i] = arr[left++];
            else
                result[i] = arr[rigth--];
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(result[i] + " ");
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Output:
1 9 2 8 3 7 4 6 5

Combine two array and duplicate element:

package pratice;

public class combine_2arr {
    public static void main(String[] args) {
        int[] arr1 = { 1, 2, 3, 4, 5, 6 };
        int[] arr2 = { 6, 7, 4, 9, 10 };
        for (int j = 0; j < arr1.length; j++) {
            int key = arr1[j];
            for (int i = 0 ; i < arr2.length ; i++) {
                if (key == arr2[i]) {
                    System.out.println("duplicate element in arr1 and arr2 = "+arr2[i]+" ");
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
duplicate element in arr1 and arr2 = 4
duplicate element in arr1 and arr2 = 6

bubblesort

package afterfeb13;

public class bubblesort {
    public static void main(String[] args) {
        int[] score = { 1,-1,1,-1,1,-1,1,-1 };

        for (int j = 1; j < score.length; j++)// running 7 time

        {
            for (int i = 0; i < score.length - j; i++) // j or 1 score.length-j change to index because index star from
                                                        // [0]
            // why j order index--decrese when number order ex{80,70,60,50,80,90,90} then
            // index decreaseex{80,70,60,50,80},
            {
                if (score[i] > score[i + 1])// 0>1,1>2,2>3,3>4
                {
                    int temp = score[i];
                    score[i] = score[i + 1];
                    score[i + 1] = temp;
                }

            }

        }
        for (int i = 0; i < score.length; i++) {
            System.out.print(score[i] + " ");
        }
    }

}

Enter fullscreen mode Exit fullscreen mode

Output:
-1 -1 -1 -1 1 1 1 1

AWS Industries LIVE! Stream

Watch AWS Industries LIVE!

Discover how cloud technology is solving real-world problems on Industries LIVE!

Learn More

Top comments (0)

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay