I need to sort an integer (A number tbh) without using an array on C
Method 1:
I suppose there are other, more elegant methods but the following method does follow the constraint of not using arrays or any kind of purposefully created contiguous blocks of memory to store elements, only individually created variables.
Without the use of an array, or any other form contiguous memory it becomes increasingly messy to assign, organize and sort a collection of individual values. (See case 10:
below for illustration) The larger the collection, the messier the task becomes. The following method(s) is limited to <=10 digits.
The following method (limited to 10 digits) uses a combination of the following:
- pre-defined set of
10
int
variables
switch()
within a for()
loop to separate and store digits
- Simple sorting network and a
SWAP
macro in a switch()
Output of following:
6539345654
sorted
3344555669
int a = -1;
int b = -1;
int c = -1;
int d = -1;
int e = -1;
int f = -1;
int g = -1;
int h = -1;
int i = -1;
int j = -1;
int tmp;
long long num = 4565439356;//input value for illustration
int main(void)
{
char buf[11] = {0};
sprintf(buf, "%lld", num);
int len = strlen(buf);
//separate and store digits
//for(int x=0; num; num /= 10, ++x)//reads digits from LSD to MSD
for(int x=9; num; num /= 10, --x)//reads digits from MSD to LSD
{
switch(x) {
case 0:
a = num%10;
break;
case 1:
b = num%10;
break;
case 2:
c = num%10;
break;
case 3:
d = num%10;
break;
case 4:
e = num%10;
break;
case 5:
f = num%10;
break;
case 6:
g = num%10;
break;
case 7:
h = num%10;
break;
case 8:
i = num%10;
break;
case 9:
j = num%10;
break;
}
}
//sort appropriate number of digits
switch(len) {
case 1:
a;
break;
case 2:
printf("%d%d\n", a,b);
SORT(a,b);
printf("sorted\n%d%d\n", a,b);
break;
case 3:
printf("%d%d%d\n", a,b,c);
SORT(a,b);
SORT(a,c);
SORT(b,c);
printf("sorted:\n%d%d%d\n", a,b,c);
break;
case 4:
printf("%d%d%d%d\n", a,b,c,d);
SORT(a,b);
SORT(a,c);
SORT(a,d);
SORT(b,c);
SORT(b,d);
SORT(c,d);
printf("sorted:\n%d%d%d%d\n", a,b,c,d);
break;
case 5:
printf("%d%d%d%d%d\n", a,b,c,d,e);
SORT(a,b);
SORT(a,c);
SORT(a,d);
SORT(a,e);
SORT(b,c);
SORT(b,d);
SORT(b,e);
SORT(c,d);
SORT(c,e);
SORT(d,e);
printf("sorted:\n%d%d%d%d%d\n", a,b,c,d,e);
break;
case 6:
printf("%d%d%d%d%d%d\n", a,b,c,d,e,f);
SORT(a,b);
SORT(a,c);
SORT(a,d);
SORT(a,e);
SORT(a,f);
SORT(b,c);
SORT(b,d);
SORT(b,e);
SORT(b,f);
SORT(c,d);
SORT(c,e);
SORT(c,f);
SORT(d,e);
SORT(d,f);
SORT(e,f);
printf("sorted:\n%d%d%d%d%d%d\n", a,b,c,d,e,f);
break;
case 7:
//and so on
break;
case 8:
i = num%10;
//and so on
case 9:
//and so on
break;
case 10:
printf("%d%d%d%d%d%d%d%d%d%d\n", a,b,c,d,e,f,g,h,i,j);
SORT(a,b);
SORT(a,c);
SORT(a,d);
SORT(a,e);
SORT(a,f);
SORT(a,g);
SORT(a,h);
SORT(a,i);
SORT(a,j);
SORT(b,c);
SORT(b,d);
SORT(b,e);
SORT(b,f);
SORT(b,g);
SORT(b,h);
SORT(b,i);
SORT(b,j);
SORT(c,d);
SORT(c,e);
SORT(c,f);
SORT(c,g);
SORT(c,h);
SORT(c,i);
SORT(c,j);
SORT(d,e);
SORT(d,f);
SORT(d,g);
SORT(d,h);
SORT(d,i);
SORT(d,j);
SORT(e,f);
SORT(e,g);
SORT(e,h);
SORT(e,i);
SORT(e,j);
SORT(f,g);
SORT(f,h);
SORT(f,i);
SORT(f,j);
SORT(g,h);
SORT(g,i);
SORT(g,j);
SORT(h,i);
SORT(h,j);
SORT(i,j);
printf("sorted\n%d%d%d%d%d%d%d%d%d%d\n", a,b,c,d,e,f,g,h,i,j);
break;
}
return 0;
}
***EDIT - alternate method***
Some words about the _array_ thing...
Not all `C` variables expressed using array _notation_, are arrays. eg. this:
char *a = malloc(10*sizeof(*a));
creates a memory location in heap memory comprised of `10` contiguous `char`
sized locations, and the areas of memory of `a` can be expressed as
a[0], ... a[9]
But `a` is not a _C array_, it is a pointer. The declaration:
char a[10];
_is_ a C array, and with the exception of where it is stored (on the stack), does the same.
More on C arrays:
[Arrays ref 1](https://www.tutorialspoint.com/cprogramming/c_arrays.htm)
[Arrays ref 2](https://www.cs.nmsu.edu/~rth/cs/cs271/notes/Pointers.html)
[Arrays ref 3](https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1069897882&id=1073086407)
The following uses pointers, not C Arrays.
***Method 2***
Output of following:
345654
sorted:
344556
Example code:
char *digits = "456543";
void main(void)
{
int len = strlen(digits);
int num = atoi(digits);
char *elements = calloc(len, sizeof(*elements));
if(elements)
{
for(int i=0; num; num /= 10, ++i)
{
*(elements +i) =(char)(num%10);
}
}
for(int i = 0;i<len;i++)
{
printf("%d", *(elements + i));
}
printf("\nsorted\n");
char *sorted = sort(elements, len);
for(int i = 0;i<len;i++)
{
printf("%d", (char)(*(sorted + i)));
}
free(elements);
}
char * sort( char *numbers, int count )
{
int i, j;
char temp;
do{
j = 0;
for (i = 0;i<count-1;i++){
if (*(numbers+i) > *(numbers+i+1)){//this was numbers[k], which was an error
j = 1;
temp = *(numbers+i);
*(numbers+i) = *(numbers+i+1);
*(numbers+i+1) = temp;
}
}
} while (j == 1);
return numbers;
}