I need help removing duplicates from selectionSort and insertionSort. I think I
ID: 3654237 • Letter: I
Question
I need help removing duplicates from selectionSort and insertionSort. I think I have gotten insertionSort to work but selectionSort keeps breaking. I cannot use a method to remove duplicates because it needs to be as efficient as possible and be able to work with large sets of numbers. Please give detailed explanation. import java.util.ArrayList; public class Sorting { //----------------------------------------------------------------- // Sorts the specified array of objects using the selection // sort algorithm. //----------------------------------------------------------------- public static void selectionSort(Comparable[] list) { int min; Comparable temp; boolean[] duplicateArray = new boolean[list.length]; for (int index = 0; index < list.length - 1; index++) { min = index; for (int scan = index + 1; scan < list.length; scan++) if (list[scan].compareTo(list[min]) > 0) {// switched < to // greater to change // to descending min = scan; if ((scan + 1 < list.length) && list[scan].compareTo(list[scan - 1]) == 0) {// if it equals itself System.out.println("Match"); duplicateArray[scan - 1] = true; } } // Swap the values temp = list[min]; list[min] = list[index]; list[index] = temp; } ArrayList duplicateArrayList = new ArrayList(); for (int i = 0; i < list.length; i++) { if (duplicateArray[i] == false) { duplicateArrayList.add(list[i]); } } System.out.println(duplicateArrayList.toString());//prints out array without duplicates in descending order } //----------------------------------------------------------------- // Sorts the specified array of objects using the insertion // sort algorithm. //----------------------------------------------------------------- public static void insertionSort (Comparable[] list) { boolean[] duplicateArray = new boolean[list.length]; for (int index = 1; index < list.length; index++) { Comparable key = list[index]; int position = index; // Shift larger values to the right while (position > 0 && key.compareTo(list[position-1]) > 0)//switched sign to change to descending { list[position] = list[position-1]; position--; } if (list[position].compareTo(list[position - 1]) == 0) { duplicateArray[position] = true; } list[position] = key; } ArrayList duplicateArrayList = new ArrayList(); for (int i = 0; i < list.length; i++) { if (duplicateArray[i] == false) { duplicateArrayList.add(list[i]); } } System.out.println(duplicateArrayList.toString());//prints out array without duplicates in descending order } }Explanation / Answer
#include int removeDuplicates(int a[], int array_size); // The main function int main() { // Different test cases.. int my_array1[] = {1, 1, 2, 3, 5, 6, 6, 7, 10, 25, 100, 123, 123}; int my_array1_size = 13; int my_array2[] = {1, 2, 3, 5, 6}; int my_array2_size = 5; int my_array3[] = {1, 1, 1, 1, 1}; int my_array3_size = 5; int my_array4[] = {123, 123}; int my_array4_size = 2; int my_array5[] = {1, 123, 123}; int my_array5_size = 3; int my_array6[] = {123, 123, 166}; int my_array6_size = 3; int my_array7[] = {1, 2, 8, 8 , 24, 60, 60, 60, 60, 75, 100, 100, 123}; int my_array7_size = 13; my_array1_size = removeDuplicates(my_array1, my_array1_size); my_array2_size = removeDuplicates(my_array2, my_array2_size); my_array3_size = removeDuplicates(my_array3, my_array3_size); my_array4_size = removeDuplicates(my_array4, my_array4_size); my_array5_size = removeDuplicates(my_array5, my_array5_size); my_array6_size = removeDuplicates(my_array6, my_array6_size); my_array7_size = removeDuplicates(my_array7, my_array7_size); return(0); } // Function to remove the duplicates int removeDuplicates(int a[], int array_size) { int i, j; j = 0; // Print old array... printf(" OLD : "); for(i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.