void SelectSort(int arr[], int nSize)
{
if (NULL == arr || 1 >= nSize)
return;
for (int i=0;i<nSize;i++)
{
int nMin = i;
for (int j=i+1;j<nSize;j++)
{
if (arr[nMin] > arr[j])
{
nMin = j;
}
}
if (nMin !=i)
{
int nTmp = arr[i];
arr[i] = arr[nMin];
arr[nMin] = nTmp;
}
}
}
void BubbleSort(int arr[], int nSize)
{
if (NULL==arr && 1 >= nSize)
return;
for (int i=0;i<nSize;i++)
{
for (int j=0;j<nSize-1-i;j++)
{
if (arr[j]>arr[j+1])
{
int nTmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = nTmp;
}
}
}
}
void InsertSort(int array[], int size)
{
if (NULL == array || 1 >= size)
return;
for (int i=1;i<size;i++)
{
int nCur = array[i];
int j = i - 1;
while (0<=j && nCur<array[j])
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = nCur;
}
}
void ShellSort(int arr[], int N)
{
if (NULL == arr || 1 >= N)
return;
int i, j, gap;
for (gap = N / 2; gap > 0; gap /= 2)
{
for (int i=0;i<N;i++)
{
if (i<gap)
continue;
int nTmp = arr[i];
j = i - gap;
while (0<=j && nTmp<arr[j])
{
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = nTmp;
}
cout << "\ngap=" << gap;
cout << "\ncurrent list:";
for (int h = 0; h < N; h++)
cout << arr[h] << " ";
}
}
int main()
{
int array[] = { 12,45,90,1,34,87,-3,822,23,-222,32 };
ShowData(array,11);
BubbleSort(array, 11);
ShowData(array, 11);
return 0;
}