0

Sorting C

I have an array that is sorted by the bubble method.I need sorting by even indices. I understand that this needs to be done through for (i = 2; i <20; i + = 2) but nothing works. Please help My code: #include <stdio.h> main() { int arr[20]={-12,0,3,34,2,99,81,21,75,7,48,-5,31,95,77,43,-50,88,-4,46}, i, j, b; for (i=0; i<20; i++) { printf("%4d", arr[i]); } printf("\n"); for (i=0; i < 20-1; i++) { for (j=0; j < 20-i-1; j++) { if (arr[j] < arr[j+1]) { b = arr[j]; arr[j] = arr[j+1]; arr[j+1] = b; } } } for (i=0; i<20; i++) printf("%4d", arr[i]); printf("\n"); } This code sorts all elements of an array.

19th Nov 2018, 1:07 PM
Alex
Alex  - avatar
13 Antworten
+ 4
Alex well, I think I got it working, with a help from a friend. Check this code hope it suits what you want to do; https://code.sololearn.com/c78fkfd5VuiM/?ref=app
20th Nov 2018, 7:54 AM
Ipang
+ 4
"I don’t want to complicate anything. Your code has a strange output, not the same as needed" It doesn't flex the mind too much if you move your attention from additional decoration to the core mechanics of these two guys and the way they work together // Outer loop is a helper loop: since we are discarding odd indexes 20 steps to traverse the whole list will be (20 - 1) / 2 which is 9 steps int half_range = (N - 1) / 2; for (int i = 0, s; i < half_range; ++i) { int travers_till = N - 2 * (i + 1); for (int j = 0; j < travers_till; j += 2) { if (arr[j] < arr[j + 2]) { /*SWAPPING*/ } } } Process in details (consider even steps for j): i = 0, travers_till = 18, j = {0, 2, 4, ..., 16} i = 1, travers_till = 16, j = {0, 2, 4, ..., 14} i = 2, travers_till = 14, j = {0, 2, 4, ..., 12} i = 3, travers_till = 12, j = {0, 2, 4, ..., 10} i = 4, travers_till = 10, j = {0, 2, 4, ..., 8} i = 5, travers_till = 8, j = {0, 2, 4, 6} i = 6, travers_till = 6, j = {0, 2, 4} i = 7, travers_till = 4, j = {0, 2} i = 8, travers_till = 2, j = {0}
20th Nov 2018, 12:17 PM
Babak
Babak - avatar
+ 3
Alex , You can copy just the sorting block from line 42 to 54 if you don't want the other parts. And replace the "N" in the array declaration, and in the for loops declaration with 20 (number of array elements). Basically, these are the most important part, the part that sorts the elements on even indices. Ask again if you have any doubts ...
20th Nov 2018, 8:33 AM
Ipang
+ 2
The output seems to be well sorted, each pair also are sorted, in descending order. Can you give a little example of how exactly the output should be? or maybe you have solved this and updated the code?
19th Nov 2018, 1:40 PM
Ipang
+ 2
What happens with numbers greater than 81? BTW, this is what I see, I put each pair in square brackets, as I see it, they are sorted, sorry but I don't understand what the problem here. [99 95] [88 81] [77 75] [48 46] [43 34] [31 21] [7 3] [2 0] [-4 -5] [-12 -50]
19th Nov 2018, 2:23 PM
Ipang
+ 2
Do you mean to sort the elements on even indices, only elements on even indices are sorted? I'm confused ... (Edit) Also do you mean to ignore the elements on odd indices all together?
19th Nov 2018, 2:33 PM
Ipang
+ 2
Then you will read, compare, and swap (if necessary) the items on index [0], [2], [4], [6], ..., [18] Looks pretty tough, I'll see if I can think of something, I'll get back to you later if I can. I think you need to rephrase the original post so the problem can be understood easier by later viewers. P.S. If you want, I can delete my posts from here, so it returns to "Unanswered" state (no response) to prevent others misunderstanding this may have been solved, and refrained ...
19th Nov 2018, 2:44 PM
Ipang
+ 1
yes, I need sorting by even indices
19th Nov 2018, 2:34 PM
Alex
Alex  - avatar
+ 1
Ipang ,this is not what I need, I need to modify my code, I don’t want to complicate anything. Your code has a strange output, not the same as needed
20th Nov 2018, 8:16 AM
Alex
Alex  - avatar
+ 1
Ipang,ok,I'll try when I'm home and accomplish my goal, thanks
20th Nov 2018, 8:37 AM
Alex
Alex  - avatar
0
output should be: 81,77,75,48,31,3,2,-4,-50 (i.e [6]=81,77=[14],75=[8]..and so on)
19th Nov 2018, 2:03 PM
Alex
Alex  - avatar
0
my goal is to arrange items that are in pair positions in descending order
19th Nov 2018, 2:29 PM
Alex
Alex  - avatar
0
[2],[4],[6],[8],[10],[12]....[18] but descending
19th Nov 2018, 2:31 PM
Alex
Alex  - avatar