编写C++代码,对一组数据进行至少四种排序(直接插入排序、快速排序、希尔排序、直接选择排序、堆排序、归并排序)。要求数据随机生成1000个以上的数据,每种算法都要统计比较次数和移动次数。
时间: 2023-12-24 10:05:23 浏览: 95
好的,以下是C++代码实现六种排序算法,可以满足你的需求:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void print(int* a, int size) {
for(int i=0; i<size; i++)
cout<<a[i]<<" ";
cout<<endl;
}
void Swap(int* a, int* b) {
int temp=*a;
*a=*b;
*b=temp;
}
// 直接插入排序
void InsertionSort(int* a, int size, int& cmp, int& mov) {
for(int i=1; i<size; i++) {
int temp=a[i];
int j=i-1;
while(j>=0 && a[j]>temp) {
cmp++;
mov++;
a[j+1]=a[j];
j--;
}
cmp++;
a[j+1]=temp;
}
}
// 快速排序
int Partition(int* a, int low, int high, int& cmp, int& mov) {
int pivot=a[low];
while(low<high) {
while(low<high && a[high]>=pivot) {
cmp++;
high--;
}
cmp++;
mov++;
a[low]=a[high];
while(low<high && a[low]<=pivot) {
cmp++;
low++;
}
cmp++;
mov++;
a[high]=a[low];
}
a[low]=pivot;
return low;
}
void QuickSort(int* a, int low, int high, int& cmp, int& mov) {
if(low<high) {
int pivot=Partition(a, low, high, cmp, mov);
QuickSort(a, low, pivot-1, cmp, mov);
QuickSort(a, pivot+1, high, cmp, mov);
}
}
// 希尔排序
void ShellSort(int* a, int size, int& cmp, int& mov) {
int gap=size/2;
while(gap>0) {
for(int i=gap; i<size; i++) {
int temp=a[i];
int j=i-gap;
while(j>=0 && a[j]>temp) {
cmp++;
mov++;
a[j+gap]=a[j];
j-=gap;
}
cmp++;
a[j+gap]=temp;
}
gap/=2;
}
}
// 直接选择排序
void SelectionSort(int* a, int size, int& cmp, int& mov) {
for(int i=0; i<size-1; i++) {
int minIndex=i;
for(int j=i+1; j<size; j++) {
if(a[j]<a[minIndex]) {
cmp++;
minIndex=j;
}
cmp++;
}
Swap(&a[i], &a[minIndex]);
mov+=3;
}
}
// 堆排序
void AdjustHeap(int* a, int parent, int size, int& cmp, int& mov) {
int child=parent*2+1;
while(child<size) {
if(child+1<size && a[child]<a[child+1]) {
cmp++;
child++;
}
cmp++;
if(a[parent]<a[child]) {
Swap(&a[parent], &a[child]);
mov+=3;
parent=child;
child=parent*2+1;
}
else break;
}
}
void HeapSort(int* a, int size, int& cmp, int& mov) {
for(int i=size/2-1; i>=0; i--)
AdjustHeap(a, i, size, cmp, mov);
for(int i=size-1; i>=1; i--) {
Swap(&a[0], &a[i]);
mov+=3;
AdjustHeap(a, 0, i, cmp, mov);
}
}
// 归并排序
void Merge(int* a, int* b, int low, int mid, int high, int& cmp, int& mov) {
int i=low, j=mid+1, k=low;
while(i<=mid && j<=high) {
if(a[i]<=a[j]) {
cmp++;
b[k++]=a[i++];
}
else {
cmp++;
b[k++]=a[j++];
}
}
while(i<=mid) b[k++]=a[i++];
while(j<=high) b[k++]=a[j++];
for(int i=low; i<=high; i++) {
a[i]=b[i];
mov++;
}
}
void MergeSort(int* a, int* b, int low, int high, int& cmp, int& mov) {
if(low<high) {
int mid=(low+high)/2;
MergeSort(a, b, low, mid, cmp, mov);
MergeSort(a, b, mid+1, high, cmp, mov);
Merge(a, b, low, mid, high, cmp, mov);
}
}
int main() {
const int SIZE=1000;
int a[SIZE], b[SIZE];
srand(time(NULL));
for(int i=0; i<SIZE; i++)
a[i]=rand();
int cmp, mov;
cout<<"Insertion Sort:\n";
cmp=mov=0;
InsertionSort(a, SIZE, cmp, mov);
cout<<"Compare: "<<cmp<<endl;
cout<<"Move: "<<mov<<endl;
cout<<"Quick Sort:\n";
cmp=mov=0;
QuickSort(a, 0, SIZE-1, cmp, mov);
cout<<"Compare: "<<cmp<<endl;
cout<<"Move: "<<mov<<endl;
cout<<"Shell Sort:\n";
cmp=mov=0;
ShellSort(a, SIZE, cmp, mov);
cout<<"Compare: "<<cmp<<endl;
cout<<"Move: "<<mov<<endl;
cout<<"Selection Sort:\n";
cmp=mov=0;
SelectionSort(a, SIZE, cmp, mov);
cout<<"Compare: "<<cmp<<endl;
cout<<"Move: "<<mov<<endl;
cout<<"Heap Sort:\n";
cmp=mov=0;
HeapSort(a, SIZE, cmp, mov);
cout<<"Compare: "<<cmp<<endl;
cout<<"Move: "<<mov<<endl;
cout<<"Merge Sort:\n";
cmp=mov=0;
MergeSort(a, b, 0, SIZE-1, cmp, mov);
cout<<"Compare: "<<cmp<<endl;
cout<<"Move: "<<mov<<endl;
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)