We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
package array;
public class SupplantBubble {
private static int[] arrayFinal; // Final sorted array
private static int index = 0; // Index to keep track of the position in arrayFinal
public static void main(String[] args) {
int[] array1 = {1, 7, 6, -1, 5}; // Example input array int length = array1.length; arrayFinal = new int[length]; // Initialize final array with the same length Reorder(array1, length);
// Print the sorted array
for (int num : arrayFinal) { System.out.print(num + " "); } }
public static int[] Reorder(int[] array1, int length) {
// Base case: if the length is 0, return the final sorted array if (length == 0) { return arrayFinal; } else { int temp = array1[0]; int location = 0;
// Find the largest element and its index
for (int i = 1; i < length; i++) { if (array1[i] > temp) { temp = array1[i]; location = i; } } // Add the largest element to the final array arrayFinal[index++] = temp;
// Create a new array excluding the largest element
int[] TempArray = new int[length - 1]; int tempIndex = 0;
for (int i = 0; i < length; i++) {
if (i == location) { continue; // Skip the largest element } TempArray[tempIndex++] = array1[i]; // Copy remaining elements }