一、冒泡排序
#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void Bubble_Sort(int *p, int length)
{
for (int i = 0; i < 10; i++)
{
for (int j = length - 1; j > i; --j)
{
if (p[j] < p[j - 1])
{
swap(p[j], p[j - 1]);
}
}
}
}
int main()
{
int buf[10]{ 12, 4, 34, 6, 8, 65, 3, 2, 60, 45 };
cout << "排序前:" << endl;
for(int i = 0; i < 10; ++i)
cout << buf[i]<< " ";
Bubble_Sort(buf,10);
cout << "\n\n\n排序後" << endl;
for(int i = 0; i < 10; ++i)
cout << buf[i]<< " ";
return 0;
}
二、選擇排序
#include <iostream>
using namespace std;
void Select_sort(int a[],int n){
int i,j,temp,minium;
for(i=0; i < n-1; ++i){
minium = i;
for(j = i+1; j < n; ++j){
if(a[j] < a[minium]){
minium = j;
}
}
if(a[minium] != a[i]){
temp = a[i];
a[i] = a[minium];
a[minium] = temp;
}
}
}
int main()
{
int a[10]{3,2,6,5,4,8,9,0,7,1};
cout << "排序前: ";
for(int i=0; i < 10; ++i)
cout <<a[i]<< " ";
Select_sort(a,10);
cout << endl<< "排序後: ";
for(int i=0; i < 10; ++i)
cout <<a[i]<< " ";
cout << endl;
return 0;
}
三、直接插入排序
#include <iostream>
using namespace std;
void insert_sort(int a[], int n){
int i, j, temp;
for( i=1; i < n; ++i){
if(a[i] < a[i-1]){
temp = a[i];
for( j = i-1; a[j] > temp && j >= 0; --j){
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
}
int main()
{
int a[10]{5,2,6,0,3,9,1,7,4,8};
cout << "排序前:" << endl;
for(int i = 0; i < 10; ++i)
cout << a[i]<< " ";
insert_sort(a,10);
cout << "\n\n\n排序後:" << endl;
for(int i = 0; i < 10; ++i)
cout << a[i]<< " ";
return 0;
}
四、希爾排序
#include <iostream>
using namespace std;
void insert_sort(int a[], int n){
int i, j, temp;
int gap;
do{
gap = gap/3 + 1;
for( i=gap; i < n; i += gap){
if(a[i] < a[i-gap]){
temp = a[i];
for( j = i-gap; a[j] > temp && j >= 0; j -= gap){
a[j+gap] = a[j];
}
a[j+gap] = temp;
}
}
}while(gap > 1);
}
int main()
{
int a[10]{5,2,6,0,3,9,1,7,4,8};
cout << "排序前:" << endl;
for(int i = 0; i < 10; ++i)
cout << a[i]<< " ";
insert_sort(a,10);
cout << "\n\n\n排序後:" << endl;
for(int i = 0; i < 10; ++i)
cout << a[i]<< " ";
return 0;
}
五、堆排序
#include <iostream>
using namespace std;
void Swap(int a[], int i, int j){
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void HeapAdjust(int a[], int s, int n){
int i;
int temp = a[s];
for(i = 2*s; i <= n; i*=2){
if(i < n && a[i] < a[i+1]){
++i;
}
if(temp >= a[i]){
break;
}
a[s] = a[i];
s = i;
}
a[s] = temp;
}
void Heap_sort(int a[], int n){
int i;
for(i = n/2; i > 0; --i)
HeapAdjust(a,i,n);
for(i = n; i > 1; --i){
Swap(a,1,i);
HeapAdjust(a,1,i-1);
}
}
int main()
{
int a[10]{-1,5,2,6,0,3,9,1,7,4};
cout << "排序前:" << endl;
for(int i = 1; i < 10; ++i)
cout << a[i]<< " ";
Heap_sort(a,9);
cout << "\n\n\n排序後:" << endl;
for(int i = 1; i < 10; ++i)
cout << a[i]<< " ";
return 0;
}
六、歸併排序-遞歸實現
#include <iostream>
#define MAXSIZE 10
using namespace std;
void merging(int *list1, int list1_size, int *list2, int list2_size){
int temp[MAXSIZE];
int i, j, k;
i = j = k = 0;
while(i < list1_size && j < list2_size){
if(list1[i] < list2[j]){
temp[k++] = list1[i++];
}else {
temp[k++] = list2[j++];
}
}
while(i < list1_size){
temp[k++] = list1[i++];
}
while(j < list2_size){
temp[k++] = list2[j++];
}
for(int m = 0; m < (list1_size+list2_size); m++){
list1[m] = temp[m];
}
}
void Merge_sort(int a[],int n){
if(n > 1){
int *list1 = a;
int list1_size = n/2;
int *list2 = a + n/2;
int list2_size = n - list1_size;
Merge_sort(list1,list1_size);
Merge_sort(list2,list2_size);
merging(list1,list1_size,list2,list2_size);
}
}
int main()
{
int a[10]{3,2,6,5,4,8,9,0,7,1};
cout << "排序前: ";
for(int i=0; i < 10; ++i)
cout <<a[i]<< " ";
Merge_sort(a,10);
cout << endl<< "排序後: ";
for(int i=0; i < 10; ++i)
cout <<a[i]<< " ";
cout << endl;
return 0;
}
七、快速排序
#include <iostream>
using namespace std;
void Swap(int a[], int low, int high){
int temp;
temp = a[low];
a[low] = a[high];
a[high] = temp;
}
int Partition(int a[], int low, int high){
int point;
point = a[low];
while(low < high){
while(low < high && a[high] >= point){
high--;
}
Swap(a,low,high);
while(low < high && a[low] <= point){
low++;
}
Swap(a,low,high);
}
return low;
}
void Qsort(int a[],int low,int high){
int point;
if(low < high){
point = Partition(a,low,high);
Qsort(a,low,point-1);
Qsort(a,point+1,high);
}
}
void Quick_sort(int a[],int n){
Qsort(a,0,n-1);
}
int main()
{
int a[10]{3,2,6,5,4,8,9,0,7,1};
cout << "排序前: ";
for(int i=0; i < 10; ++i)
cout <<a[i]<< " ";
Quick_sort(a,10);
cout << endl<< "排序後: ";
for(int i=0; i < 10; ++i)
cout <<a[i]<< " ";
cout << endl;
return 0;
}