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...

  • Write a C program to assign natural numbers 1 to 100 into a one-dimensional integer array....

    Write a C program to assign natural numbers 1 to 100 into a one-dimensional integer array. Display all the values in the array on the screen. For each number in the array, determine if the number contains digit 7 or is divisible by 7. Display all those numbers on the screen. Original array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27...

  • 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....

  • 5.35 LAB: Sort a vector Write a program that gets a list of integers from input,...

    5.35 LAB: Sort a vector Write a program that gets a list of integers from input, and outputs the integers in ascending order (lowest to highest). The first integer indicates how many numbers are in the list. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 10 4 39 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space, including the last...

  • Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to...

    Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to sort." The actual question: Write a program as follows: Declare a five element array of Strings. Use a for loop and keyboard input to populate the array with the names of five friends. Sort the array in alphabetical order. Use a foreach loop to print the sorted array of friends, all on one line. Sample Output (inputs in italics) Enter the names of five...

  •   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)

  • Java: Write an application that has an array of at least 20 integers. It should call...

    Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these...

  • 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...

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