交换排序:快速排序(数组)
//============================交换排序:快速排序================//
#include <iostream>
using namespace std;
void print(int a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int partition(int a[], int low, int high)
{
int pivotValue = a[low];
while (low < high)
{
while (low < high && a[high] > pivotValue) --high;
swap(a[low], a[high]);
while (low < high && a[low] < pivotValue) ++low;
swap(a[low], a[high]);
}
return low;
}
void quickSort(int a[], int low, int high)
{
if (low < high)
{
int pivotKey = partition(a, low, high);
quickSort(a, low, pivotKey - 1);
quickSort(a, pivotKey + 1, high);
}
}
int main()
{
int a[10] = { 3, 1, 5, 7, 2, 4, 9, 6, 10, 8 };
print(a, 10);
quickSort(a, 0, 9);
print(a, 10);
}
交换排序:快速排序(链表)
//============================交换排序:快速排序================//
交换排序:起泡排序
============================交换排序:起泡排序================//
#include <iostream>
using namespace std;
void print(int a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void bubbleSort(int a[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < n - 1 - i; ++j)
{
if (a[j]>a[j + 1])
swap(a[j], a[j + 1]);
}
}
}
void bubbleSort1(int a[], int n)
{
int i = n - 1;
while (i > 0)
{
int Loc = 0;
for (int j = 0; j < i; ++j)
{
if (a[j]>a[j + 1])
{
swap(a[j], a[j + 1]);
Loc = j;
}
}
i = Loc;
}
}
int main()
{
int a[10] = { 3, 1, 5, 7, 2, 4, 9, 6, 10, 8 };
print(a, 10);
bubbleSort(a, 10);
print(a, 10);
}
选择排序:简单选择排序
//============================选择排序:简单选择排序================//
#include <iostream>
using namespace std;
void print(int a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int selectMinKey(int a[], int n, int i)
{
int k = i;
for (int j = i+1; j < n; ++j)
{
if (a[j] < a[k]) k = j;
}
return k;
}
void selectSort(int a[], int n)
{
for (int i = 0; i < n-1; ++i)
{
int key = selectMinKey(a, n, i);
if(key != i) swap(a[i], a[key]);
}
}
int main()
{
int a[10] = { 3, 1, 5, 7, 2, 4, 9, 6, 10, 8 };
print(a, 10);
selectSort(a, 10);
print(a, 10);
}
选择排序:堆排序
============================选择排序:堆排序================//
#include<iostream>
using namespace std;
void print(int a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void heapAjust(int H[], int s, int n)
{
int i;
int temp = H[s];
for (i = 2 * s + 1; i < n; i = 2 * i + 1)
{
if (i+1 < n && H[i] < H[i + 1]) ++i;
if (temp > H[i]) break;
H[s] = H[i];
s = i;
}
H[s] = temp;
}
void buildHeap(int H[], int n)
{
for (int j = n / 2 - 1; j >= 0; --j)
{
heapAjust(H, j, n);
}
}
void heapSort(int H[], int n)
{
buildHeap(H, n);
for (int i = n - 1; i > 0; --i)
{
swap(H[0], H[i]);
heapAjust(H, 0, i);
}
}
int main()
{
int a[10] = { 1, 0, 2, 5, 8, 6, 3, 4, 7, 9 };
print(a, 10);
heapSort(a, 10);
print(a, 10);
}