这篇文章包含了插入排序,希尔排序,堆排序,归并排序和快速排序,是前几篇文章的集合。
一共包括三个文件
sort.h
sort.cpp
main.cpp
1.main.cpp
1 #include <iostream> 2 #include "sort.h" 3 using namespace std; 4 5 int main() 6 { 7 \\test code 8 return 0; 9 }
2.sort.h
1 #ifndef SORT_H_ 2 #define SORT_H_ 3 void intersort(int *nums,int n); 4 void shellsort(int *nums,int n); 5 void percdown(int *num,int p,int n); 6 void heapsort(int *nums,int n); 7 void mergesort(int *nums,int n); 8 void msort(int *nums,int*tmp,int lp,int rp); 9 void merge(int *nums,int *tmp,int lp,int rp,int over); 10 void swap(int &m,int &n); 11 int mid3(int *nums,int left,int right); 12 void qs(int*nums,int left,int right); 13 void quicksort(int *nums,int n); 14 #endif // SORT_H_
3.sort.cpp
1 #include "sort.h" 2 #include<iostream> 3 4 void intersort(int *nums,int n) 5 { 6 int i,j; 7 int tmp; 8 for(i=1;i<n;i++) 9 { 10 tmp=nums[i]; 11 for(j=i;j>0&&tmp<nums[j-1];j--) 12 nums[j]=nums[j-1]; 13 nums[j]=tmp; 14 } 15 } 16 17 void shellsort(int *nums,int n) 18 { 19 int gap,i,j; 20 int tmp; 21 for(gap=n/2;gap>0;gap/=2) 22 { 23 for(i=gap;i<n;i++) 24 { 25 tmp=nums[i]; 26 for(j=i;j>0;j-=gap) 27 if(tmp<nums[j-gap]) 28 nums[j]=nums[j-gap]; 29 else 30 break; 31 nums[j]=tmp; 32 } 33 } 34 } 35 36 void heapsort(int *nums,int n) 37 { 38 int i; 39 for(i=n/2;i>=0;i--) 40 percdown(nums,i,n); 41 for(i=n-1;i>0;i--) 42 { 43 nums[0]^=nums[i]; 44 nums[i]^=nums[0]; 45 nums[0]^=nums[i]; 46 percdown(nums,0,i); 47 } 48 } 49 void percdown(int *nums,int p,int n) 50 { 51 int tmp; 52 int child; 53 for(tmp=nums[p];2*p+1<n;p=child) 54 { 55 child=2*p+1; 56 if(2*p+2!=n&&nums[2*p+2]>nums[child]) 57 child++; 58 if(nums[child]>tmp) 59 nums[p]=nums[child]; 60 else 61 break; 62 } 63 nums[p]=tmp; 64 65 } 66 67 void mergesort(int *nums,int n) 68 { 69 int *tmp=new int[n]; 70 if(tmp) 71 { 72 msort(nums,tmp,0,n-1); 73 delete[]tmp; 74 } 75 } 76 void msort(int *nums,int*tmp,int lp,int rp) 77 { 78 int center=(lp+rp)/2; 79 if(lp<rp) 80 { 81 msort(nums,tmp,lp,center); 82 msort(nums,tmp,center+1,rp); 83 merge(nums,tmp,lp,center+1,rp); 84 } 85 } 86 void merge(int *nums,int *tmp,int lp,int rp,int over) 87 { 88 int i=lp,j=rp,p=lp; 89 while(i<rp&&j<=over) 90 { 91 if(nums[i]<nums[j]) 92 tmp[p++]=nums[i++]; 93 else 94 tmp[p++]=nums[j++]; 95 } 96 while(i<=rp-1) 97 tmp[p++]=nums[i++]; 98 while(j<=over) 99 tmp[p++]=nums[j++]; 100 while(lp<=over) 101 { 102 nums[lp]=tmp[lp]; 103 lp++; 104 } 105 } 106 107 108 void quicksort(int *nums,int n) 109 { 110 qs(nums,0,n-1); 111 } 112 void qs(int*nums,int left,int right) 113 { 114 int pv; 115 int i,j; 116 int cutoff=10; 117 if(left+cutoff<=right) 118 { 119 pv=mid3(nums,left,right); 120 i=left; 121 j=right-1; 122 while(1) 123 { 124 while(nums[++i]<pv); 125 while(nums[--j]>pv); 126 if(i<j) 127 swap(nums[i],nums[j]); 128 else 129 break; 130 } 131 swap(nums[i],nums[right-1]); 132 qs(nums,left,i); 133 qs(nums,i+1,right); 134 } 135 else 136 intersort(nums+left,right-left+1); 137 } 138 int mid3(int *nums,int left,int right) 139 { 140 int center=(left+right)/2; 141 if(nums[left]>nums[center]) 142 swap(nums[left],nums[center]); 143 if(nums[left]>nums[right]) 144 swap(nums[left],nums[right]); 145 if(nums[center]>nums[right]) 146 swap(nums[center],nums[right]); 147 swap(nums[center],nums[right-1]); 148 return nums[right-1]; 149 } 150 void swap(int &m,int &n) 151 { 152 m^=n; 153 n^=m; 154 m^=n; 155 }
时间: 2024-10-20 19:41:18