排序(数据结构)

 

//排序:
//不稳定排序:口诀:考研复习,情绪不稳定,快(快速)些(希尔)选(选择)一堆(堆排序)研友
//时间复杂度为nlogn的排序:口诀:快(快速)些(希尔)归(归并)队(堆排序)
  1 #include <iostream>
  2 using namespace std;
  3 
  4 //排序:
  5 //不稳定排序:口诀:考研复习,情绪不稳定,快(快速)些(希尔)选(选择)一堆(堆排序)研友
  6 //时间复杂度为nlogn的排序:口诀:快(快速)些(希尔)归(归并)队(堆排序)
  7 
  8 void insertSort(int a[],int n) {//1.直接插入排序
  9     int i,j;
 10     for(i=1; i<n; i++) {
 11         int temp=a[i];
 12         for(j=i-1; j>=0&&a[j]>temp; j--) {
 13             a[j+1]=a[j];
 14         }
 15         a[j+1]=temp;
 16     }
 17 }
 18 void shellSort(int a[],int n) {//2.希尔排序
 19     for(int dk=n/2; dk>=1; dk/=2) {//确定步长
 20         for(int i=dk; i<n; i++) {//开始插入排序
 21             if(a[i]<a[i-dk]) {
 22                 int temp=a[i];
 23                 int j;
 24                 for(j=i-dk; j>=0&&a[j]>temp; j-=dk) {
 25                     a[j+dk]=a[j];
 26                 }
 27                 a[j+dk]=temp;
 28             }
 29         }
 30     }
 31 }
 32 void bubbleSort(int a[],int n) { //3.冒泡排序
 33     for(int i=0; i<n; i++) {
 34         for(int j=n-1; j>i; j--) {
 35             if(a[j]<a[j-1]) {
 36                 int temp=a[j-1];
 37                 a[j-1]=a[j];
 38                 a[j]=temp;
 39             }
 40         }
 41     }
 42 }
 43 void quickSort(int a[],int left,int right) {//4.快速排序 (排序越无序,本算法效率越高)
 44     int temp,i=left,j=right;
 45     if(left<right) {
 46         temp=a[i];
 47         while(i<j) {//完成一趟排序,比temp大的都在右边,小的都在左边
 48             while(i<j&&a[j]>=temp)j--;
 49             if(i<j) {
 50                 a[i]=a[j];
 51                 i++;
 52             }
 53             while(i<j&&a[i]<=temp)i++;
 54             if(i<j) {
 55                 a[j]=a[i];
 56                 j--;
 57             }
 58         }
 59         a[i]=temp;
 60         quickSort(a,left,i-1);//对左半部分排序
 61         quickSort(a,i+1,right);//对右半部分排序
 62     }
 63 }
 64 void selectSort(int a[],int n) { //5.选择排序
 65     for(int i=0; i<n; i++) {
 66         int k=i;
 67         for(int j=i; j<n; j++) {
 68             if(a[j]<a[k]) {
 69                 k=j;
 70             }
 71         }
 72         int temp=a[i];
 73         a[i]=a[k];
 74         a[k]=temp;
 75     }
 76 }
 77 void sift(int a[],int low,int high) {//堆排序时,对位置low上的结点进行调整 
 78     int i=low,j=2*i;
 79     int temp=a[i];
 80     while(j<=high) {
 81         if(j<high&&a[j]<a[j+1]) {
 82             j=j+1;
 83         }
 84         if(temp<a[j]) {
 85             a[i]=a[j];
 86             i=j;
 87             j=i*2;
 88         } else break;
 89     }
 90     a[i]=temp;
 91 }
 92 void heapSort(int a[],int n) {//6.堆排序 
 93     int i;
 94     int temp;
 95     for(i=n/2; i>=1; i--) {
 96         sift(a,i,n);
 97     }
 98     for(i=n; i>=2; i--) {
 99         temp=a[1];
100         a[1]=a[i];
101         a[i]=temp;
102         sift(a,1,i-1);
103     }
104 }
105 void mergeSort(int a[],int low,int high){//7.二路归并 
106     if(low<high){
107         int mid=low+(high-low)/2;
108         mergeSort(a,low,mid);
109         mergeSort(a,mid+1,high);
110         merge(a,low,mid,high);
111     }
112 } 
113 int main() {
114     int a[10]= {1,2,8,4,5,2,7,0,9,3};
115     for(int i=0; i<10; i++) {
116         cout<<a[i]<<" ";
117     }
118     cout<<endl;
119 //    insertSort(a,10);//1插入排序
120 //    shellSort(a,10);//2希尔排序
121 //    bubbleSort(a,10);//3冒泡排序
122 //    quickSort(a,0,9);//4快速排序
123 //    selectSort(a,10);//5选择排序
124     int b[11]= {0,1,2,8,4,5,2,7,0,9,3};//堆排序的数组从下标为1开始; 
125     heapSort(b,10);//6.堆排序 
126     for(int i=1; i<=10; i++) {
127         cout<<b[i]<<" ";
128     }
129     cout<<endl;
130     for(int i=0; i<10; i++) {
131         cout<<a[i]<<" ";
132     }
133     cout<<endl;
134     return 0;
135 }

 

posted @ 2020-04-16 16:16  瓜瓜爱呱呱  阅读(249)  评论(0编辑  收藏  举报