复制代码 代码如下:
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)
{
i = low;
j = high;
t = *(x+low);
while (i
while (i
{
j--;
}
if (i
*(x+i) = *(x+j);
i++;
}
while (i
i++;
}
if (i
*(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< }
cout<
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"<
BubbleSort(arr1, N);
cout<<"After bubble sort"<
cout<<"/nBefore chooose sort"<
ChooseSort(arr2, N);
cout<<"After chooose sort"<
cout<<"/nBefore quick sort"<
//q_sort(arr3,0, N - 1);
quick_sort(arr3,0, N - 1);
cout<<"After quick sort"<
system("pause");
}



