冒泡排序:
void bubble_sort(int arr[], int n)
{
int i, j;
int temp;
for(i=0;i
}
选择排序:
void selection_sort(int arr[], int n)
{
int i,j;
int temp;
for (i = 0; i < n-1; i++)
for (j = i + 1; j < n; j++)
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
插入排序:
void insertion_sort(int arr[], int n)
{
int temp;
int i, j;
for (i = 1; i < n; i++)
{
temp = arr[i];
for (j = i; j>0&&(temp>arr[j-1]); j–)
arr[j] = arr[j-1];
arr[j] = temp;
}
}
希尔排序:
void ShellSort(int arr[], int n)
{
int gap, i, j;
int temp;
gap = n / 2;
while (gap >= 1)
{
for (i = gap; i < n; i++)
{
temp = arr[i];
j = i - gap;
while ((j >= 0) && (temp > arr[j]))
{
arr[j + gap] = arr[j];
j -= gap;
}
arr[j+gap] = temp;
}
gap /= 2;
}
}
归并排序:
递归实现:
void MergeSort(int arr[], int n)
{
int * reg;
reg = (int *)malloc(sizeof(int)n);
MergeSort_recursive(arr, reg, 0, n - 1);
/ 其实最后将reg[]传递给arr[]就可以了,下面的是为了看清归并的每一个步骤 */
for (int i = 0; i < n; i++)
arr[i] = reg[i];
free(reg);
}
void MergeSort_recursive(int arr[], int reg[], int start, int end)
{
if (start == end)
{
reg[start] = arr[start];
printf("%d:%d,%d\n", start, arr[start], reg[start]);
putchar(’\n’);
}
else
{
int len = end - start, mid = (len >> 1) + start;
int start1 = start, end1 = mid;
int start2 = mid + 1, end2 = end;
MergeSort_recursive(arr, reg, start1, end1);
MergeSort_recursive(arr, reg, start2, end2);
int k = start;
while (start1 <= end1 && start2 <= end2)
reg[k++] = arr[start1] < arr[start2] ? arr[start1++] : arr[start2++];
while (start1 <= end1)
reg[k++] = arr[start1++];
while (start2 < end2)
reg[k++] = arr[start2++];
for (k = start; k <= end; k++)
{
printf("%d:%d,%d\n", k, arr[k], reg[k]);
arr[k] = reg[k];
}
putchar(’\n’);
}
}
迭代实现:
int minumum(int x, int y)
{
return x < y ? x : y;
}
void MergeSort2(int arr[], int n)
{
int * a = arr;
int * b = (int *)malloc(n * sizeof(int));
int seg, start;
printf(“a = %pt,b = %pt\n”, a, b);
for (seg = 1; seg < n; seg *= 2)
{
for (start = 0; start < n; start += 2 * seg)
{
int low = start, mid = minumum(start + seg, n), high = minumum(start + 2 * seg, n);
int k = low;
int start1 = low, end1 = mid;
int start2 = mid, end2 = high;
while (start1 < end1&&start2 < end2)
b[k++] = a[start1] < a[start2] ? a[start1++] : a[start2++];
while (start1 < end1)
b[k++] = a[start1++];
while (start2 < end2)
b[k++] = a[start2++];
for (k = start; k < high; k++)
a[k] = b[k];
}
}
free(b);
}
快速排序:
递归实现:
void swap(int * x, int * y)
{
int t = *x;
*x = *y;
*y = t;
}
void QuickSort_recursive(int arr[],int start,int end)
{
if (start < end)
{
int mid = arr[end];
int left = start, right = end - 1;
while (left < right)
{
while (arr[left] < mid&&left < right)
left++;
while (arr[right] >= mid && left < right)
right–;
swap(&arr[left], &arr[right]);
left++;
right--;
}
if(arr[left]>arr[end])
swap(&arr[left], &arr[end]);
if (left)
QuickSort_recursive(arr, start, left - 1);
QuickSort_recursive(arr, left + 1, end);
}
}
void QuickSort1(int arr[], int n)
{
QuickSort_recursive(arr, 0, n - 1);
}
迭代实现:
typedef struct {
int start, end;
}Range;
Range New_Range(int s, int e)
{
Range r;
r.start = s;
r.end = e;
return r;
}
void QuickSort2(int arr[], int n)
{
if (n <= 0)
return;
Range * r = (Range *)malloc(n * sizeof(Range));
int p = 0;
r[p++] = New_Range(0, n - 1);
while §
{
printf(“p = %d\n”, p);
Range range = r[–p];
if (range.start >= range.end)
continue;
int mid = arr[(range.start + range.end) / 2];
int left = range.start, right = range.end;
do {
while (arr[left] < mid)
left++;
while (arr[right] > mid)
right–;
if (left <= right)
{
swap(&arr[left], &arr[right]);
left++;
right–;
}
} while (left <= right);
if (range.start < right)
r[p++] = New_Range(range.start, right);
if (range.end > left)
r[p++] = New_Range(left, range.end);
}
free(r);
}