为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
C

课程名称: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 (双指针)的写法

图片描述

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消