在此记录下,下次再需要复习的时候可以在这里查看
#include"iostream"
using namespace std;#define N 4
void BubbleSort(int a[])
{
for (int i = 0; i < N - 1; i++)
for (int j = 0; j < N - i - 1;j++)
if (a[j]>a[j + 1])
{
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
return;
}
void SelectSort(int a[])
{
int index;
for (int i = 0; i < N - 1; i++)
{
index = i;
for (int j = i + 1; j <= N - 1; j++)
if (a[index]>a[j])
index = j;
if (index != i)
{
int t = a[index];
a[index] = a[i];
a[i] = t;
}
}
return;
}
int main()
{
//
int a[N];
for (int i = 0; i < N; i++)
cin >> a[i];
//BubbleSort(a);
SelectSort(a);
for (int i = 0; i < N; i++)
cout << a[i] << " ";
return 0;
}
快速排序重新定义的
#include"iostream"
using namespace std;
#define N 10
int Patition(int a[], int left, int right)
{
int p = a[left];
while (left<right)
{
while ((left < right) && (p <= a[right]))
right--;
a[left] = a[right];
while ((left<right) && (p >= a[left]))
left++;
a[right] = a[left];
}
a[left] = p;
return left;
}
void QuickSort(int a[], int left, int right)
{
if (left < right)
{
int m = Patition(a, left, right);
QuickSort(a, left, m - 1);
QuickSort(a, m + 1, right);
}
}
int main()
{
int a[N] = { 0 };
for (int i = 0; i < N; i++)
cin >> a[i];
QuickSort(a, 0, N-1);
for (int i = 0; i < N; i++)
cout << a[i] << " ";
return 0;
}