【学习打卡】第5天 数组进阶

2022/8/9 3:22:52

本文主要是介绍【学习打卡】第5天 数组进阶,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

课程名称:C语言系统化精讲 重塑编程思想 打造坚实的开发基础

课程章节:第六章 玩转数组

授课老师:bennyhuo

二维数组

数组就是嵌套的数组,其遍历和赋值和一维数组大同小异

int vehicle[5][2] = {
	{0,5},
	[1][1] = {1,6},
	{2,7},
	{3,8},
	{4,9}
};
// int vehicle[5][2] ={0,5,1,6,2,7,3,8,4,9}  一行也是等价的。

//循环用二维for 遍历
for (int i = = 0 ; i< 5; i++)
	for (int j = 0 ; j< 2; j++){
		//statement
}

而如果对多维数组的某一行或某一列做操作,欲返回一个数组,可以用参数 resutl 返回

void SumIntArray(int rows , int columns, int array[][columns] ,int result[]){
	//statement
}

//其中 int array[][columns] 利用了VLA 的特性,即利用其列确定其长度。

打乱数组的位置

shuffle函数,其实就是洗牌函数。从后往前交换,每次随机选取一个元素 与当前元素交换。

#include <stdlib.h>
#include <time.h>

void SwapElement(int array[] , int first ,int second){
	int temp = array[first]
	array[first] = array[second]
	array[second] = temp;

}

void Shuffle(int array[] , int length){
	srand(time());

	for(int i = length - 1; i > 0 ; --i){
		int random_number == rand()% i;
		SwapElement(array, i , random_number);
	}
		
}

可以看看这个链接

有哪些算法惊艳到了你?

数组快排

快排就是分治法的典型排序算法:

  • 使用swap 可以在原地将数组 交换为前面为小于某个数目,后面大于某个数目。
int Partition ( int array[] ,int low ,int high ){
	
}

int QuickSort (int array[] ,int low ,int high){
	if(low >= high) return; 
	int partition = Partion(array, low, high);
	QuickSort(array, low ,partion-1);
	QuickSort(array, partion+1, high);

}

课程收获

  • 复习了快排和洗牌算法
  • 熟悉了快排 中原地 partion (双指针)的写法

图片描述



这篇关于【学习打卡】第5天 数组进阶的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程