C语言三种常用的排序方法:冒泡排序,插入排序,快速排序
冒泡排序:
void maopao(int a[])
{
int temp;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}插入排序:
void insert(int a[])
{
int temp;
int j;
for (int i = 1; i < n; i++)
{
j = i;
temp = a[i];
while (temp < a[j - 1] && j >= 1)
{
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}快速排序:
void Quick_sort(int low,int high,int a[])
{
if (low >= high)
return;
int first = low;
int last = high;
int key = a[first];
while (first < last)
{
while (first < last&&a[last]>=key)
{
last--;
}
a[first] = a[last];
while (first
first++;
a[last] = a[first];
}
a[first] = key;
Quick_sort(low, first - 1, a);
Quick_sort(first+1, high, a);
}