java selection sort

Selection sort is to find the smallest element from all elements and exchange the first element, and then find the smallest element from the remaining elements and exchange the second element. That is, there are n numbers to be sorted, and n-1 times are required to be exchanged for n-1 times, and the i-th pass is to be carried out ni times, but not exchanged.

//selection sort

class select

{

//I think the first number is the minimum

int temp ;

 

public void test( int array[])

for(int i=0; i<array.length;i++)

{

int min = array[i];

int minindex =i;

 

for(int k=i+1;k<array.length;k++)

{

if(min>array[k])

{

min =array[k];

minindex = k;

}

}

//When exiting for, find the minimum value this time

temp = array[i];

array[i]= array[minindex];

array[minindex]= temp;

}

}

 

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326403571&siteId=291194637