2469 views|16 replies

9702

Posts

24

Resources
The OP
 

Array sorting problem [Copy link]

 

There is an array uint8_t buf[] = {7,8,9,1,2,3,4,5,6};

There is no need to judge the values stored in the array. If only one variable "uint8_t tmp" is allowed, how can I quickly set buf to {1, 2, 3, 4, 5, 6, 7, 8, 9}?

This post is from Talking

Latest reply

In fact, if you consider the size, tmp can be omitted   Details Published on 2021-2-7 15:19
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 

1w

Posts

25

Resources
2
 

Bubble sort?

This post is from Talking

Comments

This is equivalent to shifting 123456 to the left three times, and then adding 789 to the end of 6. It would be very convenient to create another array, but if the array is very long and the memory is limited, how to process the array quickly is a problem.  Details Published on 2020-12-24 23:39
This is equivalent to shifting 123456 to the left three times, and then adding 789 to the end of 6. It would be very convenient to create another array, but if the array is very long and the memory is limited, how to process the array quickly is a problem.  Details Published on 2020-12-24 23:35
 
 
 

9702

Posts

24

Resources
3
 
dcexpert published on 2020-12-24 22:14 Bubble sort?

This is equivalent to shifting 123456 to the left three times, and then adding 789 to the end of 6. It would be very convenient to create another array, but if the array is very long and the memory is limited, how to process the array quickly is a problem.

This post is from Talking

Comments

Isn't it similar to bubble sort?  Details Published on 2020-12-25 08:47
 
 
 

9702

Posts

24

Resources
4
 
dcexpert published on 2020-12-24 22:14 Bubble sort?

This is different from bubbling because you don't need to consider the contents of the array. You just move the data after the specified index to the front and then add the data before the index to the back.

This post is from Talking
 
 
 

321

Posts

1

Resources
5
 

Can it be like this: define it a little larger uint8_t buf[] = {7,8,9,1,2,3,4,5,6,0,0,0}; then define a pointer to point to uint8_t *ptr=&buf[3]; each time sorting, just move the content of 789 to the end, so that ptr[] can be used as the sorted array.

This post is from Talking
Personal signature模电临时工
 
 
 

1w

Posts

25

Resources
6
 
littleshrimp published on 2020-12-24 23:35 It is equivalent to shifting 123456 to the left three times, and then adding 789 to the end of 6. It would be convenient to create another array, but if the array is very long and the memory is limited...

Isn't it still similar to bubble sort? It is slightly less efficient, but simple and takes up less memory.

This post is from Talking
 
 
 

4005

Posts

0

Resources
7
 

The sorting algorithm is related to the characteristics of the data. Since your data is too small, it is difficult to say what the characteristics are.

I remember that heap sort is the fastest in basically ordered situations, but I don't remember it clearly.

This post is from Talking
 
 
 

4005

Posts

0

Resources
8
 

The efficiency issue must be combined with the actual situation. For example, each piece of data corresponds to a file on the disk, and it takes a lot of time to move a file. At this time, it is very important to minimize the number of moves.

This post is from Talking
 
 
 

6062

Posts

4

Resources
9
 
This post was last edited by damiaa on 2020-12-25 14:52

选择排序慢了点
//冒泡法快点

This post is from Talking
 
 
 

6040

Posts

204

Resources
10
 

This is a homework question in a data structure book.

Step 1: Reverse the order of 7-9

Step 2: Reverse the order of 1-6

Step 3: Flip the entire array

This post is from Talking

Comments

This approach can be done, but it requires many steps.  Details Published on 2020-12-28 14:57
 
 
 

9702

Posts

24

Resources
11
 

The position of each data in the data must be moved, and the minimum number of moves is buf_len + 1 times

I can think of moving data in the following way

But I have no idea how to write the code yet

This post is from Talking
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

1942

Posts

2

Resources
12
 

This is a question worth thinking about. Write it down in a small notebook first. You may come up with some good ideas.

This post is from Talking
 
 
 

256

Posts

0

Resources
13
 

Use y= x+4 [0 ,5] ; y=x-6 [6,9] to convert the coordinates of buf[y];

This post is from Talking
 
 
 

9702

Posts

24

Resources
14
 
lcofjp posted on 2020-12-25 11:18 This is a homework question in a data structure book. Step 1: Reverse the order of 7-9. Step 2: Reverse the order of 1-6. Step 3: Reverse the whole...

This approach can be done, but it requires many steps.

This post is from Talking
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

5

Posts

0

Resources
15
 

Sorting is too simple. Usually, questions about circular ordered arrays test binary search.

This post is from Talking
 
 
 

40

Posts

0

Resources
16
 


uint8_t buf[] = {7,8,9,1,2,3,4,5,6};

#define change(src, a, b) if((a) > (b)){(src) = (a); (a) = (b); (b) = (src);}
void main()
{
uint8_t tmp;
change(tmp, buf[0], buf[1]);
change(tmp, buf[0], buf[2]);
change(tmp, buf[0], buf[3]);
change(tmp, buf[0], buf[4]);
change(tmp, buf[0], buf[5]);
change(tmp, buf[0], buf[6]);
change(tmp, buf[0], buf[7]);
change(tmp, buf[0], buf[8]);

change(tmp, buf[1], buf[2]);
change(tmp, buf[1], buf[3]);
change(tmp, buf[1], buf[4]);
change(tmp, buf[1], buf[5]);
change(tmp, buf[1], buf[6]);
change(tmp, buf[1], buf[7]);
change(tmp, buf[1], buf[8]);

change(tmp, buf[2], buf[3]);
change(tmp, buf[2], buf[4]);
change(tmp, buf[2], buf[5]);
change(tmp, buf[2], buf[6]);
change(tmp, buf[2], buf[7]);
change(tmp, buf[2], buf[8]);

change(tmp, buf[3], buf[4]);
change(tmp, buf[3], buf[5]);
change(tmp, buf[3], buf[6]);
change(tmp, buf[3], buf[7]);
change(tmp, buf[3], buf[8]);

change(tmp, buf[4], buf[5]);
change(tmp, buf[4], buf[6]);
change(tmp, buf[4], buf[7]);
change(tmp, buf[4], buf[8]);

change(tmp, buf[5], buf[6]);
change(tmp, buf[5], buf[7]);
change(tmp, buf[5], buf[8]);

change(tmp, buf[6], buf[7]);
change(tmp, buf[6], buf[8]);

change(tmp, buf[7], buf[8]);
}

This post is from Talking
 
 
 

40

Posts

0

Resources
17
 

In fact, if you consider the size, tmp can be omitted

This post is from Talking
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list