Question

Need help with my Java Hw: Consider an algorithm that sorts an array of n elements...

Need help with my Java Hw:

Consider an algorithm that sorts an array of n elements by finding the smallest and largest elements and then exchanges those elements with the elements in the first and last positions in the array. Then the size of the array is reduced by two elements after excluding the two elements that are already in the proper positions, and the process is repeated on the remaining part of the array until the entire array is sorted.

Write a code to implement the above algorithm.

Write a driver program to show the novel sorting algorithm works correctly.

Please complete all steps and show proof of code working

DO THIS IN JAVA PLEASE

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

JAVA Program:


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

class Sorter {
   public static int findMinIndex(int arr[],int start,int end){
int minIndex=start;
for(int i=start+1;i<=end;i++){
if(arr[i]<arr[minIndex])
minIndex=i;
}
       return minIndex;
}
public static int findMaxIndex(int arr[],int start,int end){
int maxIndex=start;
for(int i=start+1;i<=end;i++){
if(arr[i]>arr[maxIndex])
maxIndex=i;
}
return maxIndex;
}
public static void swap(int arr[],int pos1,int pos2){
int temp=arr[pos1];
arr[pos1]=arr[pos2];
arr[pos2]=temp;
}
public static void sorter(int arr[],int n){
int start=0,end=n-1;
while(start<end){
int minIndex=findMinIndex(arr,start,end);
           int maxIndex=findMaxIndex(arr,start,end);
           swap(arr,start,minIndex);
swap(arr,end,maxIndex);
start++;
end--;
}
}
   public static void main (String[] args) {
       int n;
       System.out.println("Enter the size of the array: ");
       Scanner sc = new Scanner(System.in);
       n=sc.nextInt();
       int []arr=new int[n];
       System.out.println("Enter the values of array: ");
       for(int i=0;i<n;i++)
       arr[i]=sc.nextInt();
       sorter(arr,n);
       System.out.println("Sorted array:");
       for(int i=0;i<n;i++)
       System.out.print(arr[i]+" ");
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Need help with my Java Hw: Consider an algorithm that sorts an array of n elements...
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
  • I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...

    I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • Consider an array A[1...n] which is originally unsorted. One method to sort the array is as...

    Consider an array A[1...n] which is originally unsorted. One method to sort the array is as follows: First find the largest key in the unsorted portion of the array (initially the entire array s unsorted) and swap this with the last position in the unsorted section. This last position is now considered part of the sorted portion. The procedure is repeated until the entire array is sorted. (a) Write an algorithm to sort A as outlined above (in pseudo-code, no...

  • Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...

    Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element]. A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we discuss the bubble sort It requires the swapping...

  • the programming language is in java Problem 2 You are given an array A with n...

    the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...

  • Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers)...

    Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...

  • Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm...

    Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm Write a static function called RemoveNullElements() without creating another array. that remove Null elements and shift all the rest of the elements to the left/front (small indexes) of the array without changing the size or length of the original array( this meant the front of the array will have all the not Null elements, and the other haft will be empty/Null. Please provide test...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

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