呃终于写完了 我写的c++的 才发现是Python板块的 不好意思哇//-----------------冒泡排序--------------------
void bubblesort()
{
int i, j,flag,temp;
for (i = 0; i <= 1999; i++)
{
flag = 0;
for (j = 0; j <= 1999 - i; j++)
{
if (b[j] > b[j + 1])
{
temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
flag = 1;
}
}
if (!flag) break;
}
}
//-----------------快速排序1--------------------
void quicksort1(int left,int right)
{
int i, j, t, temp;
if (left > right)
return;
i = left;
j = right;
temp = a[left];
while (i!=j)
{
while (i < j&&a[j] >= temp)
--j;
while (i < j&&a[i] <= temp)
++i;
if (i<j)
t = a[j]; a[j] = a[i]; a[i] = t;
}
a[left] = a[i];
a[i] = temp;
quicksort1(left,i-1);
quicksort1(i+1,right);
}
//-----------------快速排序2--------------------
int part(int left, int right)
{
int temp = a[left];
int pivot = a[left];
while (left < right)
{
while (left < right&&a[right] >= pivot) right--;
a[left] = a[right];
while (right > left&&a[left] <= pivot) left++;
a[right] = a[left];
}
a[left] = temp;
return left;
}
void quicksort(int left, int right)
{
if (left < right)
{
int flag = part(left, right);
quicksort(left, flag - 1);
quicksort(flag+1,right);
}
}
//-----------------直接插入排序--------------------
void insertsort()
{
int i, j, temp;
for (i = 1; i < 2000; i++)//a[0]作监视哨
{
temp = a[i];
j = i - 1;
while (a[j]>temp && j >= 0)
{
a[j + 1] = a[j];//后移
--j;
}
a[j + 1] = temp;
}
}
//-----------------希尔排序--------------------
void shellsort()
{
int i, j,temp,k;
int d;//间隔
for (d = 2000 / 2; d > 0;d/=2)
{
//直接插入排序
for (k = 0; k < d; ++k)
{
for (i = k+d; i < 2000; i += d)
{
temp = a[i];
j = i - d;
while (a[j]>temp && j >= k)
{
a[j + d] = a[j];
j -= d;
}
a[j + d] = temp;
}
}
}
}
//-----------------堆排序--------------------
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
void HeapAdjust(int i,int n)
{
int j, temp;
temp = a[i];
j = 2 * i + 1;
while (j<n)
{
if (j+1<n && a[j] < a[j + 1])
{
j++;
}
if (temp>=a[j]) break;
a[i] = a[j];
i = j;
j = 2 * i + 1;
}
a[i]=temp;
}
void Heapsort()
{
int i;
for (i = 2000/ 2-1; i >= 0; i--)
{
HeapAdjust(i,2000);
}
for (i = 1999; i > 0; i--)
{
swap(a[0],a[i]);
HeapAdjust(0,i-1);
}
}
//-----------------归并排序--------------------
void merge(int*left,int left_length,int *right,int right_length)
{
int i, j, k,m;
i = j = k = 0;
while (i<left_length && j<right_length)
{
if (left[i] < right[j])
temp[k++] = left[i++];
else
temp[k++] = right[j++];
}
while (i < left_length)
temp[k++] = left[i++];
while (j < right_length)
temp[k++] = right[j++];
for ( m = 0; m < (left_length+right_length); m++)
left[m] = temp[m];
}
void mergesort(int* x, int n)
{
if (n >= 2)
{
int *left = x;
int left_length = n / 2;
int *right = x + left_length;
int right_length = n - left_length;
mergesort(left, left_length);
mergesort(right, right_length);
merge(left, left_length, right, right_length);
}
}
|