用c语言实现冒泡排序,选择排序,快速排序
作者:
本篇文章是对使用c语言实现冒泡排序,选择排序,快速排序的代码进行了详细的分析介绍,需要的朋友参考下
代码如下所示:
/*
* 冒泡排序
*/
void BubbleSort(int arr[], int n)
{
int temp;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
/*
* 选择排序
*/
void ChooseSort(int arr[], int n)
{
int temp, k;
for (int i = 0; i < n - 1; i++)
{
k = i;
for (int j = i + 1; j < n; j++)
{
if (arr[k] > arr[j])
{
k = j;
}
}
if (k != i)
{
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
}
/*
* 快速排序,官方原版
*/
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
{
right--;
}
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
{
left++;
}
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
{
q_sort(numbers, left, pivot-1);
}
if (right > pivot)
{
q_sort(numbers, pivot+1, right);
}
}
/*
* 快速排序
*/
void quick_sort(int *x, int low, int high)
{
int i, j, t;
if (low < high) /*要排序的元素起止下标,保证小的放在左边,大的放在右边。这里以下标为low的元素为基准点*/
{
i = low;
j = high;
t = *(x+low); /*暂存基准点的数*/
while (i<j) /*循环扫描*/
{
while (i<j && *(x+j)>t) /*在右边的只要比基准点大仍放在右边*/
{
j--; /*前移一个位置*/
}
if (i<j)
{
*(x+i) = *(x+j); /*上面的循环退出:即出现比基准点小的数,替换基准点的数*/
i++; /*后移一个位置,并以此为基准点*/
}
while (i<j && *(x+i)<=t) /*在左边的只要小于等于基准点仍放在左边*/
{
i++; /*后移一个位置*/
}
if (i<j)
{
*(x+j) = *(x+i); /*上面的循环退出:即出现比基准点大的数,放到右边*/
j--; /*前移一个位置*/
}
}
*(x+i) = t; /*一遍扫描完后,放到适当位置*/
quick_sort(x,low,i-1); /*对基准点左边的数再执行快速排序*/
quick_sort(x,i+1,high); /*对基准点右边的数再执行快速排序*/
}
}
// 输出数组元素
void outArray(int arr[], int n)
{
for(int i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void main()
{
const int N = 5;
int arr1[N] = {4, 3, 5, 2, 1};
int arr2[N] = {4, 3, 5, 2, 1};
int arr3[N] = {4, 3, 5, 2, 1};
cout<<"Before bubble sort"<<endl;
outArray(arr1, N);
BubbleSort(arr1, N);
cout<<"After bubble sort"<<endl;
outArray(arr1, N);
cout<<"/nBefore chooose sort"<<endl;
outArray(arr2, N);
ChooseSort(arr2, N);
cout<<"After chooose sort"<<endl;
outArray(arr2, N);
cout<<"/nBefore quick sort"<<endl;
outArray(arr3, N);
//q_sort(arr3,0, N - 1);
quick_sort(arr3,0, N - 1);
cout<<"After quick sort"<<endl;
outArray(arr3, N);
system("pause");
}
复制代码 代码如下:
/*
* 冒泡排序
*/
void BubbleSort(int arr[], int n)
{
int temp;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
/*
* 选择排序
*/
void ChooseSort(int arr[], int n)
{
int temp, k;
for (int i = 0; i < n - 1; i++)
{
k = i;
for (int j = i + 1; j < n; j++)
{
if (arr[k] > arr[j])
{
k = j;
}
}
if (k != i)
{
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
}
/*
* 快速排序,官方原版
*/
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
{
right--;
}
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
{
left++;
}
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
{
q_sort(numbers, left, pivot-1);
}
if (right > pivot)
{
q_sort(numbers, pivot+1, right);
}
}
/*
* 快速排序
*/
void quick_sort(int *x, int low, int high)
{
int i, j, t;
if (low < high) /*要排序的元素起止下标,保证小的放在左边,大的放在右边。这里以下标为low的元素为基准点*/
{
i = low;
j = high;
t = *(x+low); /*暂存基准点的数*/
while (i<j) /*循环扫描*/
{
while (i<j && *(x+j)>t) /*在右边的只要比基准点大仍放在右边*/
{
j--; /*前移一个位置*/
}
if (i<j)
{
*(x+i) = *(x+j); /*上面的循环退出:即出现比基准点小的数,替换基准点的数*/
i++; /*后移一个位置,并以此为基准点*/
}
while (i<j && *(x+i)<=t) /*在左边的只要小于等于基准点仍放在左边*/
{
i++; /*后移一个位置*/
}
if (i<j)
{
*(x+j) = *(x+i); /*上面的循环退出:即出现比基准点大的数,放到右边*/
j--; /*前移一个位置*/
}
}
*(x+i) = t; /*一遍扫描完后,放到适当位置*/
quick_sort(x,low,i-1); /*对基准点左边的数再执行快速排序*/
quick_sort(x,i+1,high); /*对基准点右边的数再执行快速排序*/
}
}
// 输出数组元素
void outArray(int arr[], int n)
{
for(int i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void main()
{
const int N = 5;
int arr1[N] = {4, 3, 5, 2, 1};
int arr2[N] = {4, 3, 5, 2, 1};
int arr3[N] = {4, 3, 5, 2, 1};
cout<<"Before bubble sort"<<endl;
outArray(arr1, N);
BubbleSort(arr1, N);
cout<<"After bubble sort"<<endl;
outArray(arr1, N);
cout<<"/nBefore chooose sort"<<endl;
outArray(arr2, N);
ChooseSort(arr2, N);
cout<<"After chooose sort"<<endl;
outArray(arr2, N);
cout<<"/nBefore quick sort"<<endl;
outArray(arr3, N);
//q_sort(arr3,0, N - 1);
quick_sort(arr3,0, N - 1);
cout<<"After quick sort"<<endl;
outArray(arr3, N);
system("pause");
}