z

Cải tiến Selection Sort với code C

Sắp xếp chọn cải tiến với mảng số nguyên:

void SelectionSort_Caitien(int a[], int n)
{
int minIndex;
int maxIndex;
for(int i=0;i<=(n-1)/2;i++)
{
minIndex=i;
maxIndex=i;
for(int j=i+1;j<n-i;j++)
{
if(a[j]<a[minIndex])
minIndex=j;
if (a[j]>a[maxIndex])
maxIndex=j;
}

//Hoan vi a[i], a[minIndex]
if(maxIndex==i)
maxIndex=minIndex;
swap(a[i],a[minIndex]);
swap(a[n-1-i],a[maxIndex]);
}
}

Read more »
Previous
Next Post »