Question

How to write a recursive method named lessThanKFirst that receives an array of integers a and...

How to write a recursive method named lessThanKFirst that receives an array of integers a and an integer k and rearranges the integers in a in such a way that all integers that are smaller than k come before any integers that are greater than or equal to k. As an example, suppose that a and k are: int[] a  = {35, 12, 57, 28, 49, 100, 61, 73, 92, 27, 39, 83, 52}; int k = 73; Then, the following is one possible output: a = [35, 12, 57, 28, 49, 52, 61, 39, 27, 92, 83, 73, 100] Your output may be different from the above output. As long as all integers smaller than 73 come before those that are greater than or equal to 73, that is OK. You must not use another array when you rearrange the elements of the given array and you must not sort the given array (a sorted array automatically satisfies the given requirement). The signature of your method must be: lessThanKFirst(int[] a, k) and write a main method to test the above method. My code is only sorting from low to high and not sure what type of sort this is:

public static void main(String b[]){

        int[] a = {35, 12, 57, 28, 49, 100, 61, 73, 92, 27, 39, 83, 52};

        int[] k = lessThanKFirst(a);

        for(int i:k){

            System.out.print(i);

            System.out.print(", ");

        }

    }

    

    public static int[] lessThanKFirst(int[] a){

        

        int temp;

        for (int i = 1; i < a.length; i++) {

            for(int j = i ; j > 0 ; j--){

                if(a[j] < a[j-1]){

                    temp = a[j];

                    a[j] = a[j-1];

                    a[j-1] = temp;

                }

            }

        }

        return a;

    }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA FUNCTION:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
   public static void main(String b[]){

int[] a = {35, 12, 57, 28, 49, 100, 61, 73, 92, 27, 39, 83, 52};

int k = 57;
int n = a.length;
int pivot = 0;
//now call the function with array a and k
int[] p = lessThanKFirst(a,k);
System.out.println(Arrays.toString(p));
  

}

  

public static int[] lessThanKFirst(int[] a,int k){
int n = a.length;
//find the index where k occurs
int kth=0;
for(int i=0;i<n;i++){
if(a[i]==k){
kth = i;
break;
}
}
//swap last element and the element with value k
int temp = a[kth];
a[kth] = a[n-1];
a[n-1] = temp;
  
// initialize last as n-1 i.e index where the last element occurs
int last = n-1;
int pivot = a[last];
int i = -1;
for(int j=0;j<last;j++){
//if jth element is smaller than pivot swap it with ith element
//this way you can shift smaller elements to the LHS of the pivot
if(a[j]<pivot){
i++;
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
//swap pivot with the (i+1)th element
temp = a[i+1];
a[i+1] = a[last];
a[last] = temp;
return a;
}

}

OUTPUT:

CODE SCREENSHOT:

Add a comment
Know the answer?
Add Answer to:
How to write a recursive method named lessThanKFirst that receives an array of integers a and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • I need it in JAVA Write a program that randomly populates an array of size 100,...

    I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....

  •   Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...

      Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion:    Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) {     j=i;     temp=arr[i];                while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...

  • a. Write a method that receives (a reference to an array of integers and returns the...

    a. Write a method that receives (a reference to an array of integers and returns the number of odd values in the array that are between 20 and 35 (inclusive) or greater than 65 (also b. Write a single Java expression in Java that corresponds to the following formula: 2 (y3 – 5) - VZ)10)

  • Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort...

    Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm. Your program should print the processed array after each step of the merge sort. For example, if the input array is 14 27 13 11 49 63 17 9, your program should print each sort process: Input Arra;y 14 27 13 11 49 63 17 9 Print After first Iteration 14 27 11 13 49 639 17 Print After second iteration 11 13...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT