我的C算法库【2】:SelectionSort:

SelectionSort:

Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A, and exchange it with A[2]. Continue in this manner for the first n - 1 elements of A. Write pseudocode for this algorithm, which is known as selection sort. 

/** * SelectionSort Algorithm * * @param arrays arrays to sort * @n the size of the array * return null */ void SelectionSort(SortValue arrays[], int n) { if (n == 1) return; int i = 0;//outer loop int j = 0;//inner loop SortValue min;//pointer to the minmum value for (i = 0; i < n; i++) { min = arrays[i]; for (j = i + 1; j < n; j++) { if (int_compare(arrays[j], min) < 0)//value is less than the min min = arrays[j]; } //swap the swap(min, arrays[i]); } 

你可能感兴趣的:(Algorithm,c,算法,Arrays,Exchange,sorting)