Question
The following Java program takes an array from a user, quick sorts it, and deduplicates it. Something is terribly wrong in my main method and
The following Java program takes an array from a user, quick sorts it, and deduplicates it. Something is terribly wrong in my main method and I can't get past the input. Help?
import java.util.Scanner;
public class DedupAndSort { // method to perform deduplication of the array the user inputs static int deDuplication1(int[] userArray, int n) { // int n = 1; // Return, if array is empty // or contains a single element if (n == 0 || n == 1) return n;
int[] temp = new int[n];
// Start traversing elements int j = 0; for (int i = 0; i // If current element is not equal // to next element then store that // current element if (userArray[i] != userArray[i + 1]) temp[j++] = userArray[i];
// Store the last element as whether // it is unique or repeated, it hasn't // stored previously temp[j++] = userArray[n - 1];
// Modify original array for (int i = 0; i userArray[i] = temp[i];
return j;
}
// method to sort the array the user inputs // quickSort here // A utility function to swap two elements static void swap(int[] userArray, int i, int j) { int temp = userArray[i]; userArray[i] = userArray[j]; userArray[j] = temp; }
/* * This function takes last element as pivot, places the pivot element at its * correct position in sorted array, and places all smaller (smaller than pivot) * to left of pivot and all greater elements to right of pivot */ static int partition(int[] userArray, int low, int high) {
// pivot int pivot = userArray[high];
// Index of smaller element and // indicates the right position // of pivot found so far int i = (low - 1);
for (int j = low; j =>
// If current element is smaller // than the pivot if (userArray[j]
// Increment index of // smaller element i++; swap(userArray, i, j); } } swap(userArray, i + 1, high); return (i + 1); }
/* * The main function that implements QuickSortarr[] --> Array to be sorted, low * --> Starting index, high --> Ending index */ static void quickSort(int[] userArray, int low, int high) { if (low
// pi is partitioning index, arr[p] // is now at right place int pi = partition(userArray, low, high);
// Separately sort elements before // partition and after partition quickSort(userArray, low, pi - 1); quickSort(userArray, pi + 1, high); } }
// Function to print an array static void printArray(int[] userArray, int size) { for (int i = 0; i // System.out.print(dedupped[i] + \" \");
System.out.println(); }
public static void main(String[] args) { int n; Scanner sc = new Scanner(System.in); System.out.print(\"Enter the number of elements you want to store: \"); // reading the number of elements from the that we want to enter n = sc.nextInt(); // creates an array in the memory of length n int[] userArray = new int[n];
System.out.println(\"Enter the elements of the array: \"); for (int i = 0; i // reading array elements from the user userArray[i] = sc.nextInt(); // test case to see if the array contains null // int userArray[] = null; // if (userArray == null) { // System.out.println(\"array is null\");
// test case to see if the array has zero elements // userArray = new int[0]; // if (userArray.length == 0) { // System.out.println(\"array is empty\"); } // int k = userArray.length; // quickSort(userArray, 0, k - 1); // printArray(userArray, n);
// intitializing a new array called newdedupped int[] dedupped = new int[0];
// looping through each element in the new dedupped array for (int i = 0; i;> // looping through each element of newdedupped array for (int j = 0; j ;> // comparing the elements of the two arrays and printing them if (userArray[i] != dedupped[j]) { dedupped[i] = userArray[i]; System.out.println(i); System.out.println(dedupped[i]);
int k = dedupped.length; quickSort(dedupped, 0, k - 1); printArray(dedupped, n);
deDuplication1(dedupped, k);
// calling the method quickSort on the array dedupped
System.out.println(\"Array After Deduplication\"); // running through the array to print it out to the console for (int m = 0; i;> System.out.print(dedupped[m] + \" \"); }
}
} } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started