稳定性:快速 希尔 选择 堆排序不稳定
时间复杂度:平均情况下,快速、希尔、归并和堆排序的时间复杂度均为O(nlog2(n)),其他都是O(n^2)。最坏情况下,快排的时间复杂度为O(n^2)
1 #include <iostream> 2 #include <stdlib.h> 3 #include <time.h> 4 #define N 1000 5 using namespace std; 6 7 //输出 8 void output(int a[], int num){ 9 for(int i=0;i<num;i++){ 10 cout<<a[i]<<" "; 11 } 12 cout<<endl; 13 } 14 15 //交换元素位置 16 void swap(int a[], int index1, int index2){ 17 int tmp = a[index1]; 18 a[index1] = a[index2]; 19 a[index2] = tmp; 20 } 21 22 //快速排序 23 void quickSort(int a[], int l, int r, int num){ 24 if(l<r){ 25 int left = l; 26 int right = r; 27 int tmp = a[l]; 28 while(left<right){ 29 while(left<right && a[right]>=tmp){ 30 right--; 31 } 32 if(left<right){ 33 a[left++] = a[right]; 34 } 35 while(left<right && a[left]<=tmp){ 36 left++; 37 } 38 if(left<right){ 39 a[right--] = a[left]; 40 } 41 } 42 a[left] = tmp; 43 //output(a,num); 44 quickSort(a,l,left-1,num); 45 quickSort(a,left+1,r,num); 46 } 47 } 48 49 //冒泡排序 50 void bubbleSort(int a[], int num){ 51 int i,j; 52 int flag = 0; 53 for(i=num-1;i>0;i--){ 54 flag = 0; 55 for(j=0;j<i;j++){ 56 if(a[j]>a[j+1]){ 57 swap(a,j,j+1); 58 flag=1; 59 } 60 } 61 if(flag==0){ 62 break; 63 } 64 } 65 } 66 67 //选择排序 68 void selectSort(int a[], int num){ 69 int right,i; 70 int maxIndex; 71 for(right=num-1;right>0;right--){ 72 maxIndex = 0; 73 for(i=0;i<right;i++){ 74 if(a[maxIndex]<a[i]){ 75 maxIndex = i; 76 } 77 } 78 swap(a,maxIndex,right); 79 } 80 81 } 82 83 //建立大顶堆 84 void buildHeap(int a[], int index, int len){ 85 int curParent = a[index]; 86 int child = index * 2 +1; 87 while(child<len){ 88 if(child+1<len && a[child]<a[child+1]){ 89 child++; 90 } 91 if(curParent < a[child]){ 92 a[index] = a[child]; 93 //这里不用把curParent赋值给child,因为还要迭代子树,如果孩子中有更大的,会上移,curParent还要继续下移 94 index = child; 95 child = child * 2 +1; 96 } else { 97 break; 98 } 99 } 100 a[index] = curParent; 101 } 102 103 /* 104 void buildHeap(int a[], int i, int len){ 105 int parent = a[i]; 106 int left = i*2+1; 107 while(child < len){ 108 if(child+1<len && a[child]<a[child+1]){ 109 child++; 110 } 111 if(parent<a[child]){ 112 a[i] = child; 113 child = 2*i+1; 114 } else { 115 break; 116 } 117 } 118 a[p] = parent; 119 }*/ 120 121 122 //堆排序 123 void heapSort(int a[], int num){ 124 int i; 125 for(i=num/2-1;i>=0;i--){ 126 buildHeap(a, i, num); 127 } 128 for(i=num-1;i>=0;i--){ 129 int max = a[0]; 130 a[0] = a[i]; 131 a[i] = max; 132 buildHeap(a,0,i); 133 } 134 135 } 136 //插入排序 137 void insertSort(int a[], int num){ 138 int i,j,tmp; 139 for(i=1;i<num;i++){ 140 tmp = a[i]; 141 for(j=i-1;j>=0;j--){ 142 if(a[j]>tmp){ 143 a[j+1] = a[j]; 144 } else { 145 break; 146 } 147 } 148 a[j+1] = tmp; 149 } 150 } 151 152 void merge(int a[], int first, int mid, int last){ 153 int *tmp = (int *)malloc((last-first+1)*sizeof(int)); 154 int i = first; 155 int j = mid+1; 156 int k = 0; 157 while(i<=mid && j<=last){ 158 if(a[i]<=a[j]){ 159 tmp[k++] = a[i++]; 160 } else { 161 tmp[k++] = a[j++]; 162 } 163 } 164 while(i<=mid){ 165 tmp[k++] = a[i++]; 166 } 167 while(j<=last){ 168 tmp[k++] = a[j++]; 169 } 170 for(i=first;i<=last;i++){ 171 a[i] = tmp[i-first]; 172 } 173 free(tmp); 174 } 175 176 void mergeSort(int a[], int first, int last){ 177 int mid = (first+last)/2; 178 if(first<last){ 179 mergeSort(a,first,mid); 180 mergeSort(a,mid+1,last); 181 merge(a,first,mid,last); 182 } 183 } 184 185 int main(){ 186 int num; 187 while(cin>>num){ 188 int i; 189 start = clock(); 190 int *arr = new int[num]; 191 for(int i=0; i<num; i++){ 192 arr[i] = rand()%1000; 193 //cin>>arr[i]; 194 } 195 cout<<"待排序数组为:"<<endl; 196 output(arr,num); 197 198 //quickSort(arr,0,num-1,num); //快速排序 199 //bubbleSort(arr,num); //冒泡排序 200 //selectSort(arr,num); //归并排序 201 //insertSort(arr,num); //插入排序 202 //heapSort(arr,num); //堆排序 203 //mergeSort(arr,0,num-1); //归并排序 204 stop = clock(); 205 cout<<stop-start<<endl; 206 207 cout<<"排序后的数组为:"<<endl; 208 output(arr,num); 209 210 } 211 return 0; 212 }
时间: 2024-11-01 15:02:50