Problem in C: "The string containing integers (maximum 15 numbers) is specified. Arrange the integers in the string in ascending order."
|
3 answers
#include <stdio.h> int main() { int i, j, str[15]; for (i=0; i < sizeof str/sizeof(int); i++) scanf ("%d", str+i); for (i=0; i < sizeof str/sizeof(int)-1; i++) for (j=i; j < sizeof str/sizeof(int); j++) if (str[j]<str[i]) { int w; w = str[i]; str[i] = str[j]; str[j] = w; } for(i=0; i < sizeof str/sizeof(int); i++) printf(" %d", str[i]); return 0; }
Go down on the top three?
- On deuce, if the specialty is closely related to programming :-) - psyhitus
- Come on you. If you remove the infinite sizeof on the sizeof-th sizeof-th drive, then it will come down to the top three: D Although, it doesn’t bother to think about sorting, but it’s too much a school decision: I sorted in the ninth grade: D - cy6erGn0m
|
I advise you to familiarize yourself with the typical sorting algorithms . By applying any of them, you get a solution.
|
Offended, in textbooks tychut. The rating is reduced. And I just wanted to help the questioner. Well, here's a textbook for you (I don’t remember which one, just like the name of the algorithm):
#include <stdio.h> void sort1(unsigned int m, unsigned int *b, unsigned int *e) { unsigned int *b1, *e1, m1; b1 = b; e1 = e; while(b1 < e1) { if(*b1&m) if(*e1&m) e1--; else { unsigned int w; w = *b1; *b1 = *e1; *e1 = w; } else b1++; } m1 = m >> 1; if(m1) { if((*b&m) == (*e&m)) sort1(m1, b, e); else { if((*b&m) != (*b1&m)) --b1; if(b1 > b) sort1(m1, b, b1); if(e > b1 + 1) sort1(m1, b1+1, e); } } } void sort(const unsigned int n, unsigned int str[]) { unsigned int i, m; for(i=1, m = str[0]; i < n; i++) m = str[i] > m ? str[i] : m; while(m) { i = m; m &= m - 1; } sort1(i, str, str+n-1); } int main() { unsigned int n, i, m, str[15]; n = 0; while(n < sizeof str/sizeof(unsigned int) -1 && (i = scanf("%td", str+n++)) != EOF) {} if (i == EOF) --n; sort(n, str); for (i = 0; i < n; ) printf(" %td", str[i++]); return 0; }
And what, you like it more?
|