Computer Science, asked by sameekshakrv, 17 hours ago

W.A.P in java to merge sort array list

Answers

Answered by samarthkrv
3

Answer:

import java.util.*;

import java.lang.*;

class mergeSortList{

public static void merge_Sort(ArrayList<Integer> numList){

 int mid;

 ArrayList<Integer> left = new ArrayList<>();

 ArrayList<Integer> right = new ArrayList<>();

 

 if (numList.size() > 1) {

    mid = numList.size() / 2;

       for (int i = 0; i < mid; i++)

           left.add(numList.get(i));

       for (int j = mid; j < numList.size(); j++)

           right.add(numList.get(j));      

    merge_Sort(left);

    merge_Sort(right);

    merge(numList, left, right);

 }

}

private static void merge(ArrayList<Integer> numList, ArrayList<Integer> left, ArrayList<Integer> right){

 ArrayList<Integer> temp = new ArrayList<>();

 int numbersIndex = 0;    

 int leftIndex = 0;

 int rightIndex = 0;

 while (leftIndex < left.size() && rightIndex < right.size()) {

   if (left.get(leftIndex) < right.get(rightIndex) ) {

           numList.set(numbersIndex, left.get(leftIndex));

           leftIndex++;

       } else {

           numList.set(numbersIndex, right.get(rightIndex));

           rightIndex++;

       }

       numbersIndex++;

   }

   int tempIndex = 0;

   if (leftIndex >= left.size()) {

       temp = right;

       tempIndex = rightIndex;

   }

   else {

       temp = left;

       tempIndex = leftIndex;

   }

 

   for (int i = tempIndex; i < temp.size(); i++) {

       numList.set(numbersIndex, temp.get(i));

       numbersIndex++;

   }

}

public static void main(String[] args) {

   ArrayList<Integer> numList = new ArrayList<>();

   int temp;

   for (int i = 1; i <= 9; i++)

     numList.add( (int)(Math.random() * 50 + 1) );  

   System.out.println("Original ArrayList:");

   for(int val: numList)

     System.out.print(val + " ");

   merge_Sort(numList);

   System.out.println("\nSorted ArrayList:");

   for(int ele: numList)

     System.out.print(ele + " ");

   System.out.println();

 }

}

Explanation:

Answered by kuldeeppai
0

Answer:

System.out.print("Enter the value for x:");

             int x = sc.nextInt();

             System.out.print("Enter the value for y:");

             int y = sc.nextInt();

             System.out.print("Enter the value for z:");

             int z = sc.nextInt();

             int equation = 3*x + 4*y + z;

             System.out.println("The solution for the equation 3X+4Y+Z based on the values you entered is " + equation);

Explanation:

Similar questions