I think I have done the selection sort but I am not sure. Is this really an implementation of selection sort?
static void selectionSort()
{
int min = Integer.MIN_VALUE;
int n = 0;
for(int I=0; I<arraySize; I++)
{
min = dataArray[I];
for(int j=I; j<n; j++)
{
if(dataArray[min]<dataArray[j])
{
min = j;
if(dataArray[min] < dataArray[I])
{
int temp = dataArray[I];
dataArray[I] = dataArray[min];
dataArray[min] = temp;
}
}
}
}
}
I’m not sure I understand how your algorithm works at all. Specifically, you do
and then later
i.e. you treat
minboth as a value in the array, and an index.Selection sort works as follows:
(source)
The changes required for your code to accurately implement selection sort would be the following:
minIndexfor instance.IwithminIndex.Oh, and as DonCallisto points out in the comments, you may want to do
n = dataArray.lengthinstead ofn = 0🙂