I am basically dealing with the following problem where i am trying to alter the insert sort so that it can also delete duplicates it counters. The following is the insert sort.
public void insertSort() {
for (int i = 1; i < nElems; i++) {
int temp = a[i];
int j = i;
while (j > 0 && temp <= a[j - 1]) {
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}
I am not quire sure if i have understood the approach correctly. IF i am understanding this correctly(please tell me if i am wrong or not) the approach suggests that i should iterate through the entire array before the inner while loop begins and label any duplicate with a arbitrary number such as -1. And then when the inner while loop starts it will sort out the array and all the duplicates will be stacked up at the beginning together.
If this is the case then i can simply compare every element in the array with each other right before the insert sort begins and label any duplicates - 1 and then the insert sort will take care of the sorting part. After which i can reduce arraySize.
However i feel i have not understood it correctly so can someone please make any suggestions regarding this?
if(temp == a[j - 1]) temp = -1;
. Now you just need to figure out where to place it. – Dispel