数据结构——浙大版中国大学mooc(week9)

《数据结构》09-排序3 Insertion or Heap Sort 题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input dat... 阅读详情

课程给出代码

C语言:插入排序

void InsertionSort( ElementType A[], int N )
{ /* 插入排序 */
     int P, i;
     ElementType Tmp;
      
     for ( P=1; P<N; P++ ) {
         Tmp = A[P]; /* 取出未排序序列中的第一个元素*/
         for ( i=P; i>0 && A[i-1]>Tmp; i-- )
             A[i] = A[i-1]; /*依次与已排序序列中元素比较并右移*/
         A[i] = Tmp; /* 放进合适的位置 */
     }
}

C语言: 希尔排序 - 用Sedgewick增量序列

void ShellSort( ElementType A[], int N )
{ /* 希尔排序 - 用Sedgewick增量序列 */
     int Si, D, P, i;
     ElementType Tmp;
     /* 这里只列出一小部分增量 */
     int Sedgewick[] = {929, 505, 209, 109, 41, 19, 5, 1, 0};
      
     for ( Si=0; Sedgewick[Si]>=N; Si++ ) 
         ; /* 初始的增量Sedgewick[Si]不能超过待排序列长度 */
 
     for ( D=Sedgewick[Si]; D>0; D=Sedgewick[++Si] )
         for ( P=D; P<N; P++ ) { /* 插入排序*/
             Tmp = A[P];
             for ( i=P; i>=D && A[i-D]>Tmp; i-=D )
                 A[i] = A[i-D];
             A[i] = Tmp;
         }
}

C语言:堆排序

void Swap( ElementType *a, ElementType *b )
{
     ElementType t = *a; *a = *b; *b = t;
}
  
void PercDown( ElementType A[], int p, int N )
{ /* 改编代码4.24的PercDown( MaxHeap H, int p )    */
  /* 将N个元素的数组中以A[p]为根的子堆调整为最大堆 */
    int Parent, Child;
    ElementType X;
 
    X = A[p]; /* 取出根结点存放的值 */
    for( Parent=p; (Parent*2+1)<N; Parent=Child ) {
        Child = Parent * 2 + 1;
        if( (Child!=N-1) && (A[Child]<A[Child+1]) )
            Child++;  /* Child指向左右子结点的较大者 */
        if( X >= A[Child] ) break; /* 找到了合适位置 */
        else  /* 下滤X */
            A[Parent] = A[Child];
    }
    A[Parent] = X;
}
 
void HeapSort( ElementType A[], int N ) 
{ /* 堆排序 */
     int i;
       
     for ( i=N/2-1; i>=0; i-- )/* 建立最大堆 */
         PercDown( A, i, N );
      
     for ( i=N-1; i>0; i-- ) {
         /* 删除最大堆顶 */
         Swap( &A[0], &A[i] ); /* 见代码7.1 */
         PercDown( A, 0, i );
     }
}

C语言:归并排序 - 递归实现

/* 归并排序 - 递归实现 */
 
/* L = 左边起始位置, R = 右边起始位置, RightEnd = 右边终点位置*/
void Merge( ElementType A[], ElementType TmpA[], int L, int R, int RightEnd )
{ /* 将有序的A[L]~A[R-1]和A[R]~A[RightEnd]归并成一个有序序列 */
     int LeftEnd, NumElements, Tmp;
     int i;
      
     LeftEnd = R - 1; /* 左边终点位置 */
     Tmp = L;         /* 有序序列的起始位置 */
     NumElements = RightEnd - L + 1;
      
     while( L <= LeftEnd && R <= RightEnd ) {
         if ( A[L] <= A[R] )
             TmpA[Tmp++] = A[L++]; /* 将左边元素复制到TmpA */
         else
             TmpA[Tmp++] = A[R++]; /* 将右边元素复制到TmpA */
     }
 
     while( L <= LeftEnd )
         TmpA[Tmp++] = A[L++]; /* 直接复制左边剩下的 */
     while( R <= RightEnd )
         TmpA[Tmp++] = A[R++]; /* 直接复制右边剩下的 */
          
     for( i = 0; i < NumElements; i++, RightEnd -- )
         A[RightEnd] = TmpA[RightEnd]; /* 将有序的TmpA[]复制回A[] */
}
 
void Msort( ElementType A[], ElementType TmpA[], int L, int RightEnd )
{ /* 核心递归排序函数 */ 
     int Center;
      
     if ( L < RightEnd ) {
          Center = (L+RightEnd) / 2;
          Msort( A, TmpA, L, Center );              /* 递归解决左边 */ 
          Msort( A, TmpA, Center+1, RightEnd );     /* 递归解决右边 */  
          Merge( A, TmpA, L, Center+1, RightEnd );  /* 合并两段有序序列 */ 
     }
}
 
void MergeSort( ElementType A[], int N )
{ /* 归并排序 */
     ElementType *TmpA;
     TmpA = (ElementType *)malloc(N*sizeof(ElementType));
      
     if ( TmpA != NULL ) {
          Msort( A, TmpA, 0, N-1 );
          free( TmpA );
     }
     else printf( "空间不足" );
}

C语言:归并排序 - 循环实现

/* 归并排序 - 循环实现 */
/* 这里Merge函数在递归版本中给出 */
 
/* length = 当前有序子列的长度*/
void Merge_pass( ElementType A[], ElementType TmpA[], int N, int length )
{ /* 两两归并相邻有序子列 */
     int i, j;
       
     for ( i=0; i <= N-2*length; i += 2*length )
         Merge( A, TmpA, i, i+length, i+2*length-1 );
     if ( i+length < N ) /* 归并最后2个子列*/
         Merge( A, TmpA, i, i+length, N-1);
     else /* 最后只剩1个子列*/
         for ( j = i; j < N; j++ ) TmpA[j] = A[j];
}
 
void Merge_Sort( ElementType A[], int N )
{ 
     int length; 
     ElementType *TmpA;
      
     length = 1; /* 初始化子序列长度*/
     TmpA = malloc( N * sizeof( ElementType ) );
     if ( TmpA != NULL ) {
          while( length < N ) {
              Merge_pass( A, TmpA, N, length );
              length *= 2;
              Merge_pass( TmpA, A, N, length );
              length *= 2;
          }
          free( TmpA );
     }
     else printf( "空间不足" );
}

09-排序1 排序 (25 分)

给定N个(长整型范围内的)整数,要求输出从小到大排序后的结果。
本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:
数据1:只有1个元素;
数据2:11个不相同的整数,测试基本正确性;
数据3:103个随机整数;
数据4:104个随机整数;
数据5:105个随机整数;
数据6:105个顺序整数;
数据7:105个逆序整数;
数据8:105个基本有序的整数;
数据9:105个随机正整数,每个数字不超过1000。
输入格式:
输入第一行给出正整数N(≤E​5),随后一行给出N个(长整型范围内的)整数,其间以空格分隔。
输出格式:
在一行中输出从小到大排序后的结果,数字间以1个空格分隔,行末不得有多余空格。
强推代码来源: https://blog.youkuaiyun.com/xijujie/article/details/53103012

#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define Cutoff 10
#define Radix 10
 
void percdown(long *a, int n, int i);
void merge(long *a, long *tmp, int start, int end, int middle);
void msort(long *a, long *tmp, int start, int end);
void merge_pass(long *a, long *tmp, int n, int length);
void q_sort(long *a, int left, int right);
 
void bubble_sort(long *a, int n);
void insertion_sort(long *a, int n);
void selection_sort(long *a, int n);
void shell_sort(long *a, int n);
void shellsedgewick_sort(long *a, int n);
void heap_sort(long *a, int n);
void merge1_sort(long *a, int n);
void merge2_sort(long *a, int n);
void quick_sort(long *a, int n);
void radix_sort(long *a, int n);
 
int main() {
	int i, n;
	long a[N];
	scanf("%d", &n);
	for (i = 0;i < n;i++)
		scanf("%ld", &a[i]);
	radix_sort(a, n);
	printf("%ld", a[0]);
	for (i = 1;i < n;i++)
		printf(" %ld", a[i]);
	return 0;
}
 
//冒泡排序
void bubble_sort(long *a, int n) {
	int i, j, flag;
	long temp;
	for (i = n - 1;i > 0;i--) {
		flag = 0;
		for (j = 0;j < i;j++) {
			if (a[j] > a[j + 1]) {
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
				flag = 1;
			}
		}
		if (!flag) break;
	}
}
 
//插入排序
void insertion_sort(long *a, int n) {
	int i, j;
	long temp;
	for (i = 1;i < n;i++) {
		temp = a[i];
		for (j = i; j > 0 && a[j - 1] > temp; j--) 
				a[j] = a[j - 1];
		a[j] = temp;
	}
}
 
//选择排序
void selection_sort(long *a, int n) {
	int i, j, t;
	long temp;
	for (i = 0;i < n - 1;i++) {
		temp = a[i];
		t = i;
		for (j = i + 1;j < n;j++) {
			if (a[j] < temp) {
				temp = a[j];
				t = j;
			}	
		}
		a[t] = a[i];
		a[i] = temp;
	}
}
 
//希尔排序-希尔增量
void shell_sort(long *a, int n) {
	int i, j, d;
	long temp;
	for (d = n / 2;d > 0;d /= 2) {
		for (i = d;i < n;i++) {
			temp = a[i];
			for (j = i;j >= d && a[j - d] > temp;j -= d)
				a[j] = a[j - d];
			a[j] = temp;
		}
	}
}
 
//希尔排序-sedgewick增量
void shellsedgewick_sort(long *a, int n) {
	int i, j, d, si;
	int Sedgewick[] = { 929, 505, 209, 109, 41, 19, 5, 1, 0 };
	long temp;
	for (si = 0;Sedgewick[si] >= n;si++)
		;
	for (;Sedgewick[si] > 0;si++) {
		d = Sedgewick[si];
		for (i = d;i < n;i++) {
			temp = a[i];
			for (j = i;j >= d && a[j - d] > temp;j -= d)
				a[j] = a[j - d];
			a[j] = temp;
		}
	}
}
 
//堆排序
void heap_sort(long *a, int n) {
	int i;
	long temp;
	for (i = (n - 2) / 2; i >= 0; i--)
		percdown(a, n, i);
	for (i = n - 1;i > 0;i--) {
		temp = a[i];
		a[i] = a[0];
		a[0] = temp;
		percdown(a, i, 0);
	}
}
 
void percdown(long *a, int n, int i) {
	int child;
	long x = a[i];
	for (;i * 2 + 1 <= n - 1;i = child) {
		child = i * 2 + 1;
		if (child < n - 1 && a[child + 1] > a[child])
			child++;
		if (x >= a[child]) break;
		else a[i] = a[child];
	}
	a[i] = x;
}
 
//归并排序-递归实现
void merge1_sort(long *a, int n) {
	long *tmp = (long*)malloc(n*sizeof(long));
	msort(a, tmp, 0, n - 1);
	free(tmp);
}
 
void msort(long *a, long *tmp, int start, int end) {
	int middle;
	if (start < end) {
		middle = (start + end) / 2;
		msort(a, tmp, start, middle);
		msort(a, tmp, middle + 1, end);
		merge(a, tmp, start, end, middle);
	}
}
 
void merge(long *a, long *tmp, int start, int end, int middle) {
	int l, r, s;
	s = start;
	l = start;
	r = middle + 1;
	while (l <= middle && r <= end) {
		if (a[l] <= a[r]) tmp[s++] = a[l++];
		else tmp[s++] = a[r++];
	}
	while (l <= middle) tmp[s++] = a[l++];
	while (r <= end) tmp[s++] = a[r++];
	for (;start <= end;start++)
		a[start] = tmp[start];
}
 
//归并排序-循环实现
void merge2_sort(long *a, int n) {
	int length = 1;
	long *tmp = (long*)malloc(n*sizeof(long));
	while (length < n) {
		merge_pass(a, tmp, n, length);
		length *= 2;
		merge_pass(tmp, a, n, length);
		length *= 2;
	}
	free(tmp);
}
 
void merge_pass(long *a, long *tmp, int n, int length) {
	int i, j;
	for (i = 0;i + 2 * length <= n;i += 2*length) 
		merge(a, tmp, i, i + 2 * length - 1, i + length - 1);
	if (i + length <= n)
		merge(a, tmp, i, n - 1, i + length - 1);
	else
		for (j = i;j < n;j++)
			tmp[j] = a[j];
}
 
//快速排序
void quick_sort(long *a, int n) {
	q_sort(a, 0, n - 1);
}
 
void q_sort(long *a, int left, int right) {
	long pivot, temp;
	int i, j, center;
 
	if (right - left + 1 > Cutoff) {
		center = (left + right) / 2;
		if (a[center] < a[left]) {
			temp = a[center];a[center] = a[left];a[left] = temp;
		}
		if (a[right] < a[left]) {
			temp = a[right];a[right] = a[left];a[left] = temp;
		}
		if (a[right] < a[center]) {
			temp = a[right];a[right] = a[center];a[center] = temp;
		}
		temp = a[right - 1];a[right - 1] = a[center];a[center] = temp;
		pivot = a[right - 1];
 
		i = left;
		j = right - 1;
		for (;;) {
			while (a[++i] < pivot);
			while (a[--j] > pivot);
			if (i < j) {
				temp = a[i];a[i] = a[j];a[j] = temp;
			}
			else break;
		}
		temp = a[i];a[i] = a[right - 1];a[right - 1] = temp;
 
		q_sort(a, left, i - 1);
		q_sort(a, i + 1, right);
	}
	else
		insertion_sort(a + left, right - left + 1);
}
 
//基数排序-次位优先
struct Node {
	int data;
	Node* next;
};
struct Bucket {
	Node *head, *tail;
};
 
void radix_sort(long *a, int n) {
	int d, di, i, flag;
	long t;
	Bucket b[Radix*2 - 1];//19个桶 -9--1,0,1-9;
	Node *tmp, *p, *list = NULL;
 
	for (i = 0;i <= (Radix-1) * 2;i++)
		b[i].head = b[i].tail = NULL;
	for (i = n - 1;i >= 0;i--) {
		tmp = (Node*)malloc(sizeof(Node));
		tmp->data = a[i];
		tmp->next = list;
		list = tmp;
	}
	for (d = 1;;d++) {
		p = list;
		flag = 0;
		while (p) {
			t = p->data;
			for (i = 1;i <= d;i++) {
				di = t % Radix;t /= Radix;
			}
			if (di != 0) flag = 1;
			di += Radix-1;
			tmp = p;
			p = p->next;
			tmp->next = NULL;
			if (!b[di].head)
				b[di].head = b[di].tail = tmp;
			else {
				b[di].tail->next = tmp;
				b[di].tail = tmp;
			}
		}
 
		if (!flag) break;
		else {
			list = NULL;
			for (i = (Radix - 1) * 2;i >= 0;i--) {
				
				if (b[i].head) {
					b[i].tail->next = list;
					list = b[i].head;
					b[i].head = b[i].tail = NULL;
				}
			}
		}
	}
 
	for (i = 0;i < n;i++) {
		a[i] = b[Radix - 1].head->data;
		b[Radix - 1].head = b[Radix - 1].head->next;
	}
}

09-排序2 Insert or Merge (25 分)

According to Wikipedia:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.
Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in the first line either “Insertion Sort” or “Merge Sort” to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
代码来源: https://blog.youkuaiyun.com/xijujie/article/details/53155723

#include <iostream>
#include <algorithm>
int main() {
	int n, i, count, length, a[100], b[100];
	int flag = 1;
	scanf("%d", &n);
	for (i = 0;i < n;i++)
		scanf("%d", &a[i]);
	for (i = 0;i < n;i++)
		scanf("%d", &b[i]);
 
	//i从0到n-1遍历,当b[i+1]<b[i],继续遍历判断a[i] == b[i]
	//不相等,则为归并,否则为插入
	for (i = 1; b[i] >= b[i - 1]; i++);
	count = i;
	for (;i < n && a[i] == b[i];i++);
 
	if (i == n) {
		printf("Insertion Sort\n");
		std::sort(b, b + count + 1);
		printf("%d", b[0]);
		for (i = 1;i < n;i++)
			printf(" %d", b[i]);
	}
 
	else {
		printf("Merge Sort\n");
		for (length = 2;flag;length *= 2) {
			//在长度为length片段满足顺序排列的基础上判断length*2
			for (i = length; i < n; i += length * 2) {
				if (b[i - 1] > b[i]) {
					flag = 0;break;
				}
			}
		}
		for (i = 0;i + length <= n;i += length)
			std::sort(b + i, b + i + length);
		std::sort(b + i, b + n);
		printf("%d", b[0]);
		for (i = 1;i < n;i++)
			printf(" %d", b[i]);
		//另一种比较直接的做法是模拟merge过程,每一步都判断是否与b相等
	}
	return 0;
}

09-排序3 Insertion or Heap Sort (25 分)

According to Wikipedia:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.
Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in the first line either “Insertion Sort” or “Heap Sort” to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resulting sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
代码来源:https://blog.youkuaiyun.com/xijujie/article/details/53155865

#include <iostream>
#include <algorithm>
int main() {
	int n, i, count, length, a[100], b[100];
	int flag = 1;
	scanf("%d", &n);
	for (i = 0;i < n;i++)
		scanf("%d", &a[i]);
	for (i = 0;i < n;i++)
		scanf("%d", &b[i]);
 
	for (i = 1; b[i] >= b[i - 1]; i++);
	count = i;
	for (;i < n && a[i] == b[i];i++);
 
	if (i == n) {
		printf("Insertion Sort\n");
		std::sort(b, b + count + 1);
		printf("%d", b[0]);
		for (i = 1;i < n;i++)
			printf(" %d", b[i]);
	}
 
	else {
		printf("Heap Sort\n");
		for (i = n - 1;i > 0 && b[i] > b[i - 1];i--);
		std::pop_heap(b, b + i + 1);
		printf("%d", b[0]);
		for (i = 1;i < n;i++)
			printf(" %d", b[i]);
	}
	return 0;
}
Geant4学习——入门(基本概念、Geant4工具包的结构、强制类、可选类) Geant4是由欧洲核子研究组织基于C++面向对象技术开发的蒙特卡罗应用软件包,用于模拟粒子在物质中输运的物理过程。由于具有良好的通用性和扩展能力,Geant4在涉及微观粒子与物质相互作用的诸多领域获得了广泛应用。 第一部分 Geant4的基本概念 HEP中的模拟是“虚拟现实”。仿真既可用于在研发阶段帮助设计探测器,又可用于物理研究中了解探测器的响应。为了创建这样的虚拟现实,我们需要对粒子间的相互作用,几何形状和材料进行建模,以便将基本粒子传播到检测器中。我们还需要描述检测器生成原始数据的灵敏度。 Gea 阅读详情

相关推荐

双系统引导修复:GRUB消失后恢复Windows与Ubuntu启动菜单

在UEFI引导架构下,操作系统启动依赖于EFI系统分区(ESP)中存储的引导加载程序。当多个系统共存时,引导管理器负责识别并加载不同操作系统的引导文件。然而,Windows更新或系统调整时常会重置UEFI启动顺序,导致GRUB引导菜单消失,直接进入Windows。这一问题的技术价值在于理解UEFI启动流程与多系统引导管理机制,掌握ESP分区结构与引导文件修复方法。常见的应用场景包括双系统安装后的引导修复、系统更新后的启动项丢失以及磁盘分区调整导致的引导故障。本文针对**GRUB引导菜单消失**和**UEFI

weixin_29895109的博客 295

C++PTA A1098(插入堆排序)

请默写:插入排序sort版本与堆排序(25分)

执念斩长河 862

微软登录OAuth 2.0授权流程

OAuth 2.0是一个关于授权的开放网络标准,它允许第三方应用获取用户数据,是目前最流行的授权机制。微软身份平台支持OAuth 2.0授权码流程,使得客户端应用程序能够获得对受保护资源(如网络API)的授权访问。这一流程在保障用户数据安全的前提下,实现了应用的便捷登录与数据共享。当用户在一个支持OAuth 2.0的应用上,希望使用微软账号进行登录时,OAuth 2.0授权流程即被触发。

qq_36569837的博客 4094

线性代数mooc课(3.1)

看到线性变换这部分,  二阶矩阵可以实现对平面的 翻转,旋转,拉伸。 如何得到旋转的变换? 传统的做法:     设点A( x,y) 的极坐标 (r, θ ), 旋转d角度之后得到B(r, θ+d),   因为 x = rcosθ,  y=rsinθ 所以 B = (r*cos(θ+d), r*sin(θ+d)) =  ( xcos(d)-ysin(d),   y cos(d

A Little Programmer's Base 965

MTK Sensor驱动调试实战:从AP到SCP的完整流程解析

本文详细解析了MTK平台Sensor驱动从AP侧到SCP侧的完整调试流程。内容涵盖AP侧驱动集成、DTS配置、内核编译与验证,以及SCP侧Tinysys框架下的代码集成、Overlay配置与I2C设置等核心步骤,并分享了中断唤醒、功耗优化及传感器数据校准等实战经验与常见问题排查方法,为硬件驱动工程师提供了一份全面的实战指南。

weixin_29175469的博客 354

2025年GESP3月认证C++五级真题解析

小 A 有 2n 件物品,小 B 和小 C 想从小 A 手上买走这些物品。对于第 i 件物品,小 B 会以 bi 的价格购买,而小 C 会以 ci 的价格购买。对于所有测试点,保证 1 <= T <= 100, 2 <= g < p <= 10^9, g, p 为整数, p 为质数。对于所有测试点,保证 1 <= n <= 10^5, 0 <= bi, ci <= 10^9, bi, ci 为整数。对于另外 50 % 的测试点,保证 n <= 100000, 0 <= bi, ci <= 10^9。

lan_in的博客 1036

(c语言详解)09-排序2 Insert or Merge (详细解释)

首先肯定要将数组与输入的增量的序列作比较,如果相等说明就是这种排序,其次就是两种排序,第三就是main函数,比如在插入排序里做一下这样的手脚,他就在每趟排序的时候就会判断

执念斩长河 1502

13、多实例任务(multi instance)

多实例任务,是指一个类型的任务需要循环执行多次,包括串行多实例任务和并行多实例任务。

leyoliu的博客 3203

数据结构学习记录——堆的建立(最大堆的建立、思路图解、代码实现、代码解释)

对其进行调整,调整的思路和堆的删除相似,但是要从后往前开始,在左右两个堆中进行调整;从倒数第一个没有儿子的结点开始,即从最后一个非叶子节点开始,依次进行下滤操作,将其子树调整为最大堆。因为最后一个非叶子节点的数组下标为n/2,其中n为堆中元素的个数,所以从i=n/2开始循环。Parent表示当前结点的父结点,Child表示当前结点的子结点,X表示当前结点的值。然后,从要下滤的结点开始,沿着其左右子结点中较大的一个不断下滤,具体来说,每次将要下滤的结点与其左右子结点中较大的一个进行比较,

li13437542099的博客 4956

C#与欧姆龙PLC 通信——fins udp协议

该调试助手专门为了帮助大家学习以及测试Fins-Udp通信,下面进行详细介绍。

qq_34059233的博客 2876

沙猫群优化算法(SCSO)详解:从沙漠生存智慧到全局优化引擎

本文详细介绍了沙猫群优化算法(SCSO),这是一种新型元启发式优化算法,灵感来源于沙猫在沙漠环境中的卓越狩猎行为。SCSO通过模拟沙猫的低频探测能力和精确攻击策略,将优化问题转化为搜索和捕获"猎物"的过程。算法核心包括探索阶段(全局搜索)和开发阶段(局部精细搜索),通过灵敏度范围模型和自适应控制参数实现两者的平衡。SCSO具有参数简单、收敛性能好等优点,适用于解决复杂全局优化问题。

qq_42212808的博客 202

数据结构——浙大版中国大学mooc(week5)

树(下)课程给出代码05-树7 堆中的路径 (25 分)05-树8 File Transfer (25 分)05-树9 Huffman Codes (30 分) 课程给出代码 C语言描述:堆的描述和操作 typedef struct HNode *Heap; /* 堆的类型定义 */ struct HNode { ElementType *Data; /* 存储元素的数组 */ i...

CodingW的博客 393

【BlueZ】编译产物详解:可执行文件/库文件/工具的生成路径与用途

BlueZ二次开发编译产物机制解析 摘要:在BlueZ蓝牙协议栈二次开发中,90%的报错源于对编译产物类型和路径的混淆。本文系统剖析BlueZ的三阶段编译机制:1)配置阶段生成Makefile;2)编译阶段在.libs目录下产生临时库和可执行文件(不可直接运行);3)安装阶段将产物部署到系统标准路径。重点解析守护进程、命令行工具、动态库的核心路径差异,并针对五大高频问题(如临时库运行崩溃、动态库不匹配、系统覆盖等)给出解决方案。

byte轻骑兵的技术小窝 1万+

数据结构——浙大版中国大学mooc(week8)

图(下)课程给出代码08-图7 公路村村通 (30 分)08-图8 How Long Does It Take (25 分)08-图9 关键活动 (30 分) 课程给出代码 C语言:邻接矩阵存储 - Prim最小生成树算法 /* 邻接矩阵存储 - Prim最小生成树算法 */ Vertex FindMinDist( MGraph Graph, WeightType dist[] ) { /* ...

CodingW的博客 365

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【排序】堆排序

什么是堆 堆是一棵完全二叉树,可以用数组来存储。比如一个数组[3, 8, 15, 31, 24],具体为一个堆,它的逻辑结构如下所示:(图来自https://www.cnblogs.com/jingmoxukong/p/4303826.html#堆的概念,侵删) 最大堆和最小堆 最大堆:根结点的值是所有堆结点值中最大者,且以每个节点为根的所有子堆都为最大堆。 最小堆:根结点的值是所有...

denghong637573的博客 221

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

最大堆创建(过滤法)用c++编译运行

最大堆创建(过滤法)C语言代码

2301_80484340的博客 505

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

PATA 1098 Insertion or Heap Sort(25 分)解题报告

1098 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort remov...

热心市民小黎的博客 896

PAT甲级1093,1098解题报告

1093 Count PAT's (25 point(s)) The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4t...

TateBrwonJava的博客 466

1089. Insert or Merge (25)

(merge不能更坑。。。。)时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueAccording to Wikipedia:Insertion sortiterates, consuming one input element each repetition, and growing a sorted output list. ...

weixin_30527551的博客 150

PTA 09-排序2 Insert or Merge 及 09-排序3 Insertion or Heap Sort 两道题目分析

PTA-mooc完整题目解析及AC代码库:PTA(拼题A)-浙江大学中国大学mooc数据结构2020年春AC代码与题目解析(C语言) 这两道题比较类似,故放在一些进行分析。 09-排序2 Insert or Merge According to Wikipedia: Insertion sort iterates, consuming one input element each repetit...

zhuiyisinian的博客 501

1098. Insertion or Heap Sort (25)

According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, fi

accbbbxx的博客 426

Insert or Merge(25 分)

7-13 Insert or Merge(25 分) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort remove

A-Little-Nut的博客 822

7-12 排序 (25分)

冒泡排序: 外层循环N-1次,内循环每两个相邻的数相比较,如果前面的比后面大,二者交换。每一次外循环的结果是前N-i个数中最大的数放第N-i个位置(下标N-i-1)。将N-1个数处理完后,剩下的一个数自然在第一个位置,不需要处理,所以是N-1次外循环。 时间复杂度: 最坏:O(N^2) 最好:O(N) 1 void bubble_sort(long int a[]) 2 { 3 ...

weixin_43646077的博客 598

堆排序

一般的教科书上在heapsort函数构建堆的过程中写的是for(i = N / 2;i >= 0; i--) { PercDown(A, i, n); }实际上这个有丁点问题,数组是从下标0开始的,因此下标为i的结点的双亲为(i - 1) / 2;所以最后一个又孩子的结点的下标为(N - 1) / 2,读者可以亲自实验,因此应修正为:for(i = (N - 1) / 2;i >= 0; i

Zl1421169284的专栏 730

堆排序的C语言实现

#include<stdio.h>//堆排序 #define leftchild(i) (2*i+1) void percdown(int a[],int i,int n){ int child; int temp; for(temp=a[i];leftchild(i)<n;i=child){ child=leftchild(i); if(child!=n-1&&a[child+1]>a[child]) child++; if(temp&l.

m0_57673664的博客 947

排序——堆排序-大根堆(大顶堆)

1.小根堆 若根节点存在左子女则根节点的值小于左子女的值;若根节点存在右子女则根节点的值小于右子女的值。 2.大根堆 若根节点存在左子女则根节点的值大于左子女的值;若根节点存在右子女则根节点的值大于右子女的值。 3.结论 (1)堆是一棵完全二叉树(如果公有h层,那么1~h-1层均满,在h层连续缺失若干个右叶子)。 (2)小根堆的根节点的值是最小值,大根堆的根节点的值是最大值。 (3)

laoniu_c的专栏 2万+
上一篇: 数据结构——浙大版中国大学mooc(week8)
下一篇: 数据结构——浙大版中国大学mooc(week10)
CodingW
博客等级 码龄8年 3粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值