DEV Community

Neelakandan R
Neelakandan R

Posted on

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay