知识总结(6):数据结构和算法

排序算法

参考:https://blog.csdn.net/qq_40732350/article/details/84503788

冒泡排序:https://blog.csdn.net/qq_40732350/article/details/84527383


void Bubble_2( int r[], int n)
{  
    int i= n -1, j;
	int tmp;
    while ( i > 0)//注意点1
    {   
        int pos= 0;//注意点2
        for (j = 0; j < i; j++)
       {
            if (r[j] > r[j+1])
            {     
                r[j]   ^= r[j+1];
                r[j+1] ^= r[j];
                r[j]   ^= r[j+1];
				
                pos = j;//注意点3
            } 			
       }
        i = pos;//注意点4
		print(r, i);
    }
}

选择排序:https://blog.csdn.net/qq_40732350/article/details/84521656

void xuanzhe(int *p, int n)
{
	int i,j;
	int top_max;
	int top_min;
	int tmp = 0;
	for (i = 0; i < n/2; i++) {//注意点1
		top_max = n - i -1;//注意点2
		top_min = i;
		for (j = i; j < n - i;j++) {
			if (p[j] > p[top_max]) {
				top_max = j;
			}
			if (p[j] < p[top_min]) {
				top_min = j;
			}

		}
		if (top_min != i) {
			p[top_min]    ^= p[i];
			p[i]          ^= p[top_min];
			p[top_min]    ^= p[i];
		}
		if (top_max != n - i -1) {//注意点3
			p[top_max]    ^= p[n - i -1];
			p[n - i -1]   ^= p[top_max];
			p[top_max]    ^= p[n - i -1];
		}
		print(p, tmp++);
	}	
}

插入排序:https://blog.csdn.net/qq_40732350/article/details/84503669

void charu(int *p, int n)
{
	int i,j;
	int tmp, t = 0;
	for (i = 1; i < n; i++) {
		tmp = p[i];
		j = i;     //注意
		while ((j > 0) && (p[j - 1] > tmp)) {//注意是和tmp比较
			p[j] = p[j - 1];//注意
			j--;
		}
		p[j] = tmp;//注意
		print(p, t++);
	}
}

希尔排序:https://blog.csdn.net/qq_40732350/article/details/84503716

适合大部分都已经排序完成的情况。希尔排序 = 先分组 + 插入排序

void shellpaixu(int *p, int n)
{
    int i,j;
	int tmp;
	i = 1;
	int size = n;//间隔
	int start;
	int t = 0;
	for (size /= 2; size > 0; size /= 2) {
		for(start = 0; start < size; start++) {//start++:换到下一组
			//插入排序
			for (i = start + size; i < n; i += size) {//start + size:从第二个数开始
				tmp = p[i];                             
				j = i;
				while ((j > start) && (p[j - size] > tmp)) {
					p[j] = p[j - size];
					j -= size;
				}
				p[j] = tmp;
			}
		}
		print(p, t++);
	}
}

归并排序:https://blog.csdn.net/qq_40732350/article/details/84554394

优点:

  1. 在最坏的情况下,是排序法中最好的。
  2. 适合外部文件的排序。

快速排序:https://blog.csdn.net/qq_40732350/article/details/84536152

可以用栈来代替递归。

优化:参考:https://blog.csdn.net/lingling_nice/article/details/80943231

  1. 三数取中法,解决数据基本有序的(就是找到数组中最小下标,最大下标,中间下标的数字,进行比较,把中间大的数组放在最左边)
  2. 随机选取基准
  3. 对于很小和部分有序的数组,快排不如插排好。
  4. 在一次分割结束后,可以把与Key相等的元素聚在一起,继续下次分割时,不用再对与key相等元素分割
	int m=left+(right-left)/2;//找到中间的数字的下标
	if(arr[left]>arr[right])//最左大于最右的时候,交换左右
	{
		swap(arr,left,right);
	}
	if(arr[m]>arr[right])//如果中间的>right ,交换
	{
		swap(arr,m,right);
	}
	if(arr[m]>arr[left])//如果中间的>left,交换
	{
		swap(arr,m,right);
    }

优点:

缺点:

int partition(int a[], int low, int high)
{
    int privotKey = a[low];  //基准元素
    while(low < high)
    {
        while(low < high  && a[high] >= privotKey)
            --high;
        a[low] = a[high];     //将大于基准值的数放到低处
        while(low < high  && a[low] <= privotKey )
            ++low;
        a[high] = a[low];     //将小于基准值的数放到高处
    }
    a[low] = privotKey;//将基准放到合适的位置
    print(a,i++);
    return low;
}

void quickSort(int a[], int low, int high)
{
    if(low < high){
        int privotLoc = partition(a,  low,  high);  //将表一分为二
        quickSort(a,  low,  privotLoc -1);          //递归对低子表递归排序
        quickSort(a,   privotLoc + 1, high);        //递归对高子表递归排序
    }
}

堆排序:https://blog.csdn.net/qq_40732350/article/details/84525277

从算法描述来看,堆排序需要两个过程:

  • 一是建立堆
  • 二是堆顶与堆的最后一个元素交换位置。

所以堆排序有两个函数组成。

  • 一是建堆的渗透函数
  • 二是反复调用渗透函数实现排序的函数。

优点:

缺点:

查找算法

参考:https://blog.csdn.net/QQ2558030393/article/details/89810356

猜你喜欢

转载自blog.csdn.net/QQ2558030393/article/details/89735994
今日推荐