-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSort.cpp
More file actions
147 lines (146 loc) · 2.75 KB
/
Sort.cpp
File metadata and controls
147 lines (146 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<iostream>
#include"Student.h"
#include"SortTestHelper.h"
using namespace std;
//选择排序
template<typename T>
void selectionSort(T arr[],int n){
for(int i=0;i<n;i++){
int minIndex=i;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[minIndex]){
minIndex=j;
}
}
swap(arr[i],arr[minIndex]);
}
}
//插入排序
template<typename T>
void insertionSort(T arr[],int n){
for(int i=1;i<n;i++){
T e=arr[i];
int j;
for(j=i;j>0&&arr[j-1]>e;j--){
arr[j]=arr[j-1];
}
arr[j]=e;
}
}
//冒泡排序
template<typename T>
void bubbleSort(T arr[],int n){
for(int i=0;i<n;i++){
for(int j=0;j<n-i;j++){
if(arr[j]<arr[j-1])
swap(arr[j],arr[j-1]);
}
}
}
//希尔排序
template<typename T>
void shellSort(T arr[],int n){
int gap=n/2;
while(gap>1){
for(int i=0;i<gap;i++){
if(arr[i]>arr[i+gap])
swap(arr[i],arr[i+gap]);
}
gap=gap/2;
}
insertionSort(arr,n);
}
//归并排序
template<typename T>
void mergeSort(T arr[],int l,int r){
if(r-l<1)
return;//递归终点
int m=(l+r)/2;
mergeSort(arr,l,m);
mergeSort(arr,m+1,r);
T temp[r-l+1];
for(int i=l;i<r+1;i++)
temp[i-l]=arr[i];//开辟临时数组
int i=l,j=m+1;
for(int k=l;k<r+1;k++){//k是最终下标
if(i>m){
arr[k]=temp[j-l];
j++;
}else if(j>r){
arr[k]=temp[i-l];
i++;
}
else if(temp[i-l]<temp[j-l]){
arr[k]=temp[i-l];
i++;
}else{
arr[k]=temp[j-l];
j++;
}
}
}
template<typename T>
void mergeSort(T arr[],int n){
mergeSort(arr,0,n-1);
}
//快速排序
template<typename T>
void quickSort(T arr[],int l,int r){
if(r-l<1)
return;
T v=arr[l];
int j=l;
for(int i=l+1;i<=r;i++){
if(arr[i]<v){
swap(arr[j+1],arr[i]);
j++;
}
}
swap(arr[l],arr[j]);
quickSort(arr,l,j-1);
quickSort(arr,j+1,r);
}
template<typename T>
void quickSort(T arr[],int n){
quickSort(arr,0,n-1);
}
int main(){
int a[10]={10,9,8,7,6,5,4,3,2,1};
selectionSort(a,10);
for(int i=0;i<10;i++){
cout<<a[i]<<" ";
}
cout<<endl;
float b[4]={4.4,3.3,2.2,1.1};
selectionSort(b,4);
for(int i=0;i<4;i++){
cout<<b[i]<<" ";
}
cout<<endl;
string c[4]={"d","c","b","a"};
selectionSort(c,4);
for(int i=0;i<4;i++){
cout<<c[i]<<" ";
}
cout<<endl;
Student d[4]={{"d",90},{"c",100},{"b",95},{"a",96}};
selectionSort(d,4);
for(int i=0;i<4;i++){
cout<<d[i];
}
cout<<endl;
int n=30000;
int *arr=SortTestHelper::generateNearlyOrderedArray(n,1000);
int *arr2=SortTestHelper::copyIntArray(arr,n);
int *arr3=SortTestHelper::generateRandomArray(10,1,1000);
SortTestHelper::printArray(arr3,10);
quickSort(arr3,0,9);
SortTestHelper::printArray(arr3,10);
SortTestHelper::testSort("Insertion Sort",insertionSort,arr,n);
SortTestHelper::testSort("Selection Sort",selectionSort,arr2,n);
cout<<endl;
delete[] arr;
delete[] arr2;
delete[] arr3;
return 0;
}