查找与排序

来源:互联网 发布:c语言单引号怎么打 编辑:程序博客网 时间:2024/06/05 09:55

1.查找

线性表、树表、哈希表

1.1线性表

顺序查找、折半查找、分块查找

1.1.1顺序查找。

顺序查找。

1.1.2折半查找

折半查找,只适用于有序表,且仅限于顺序存储结构,对线性链表无法进行有效的折半查找(折半查找low会指向一个元素而high会指向相邻的下一个元素,然后low和high指向同一个元素,再然后low>high,循环结束,所以循环结束的条件为low<=high)。注意不要忘记最会返回-1。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include"stdafx.h"  
  2. #include<iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. //返回所查找数值在数组中的位置,如果没有则返回-1  
  7. int binarySearch(int* data, int n, int v){  
  8.     if(data == NULL || n <= 0)  
  9.         return -1;  
  10.     int low = 0;  
  11.     int high = n - 1;  
  12.     int middle;  
  13.     while(low <= high){  
  14.         middle = (low + high)/2;  
  15.         if(data[middle] == v)  
  16.             return middle;  
  17.         if(data[middle] < v)  
  18.             low = middle + 1;  
  19.         else  
  20.             high = middle -1;  
  21.     }  
  22.     return -1;  
  23. }  
  24.   
  25. int main(){  
  26.     int data[] = {1,2};  
  27.     cout<<binarySearch(data, 2 ,2);  
  28. }  

1.1.3分块查找(索引顺序查找)

分块查找:分块查找又称为索引顺序查找,索引表采用(折半查找),块内(顺序查找)处理线性表既希望有较快的查找速度又需要动态的变化,则可以采用分块查找的方法。 

1.2树表

二叉排序树(二叉查找树)、平衡二叉树


1.3哈希表

哈希表的构造方法:直接定址法、数字分析法、平方取中法、折叠法、除留取余法、随机数法。 

解决冲突的方法: 开放定址法、再哈希法、链地址法。   


2.排序

插入排序、快速排序、选择排序、归并排序

2.1插入排序

直接插入排序、折半插入排序、希尔排序

2.1.1直接插入排序

待排记录的数量很小时使用。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include"stdafx.h"  
  2. #include<iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void insertSort(int* a,int n){  
  7.     if(a == NULL || n <= 0){  
  8.         return;  
  9.     }  
  10.     int i,j,temp;  
  11.     for(i = 1; i < n; i++){  
  12.         for(j = i; j > 0 && a[j] < a[j-1]; j--){  
  13.             temp = a[j];  
  14.             a[j] = a[j-1];  
  15.             a[j-1] = temp;  
  16.         }  
  17.     }  
  18. }  
  19.   
  20. int main(){  
  21.     int a[] = {5,3,6,3,1,4,7,2};  
  22.     insertSort(a,8);  
  23.     for(int i = 0; i < 8; i++){  
  24.             cout<<a[i]<<" ";  
  25.     }  
  26. }  

2.1.2折半插入排序

在直接插入排序的基础上减少比较的次数。

算法关键:1.折半查找最后high和low会指向同一个元素,所以循环中判定条件为low<=high

2.关键字相同时,要到高半区,包装算法的稳定性

3.元素插入位置在high+1处

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include"stdafx.h"  
  2. #include<iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void binaryInsertSort(int* a,int n){  
  7.     if(a == NULL || a <= 0)  
  8.         return;  
  9.   
  10.     int low;  
  11.     int high;  
  12.     int middle;  
  13.     int temp;  
  14.   
  15.     for(int i = 1; i < n; i++){  
  16.         low = 0;  
  17.         high = i -1;  
  18.         //折半查找最后low和high会指向同一个元素  
  19.         while(low <= high){  
  20.             middle = (low + high)/2;  
  21.             //关键字相同时,到高半区,保证稳定性  
  22.             if(a[i] >= a[middle]){  
  23.                 low = middle + 1;  
  24.             }else{  
  25.                 high = middle -1;  
  26.             }  
  27.         }  
  28.         temp = a[i];  
  29.         for(int j = i; j >= high + 2; j--){  
  30.             a[j] = a[j-1];  
  31.         }  
  32.         //插入位置在high+1处  
  33.         a[high + 1] = temp;  
  34.     }  
  35. }  
  36.   
  37. int main(){  
  38.     int a[] = {5,3,6,3,1,4,7,2};  
  39.     binaryInsertSort(a,8);  
  40.     for(int i = 0; i < 8; i++){  
  41.             cout<<a[i]<<" ";  
  42.     }  
  43. }  

2.1.3希尔排序

当待排记录序列基本有序且数目较少时,直接插入排序效率较高,希尔排序正是从这两点分析出发对直接插入排序进行改进得到的一种插入排序算法。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include"stdafx.h"  
  2. #include<iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void shellInsert(int* a,int n, int k){  
  7.     if(a == NULL || a <= 0 || k <= 0)  
  8.         return;  
  9.     int i,j,temp;  
  10.     for(i = k; i < n; i++){  
  11.         for(j = i; j > 0;){  
  12.             if(a[j] < a[j-k]){  
  13.                 temp = a[j];  
  14.                 a[j] = a[j-k];  
  15.                 a[j-k] = temp;  
  16.             }  
  17.             j = j-k;  
  18.         }  
  19.     }  
  20.       
  21. }  
  22.   
  23. void shellSort(int* a ,int n,int ks[],int t){  
  24.     for(int i = 0; i < t; i++){  
  25.         shellInsert(a,n,ks[i]);  
  26.     }  
  27. }  
  28.   
  29. int main(){  
  30.     int ks[] = {4,2,1};  
  31.     int a[] = {5,3,6,3,1,4,7,2};  
  32.     shellSort(a,8,ks,3);  
  33.     for(int i = 0; i < 8; i++){    
  34.             cout<<a[i]<<" ";    
  35.     }   
  36. }  


2.2快速排序

冒泡排序和快速排序是借助“交换”进行排序的方法。

2.2.1冒泡排序

一般算法(最好情况下时间复杂度也为n^2):

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void bubbleSort(int* a, int n){  
  7.     if(a == NULL || n <=0){  
  8.         return;  
  9.     }  
  10.     int i,j,temp;  
  11.     for(i = 0; i < n; i++){  
  12.         for(j = n-1; j > i; j--){  
  13.             if(a[j] < a[j-1]){  
  14.                 temp = a[j];  
  15.                 a[j] = a[j-1];  
  16.                 a[j-1] = temp;  
  17.             }  
  18.         }  
  19.     }  
  20. }  
  21.   
  22. int main(){  
  23.     int a[] = {5,3,6,3,1,4,7,2};  
  24.     bubbleSort(a,8);  
  25.     for(int i = 0; i < 8; i++){  
  26.             cout<<a[i]<<" ";  
  27.     }  
  28. }  

优化算法,往上冒(最好情况下时间复杂度为n):

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void bubbleSort(int* a, int n){  
  7.     if(a == NULL || n <=0){  
  8.         return;  
  9.     }  
  10.     int i,j,temp;  
  11.     bool move;  
  12.     for(i = 0; i < n; i++){  
  13.         move = false;  
  14.         for(j = n-1; j > i; j--){  
  15.             if(a[j] < a[j-1]){  
  16.                 temp = a[j];  
  17.                 a[j] = a[j-1];  
  18.                 a[j-1] = temp;  
  19.                 move = true;  
  20.             }  
  21.         }  
  22.         if(!move){  
  23.             break;  
  24.         }  
  25.     }  
  26. }  
  27.   
  28. int main(){  
  29.     int a[] = {5,3,6,3,1,4,7,2};  
  30.     bubbleSort(a,8);  
  31.     for(int i = 0; i < 8; i++){  
  32.             cout<<a[i]<<" ";  
  33.     }  
  34. }  

优化算法,往下沉(最好情况下时间复杂度为n):

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void bubbleSort(int* a, int n){  
  7.     if(a == NULL || n <= 0){  
  8.         return;  
  9.     }  
  10.     int i,j,temp;  
  11.     bool move;  
  12.     for(i = n-1; i >=1; i--){  
  13.         move = false;  
  14.         for(j =0; j < i; j++){  
  15.             if(a[j] > a[j+1]){  
  16.                 temp = a[j+1];  
  17.                 a[j+1] = a[j];  
  18.                 a[j] = temp;  
  19.                 move = true;  
  20.             }  
  21.         }  
  22.         if(!move)  
  23.             break;  
  24.     }  
  25. }  
  26.   
  27. int main(){  
  28.     int a[] = {5,3,6,3,1,4,7,2};    
  29.     bubbleSort(a,8);    
  30.     for(int i = 0; i < 8; i++){    
  31.             cout<<a[i]<<" ";    
  32.     }    
  33. }  

2.2.2快速排序

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. include"stdafx.h"  
  2. #include<iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void quickSort(int* a,int l,int h){  
  7.     if(a == NULL || l < 0 || h < 0){  
  8.         cout<<"invalid param"<<endl;  
  9.         return;  
  10.     }  
  11.     if(l < h){  
  12.         int low = l;  
  13.         int high = h;  
  14.         int temp = a[low];  
  15.         while(low < high){  
  16.             while(low < high){  
  17.                 if(a[high] >= temp){  
  18.                     high--;  
  19.                 }else{  
  20.                     a[low] = a[high];  
  21.                     low++;  
  22.                     break;  
  23.                 }  
  24.             }  
  25.             while(low < high){  
  26.                 if(a[low] <= temp){  
  27.                     low++;  
  28.                 }else{  
  29.                     a[high] = a[low];  
  30.                     high--;  
  31.                     break;  
  32.                 }  
  33.             }  
  34.         }  
  35.         a[low] = temp;  
  36.         quickSort(a,l,high-1);  
  37.         quickSort(a,high+1,h);  
  38.     }  
  39. }  
  40.   
  41. int main(){  
  42.       
  43.     int a[] = {5,3,6,3,1,4,7,2};  
  44.     quickSort(a,0,7);  
  45.     for(int i = 0; i < 8; i++){    
  46.             cout<<a[i]<<" ";    
  47.     }   
  48. }  

上述代码选取第一个元素为枢纽,若要随机选取枢纽元素,则首先将选取的元素与第一个元素对换即可。

2.3选择排序

简单选择排序、堆排序

2.3.1简单选择排序

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void simpleSelectSort(int* a, int n){  
  7.     if(a == NULL || n <= 0){  
  8.         return;  
  9.     }  
  10.     int i,j,min,temp;  
  11.     for(i = 0; i <= n -2; i++){  
  12.         min = i;  
  13.         for(j = i; j <= n-1; j++){  
  14.             if(a[j] < a[min]){  
  15.                 min = j;  
  16.             }  
  17.         }  
  18.         temp = a[i];  
  19.         a[i] = a[min];  
  20.         a[min] = temp;  
  21.     }  
  22.       
  23. }  
  24.   
  25. int main(){  
  26.     int a[] = {5,3,6,3,1,4,7,2};    
  27.     simpleSelectSort(a,8);    
  28.     for(int i = 0; i < 8; i++){    
  29.             cout<<a[i]<<" ";    
  30.     }    
  31. }  

2.3.2堆排序

思想:

(1)将初始待排序关键字序列(R1,R2....Rn)构建成大顶堆;

(2)将堆顶元素R[1]与最后一个元素R[n]交换,此时得到新的无序区(R1,R2,......Rn-1)和新的有序区(Rn);

(3)由于交换后新的堆顶R[1]可能违反堆的性质,因此需要对当前无序区(R1,R2,......Rn-1)调整为新堆,然后再次将R[1]与无序区最后一个元素交换,得到新的无序区

(R1,R2....Rn-2)和新的有序区(Rn-1,Rn)。不断重复此过程直到有序区的元素个数为n-1,则整个排序过程完成。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. //a[s..m]中除a[s]之外均满足堆的定义,  
  7. //本函数调整a[s],使a[s..m]成为一个大顶堆  
  8. void heapAdjust(int* a, int s, int m){  
  9.     int temp = a[s];  
  10.     for(int i = 2 * s + 1; i <= m; i = i * 2 + 1){  
  11.         if(i < m && a[i] < a[i+1])  
  12.             i++;  
  13.         if(temp >= a[i])  
  14.         {  
  15.             break;  
  16.         }else{  
  17.             a[s] = a[i];  
  18.             s = i;  
  19.         }  
  20.     }  
  21.     a[s] = temp;  
  22. }  
  23.   
  24. int main(){  
  25.     int a[] = {5,3,6,3,1,4,7,2};  
  26.     int n = 8;  
  27.     for(int i = n/2-1; i >= 0; i--){  
  28.         heapAdjust(a,i,n-1);  
  29.     }  
  30.     for(int i = 0; i < 8; i++){  
  31.             cout<<a[i]<<" "<<endl;  
  32.     }  
  33.     int temp;  
  34.     for(int i = n-1; i >= 1; i--){  
  35.         temp = a[0];  
  36.         a[0] = a[i];  
  37.         a[i] = temp;  
  38.         heapAdjust(a,0,i-1);  
  39.     }  
  40.     for(int i = 0; i < 8; i++){  
  41.             cout<<a[i]<<" ";  
  42.     }  
  43. }  

2.4归并排序

与快速排序和堆排序相比,归并排序的最大特点是,它是一种稳定的排序方法。但在一般情况下很少利用2-路归并排序法进行内部排序。实现归并排序需和待排记录等数量的辅助空间,时间复杂度为O(nlogn)。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void merge(int a[],int start,int mid,int end,int temp[])  
  7. {  
  8.     int i = start;  
  9.     int j = mid +1;  
  10.     int k = start;  
  11.     while(i<=mid&&j<=end)  
  12.     {  
  13.         if(a[i]<=a[j])  
  14.         {  
  15.             temp[k++]=a[i++];  
  16.         }  
  17.         else  
  18.         {  
  19.             temp[k++]=a[j++];  
  20.         }  
  21.     }  
  22.     while(i<=mid)  
  23.     {  
  24.         temp[k++]=a[i++];  
  25.     }  
  26.     while(j<=end)  
  27.     {  
  28.         temp[k++]=a[j++];  
  29.     }  
  30.     for(int m=start;m<=end;m++)  
  31.     {  
  32.         a[m] = temp[m];  
  33.     }  
  34. }  
  35.   
  36. void mSort(int a[],int start,int end,int temp [])  
  37. {  
  38.     if(start<end)  
  39.     {  
  40.         int mid = (start+end)/2;  
  41.         mSort(a,start,mid,temp);  
  42.         mSort(a,mid+1,end,temp);  
  43.         merge(a,start,mid,end,temp);  
  44.     }  
  45. }  
  46.   
  47. void mergeSort(int a[],int n)  
  48. {  
  49.     int* temp = new int[n];  
  50.     mSort(a,0,n-1,temp);  
  51.         delete [] temp;  
  52.  }  
  53.   
  54. int main()  
  55. {  
  56.     int a[] = {3,5,7,2,1,6};  
  57.     int n = 6;  
  58.     mergeSort(a,n);  
  59.     for(int i=0;i<n;i++)  
  60.     {  
  61.         cout<<a[i]<<" ";  
  62.     }  
  63. }  


性能比较排序方法平均情况(时间复杂度)最坏情况最好情况空间复杂度稳定性直接插入排序n^2n^2n1稳定折半插入排序n^2  1稳定希尔排序   1不稳定冒泡排序n^2n^2n1稳定快速排序n*lognn^2n*lognlogn不稳定简单选择排序n^2n^2n^21不稳定堆排序n*lognn*lognn*logn1不稳定归并排序n*lognn*lognn*lognn稳定

1.折半插入排序的元素比较次数由于采用了折半查找,相对与直接插入排序减少了,时间复杂度为n*logn,但是元素的移动次数并未减少,因此时间复杂度仍未n^2。

2.希尔排序是不稳定的。(希尔排序的时间复杂度是O(n的1.25次方)~O(1.6n的1.25次方) 这是一个经验公式,好像没人解释过,就是一句经验得出的。

希尔排序的分析是一个复杂的问题,以为它的时间是所取“增量”序列的函数,这涉及到一些数学上尚未解决的难题。 数据结构书上这么说的 )

3.快速排序会递归log(n)次,每次对n个数进行一次处理,所以他的时间复杂度为n*log(n)。

4.简单选择排序稳定性:举个例子,序列5 8 5 2 9, 我们知道第一遍选择第1个元素5会和2交换,那么原序列中2个5的相对前后顺序就被破坏了,所以选择排序不是一个稳定的排序算法。

5.冒泡排序和简单选择排序的区别:冒泡算法,每次比较如果发现较小的元素在后面,就交换两个相邻的元素。而选择排序算法的改进在于:先并不急于调换位置,先从A[1]开

始逐个检查,看哪个数最小就记下该数所在的位置P,等一躺扫描完毕,再把A[P]和A[1]对调,这时A[1]到A[10]中最小的数据就换到了最前面的位置。

所以,选择排序每扫描一遍数组,只需要一次真正的交换,而冒泡可能需要很多次。比较的次数是一样的。

6.堆排序在最坏的情况下时间复杂度也为n*logn,相对于快速排序来说这是堆排序的最大优点。


附:冒泡排序最坏情况是把顺序的排列变成逆序,或者把逆序的数列变成顺序。在这种情况下,每一次比较都需要进行交换运算。

举个例子来说,一个数列 5 4 3 2 1 进行冒泡升序排列,第一次大循环从第一个数(5)开始到倒数第二个数(2)结束,比较过程:先比较5和4,4比5小,交换位置变成4 5 3 2

1;比较5和3,3比5小,交换位置变成4 3 5 2 1……最后比较5和1,1比5小,交换位置变成4 3 2 1 5。这时候共进行了4次比较交换运算,最后1个数变成了数列最大数。

第二次大循环从第一个数(4)开始到倒数第三个数(2)结束。进行3次比较交换运算。……所以总的比较次数为 4 + 3 + 2 + 1 = 10次。

对于n位的数列则有比较次数为 (n-1) + (n-2) + ... + 1 = n * (n - 1) / 2,这就得到了最大的比较次数。而O(N^2)表示的是复杂度的数量级。

计算时间复杂度时要找基本操作,比如在冒泡排序中,比较是基本操作,而交换不是,因为比较每次都需要做,而交换不一定。

               转载自:http://blog.csdn.net/ywk253100/article/details/22265517#t0

0 0
原创粉丝点击