好程序员训练营
<A href="http://www.goodprogrammer.org/" target="blank">ios培训</A>
------我的c语言笔记,期待与您交流!
#include<stdio.h>
void swap(int *list, int i, int j)
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
//冒泡排序
void BubbleSort(int *list)
{
int i, j;
bool flag = true;
for (i = 0; i < 9 && flag; i++)
{
flag= false;
for (j = 8; j > i; j--)
{
if (list[j] > list[j + 1])
{
swap(list, j, j + 1);
flag = true;
}
}
}
}
int main()
{
int list[10] = { 0,9, 1, 5, 8, 3, 7, 4, 6, 2 };
int i;
for (i = 0; i < 10; i++)
printf("%5d", list[i]);
printf("\n");
BubbleSort(list);
for (i = 0; i < 10; i++)
printf("%5d", list[i]);
printf("\n");
}
//Shell排序
#include<stdio.h>
void ShellSort(int *v)
{
int gap, i, j,temp;
for (gap =9 / 2; gap > 0; gap /= 2)
for (i = gap; i <10; i++)
for (j = i - gap; j >= 0 && v[j] > v[j + gap]; j -= gap)
{
temp = v[j];
v[j] = v[j + gap];
v[j + gap] = temp;
}
}
int main()
{
int list[10] = { 17, 9, 1, 5, 8, 3, 7, 4, 6, 2 };
int i, j;
for (i = 0; i < 10; i++)
printf("%d ", list[i]);
printf("\n");
ShellSort(list);
for (j = 0; j < 10; j++)
printf("%d ", list[j]);
}
//快速排序
#include<stdio.h>
void swap(int *v, int i, int j)
{
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
void QuickSort(int *v, int left, int right)
{
int i, last;
void swap(int *v, int i, int j);
if (left >= right)
return;
swap(v, left, (left + right) / 2);
last = left;
for (i = left + 1; i <= right; i++)
if (v[i] < v[left])
swap(v, ++last, i);
swap(v, left, last);
QuickSort(v, left, last - 1);
QuickSort(v, last + 1, right);
}
int main()
{
int list[10] = { 17, 9, 1, 5, 8, 3, 7, 4, 6, 2 };
int i, j;
for (i = 0; i < 10; i++)
printf("%d ", list[i]);
printf("\n");
QuickSort(list,0,9);
for (j = 0; j < 10; j++)
printf("%d ", list[j]);
}