当前位置:网站首页>Adjust the array order so that odd numbers precede even numbers (C language)
Adjust the array order so that odd numbers precede even numbers (C language)
2022-06-11 11:56:00 【Butayarou】
Let's take a look at this problem .
subject : Enter an array of integers , Implement a function to adjust the order of the Numbers in the array , Make all odd numbers in the first half of the array , All even numbers are in the second half of the array .
Title source ( Power button ): Adjust the array order so that the odd Numbers precede the even Numbers
First of all, our idea must be to use pointers , But how to use , It will produce different solutions .
Method 1 : Regardless of relative order
Let's look at the first solution :
The basic idea :
Define two pointers left and right ( One in the head , One at the end ), send left Move right , Skip odd , Until the value pointed to is even . send right Move left , Skip the even , Until the value pointed to is an odd number . When they all stop , Exchange the values pointed to by two pointers respectively , Repeat the above operation , Until two pointers complete the task .
void exchange(int* nums, int numsSize)
{
int left = 0; // Give Way left Point to the head
int right = numsSize - 1; // Give Way right Pointing tail
while (left < right) // The loop condition
{
while ((nums[left] & 1) == 1 && left < right) //left Skip odd , Until it stops at an even number
{
left++;
}
while ((nums[right] & 1) == 0 && left < right) //right Skip the even , Until it stops at an odd number
{
right--;
}
if (left < right) // Exchange terms , Prevent the exchange result from not meeting the requirements
{
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
}
}
}
Here's another detail , Just the two inside while In cyclic conditions left < right All of them have to be written .
If you don't write , When an array of odd or even numbers is passed in , That would cause Out of bounds access to arrays ( Not necessarily wrong ). That tells us , Be as thoughtful as possible , Especially some special test cases .
But the above method also has some disadvantages , That is, it will disturb the original relative order between odd numbers or even numbers , Is there any other way to make up for this defect ?
The answer is yes .
Method 2 : Consider the relative order
For example, the following method :
void exchange(int* nums, int numsSize)
{
int i = 0; // First of all, will i Initialize to 0
for (int j = 0; j < numsSize; j++)
{
if (nums[j] & 1 == 1) /* Conditions for entry :nums[j] Is odd */
{
int tmp = nums[j]; /* use tmp Save it nums[j] */
for (int k = j - 1; k >= i; k--) /* take i and j Even numbers between move back one position */
{
nums[k + 1] = nums[k];
}
nums[i++] = tmp; /* take tmp The value of is put into an extra space in front due to movement , Let the pointer i Point to the latter position */
}
}
}
The basic idea :
Let the pointer j Traverse backwards from the beginning , Stop at odd numbers , And then use temporary variables tmp Save the odd number , Put the pointer i ( The pointer i The preceding ones are all odd numbers ) To pointer j Even numbers between move back one position , And then tmp The value of is put into an extra space in front due to movement , Let the pointer i Point to the latter position . Repeat the above , until j Traverse the entire array .
There may be other ways , If you are interested , You can continue to think about other solutions .
More articles :
Let you understand the sorting of choices (C Language )
Let you understand bubble sort (C Language )
边栏推荐
- MYCAT sub database and sub table
- my.cnf中 [mysql]与[mysqld] 的区别 引起的binlog启动失败的问题
- Hang up the interviewer
- Memory mapping image of the grayloc module in the surfacefinder process
- my. Binlog startup failure caused by the difference between [mysql] and [mysqld] in CNF
- 刷题笔记(十四)--二叉树:层序遍历和DFS,BFS
- JS 加法乘法错误解决 number-precision
- Smart sidebar plug-in: Mo widgets
- 中级web开发工程师,面试题+笔记+项目实战
- Elk - hearthbeat implements service monitoring
猜你喜欢

Use of RadioButton in QT

Node connects to MySQL database and writes fuzzy query interface

广东市政安全施工资料管理软件2022新表格来啦

ELK - Hearthbeat实现服务监控

Gestion de projets logiciels 7.1. Concept de base du calendrier du projet
![[JUC supplementary] atomic class, unsafe](/img/24/e51cfed39fe820fb46cca548af1782.jpg)
[JUC supplementary] atomic class, unsafe

C reads TXT file to generate word document
![[第二章 基因和染色体的关系]生物知识概括–高一生物](/img/f0/9f78682798a7874ba176797a6b22ca.png)
[第二章 基因和染色体的关系]生物知识概括–高一生物

Jest unit test description config json

CVPR 2022 𞓜 text guided entity level image manipulation manitrans
随机推荐
Modify WordPress management account name plug-in: admin rename extended
my.cnf中 [mysql]与[mysqld] 的区别 引起的binlog启动失败的问题
Let WordPress support registered users to upload custom avatars
JS addition and multiplication error resolution number precision
WordPress site link modification plug-in: Velvet Blues update URLs
WordPress landing page customization plug-in recommendation
[Chapter II Relationship between genes and chromosomes] summary of biological knowledge - Biology in grade one of senior high school
PS does not display text cursor, text box, and does not highlight after selection
Liufan, CFO of papaya mobile, unleashes women's innovative power in the digital age
Notes on topic brushing (XIV) -- binary tree: sequence traversal and DFS, BFS
The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)
快速搭建ELK7.3
在畢設中學習03
Elk - x-pack set user password
安全工程师发现PS主机重大漏洞 用光盘能在系统中执行任意代码
Use of RadioButton in QT
Études à la fin de l'enseignement 03
解决swagger文档接口404的问题
Full Permutation (recursion, backtracking)
Use compiler option '--downleveliteration' to allow iteration of iterations