1. ## Sorting algorithms

I have a code
Code:
```#include <stdio.h>
#include <conio.h>

void main
{
int n;
int num[100];
int L;
int desnum[100], k;
int i, i, temp;
printf("\nEnter the total number of marks to be entered: ");
scanf("%d",&n);
clrscr();
for(L=0;L<n;L++)
{
printf("\n Enter the marks of student %d : ", L+1);
scanf("%d",&num[L]);
}
for(k=0;k<n;k++)
{
desnum[k]= num[k]
}
for(i=0;i<n-1;i++)
{
for(j = i+1; j<n; j++)
{
if(desnum[i]<desnum[j])
{
temp = desnum[i];
desnum[i]= desnum[j];
desnum[j]= temp;
}
}
}
for(i=0;i<n;i++)
printf("\nNumber at [%d] is %d", i, desnum[i]);
}```
Can you tell me effection of this section
Code:
```for(k=0;k<n;k++)
{
desnum[k]= num[k]
}```

2. That loop copies one array into another.

3. Originally Posted by MK27
That loop copies one array into another.
Oh. But i think don't need that loop . Code still have not error.

4. Yeah you need that loop.... look at what happens next...

This is one of the problems you run into when you don't write your own code...
You end up with programs that you don't know how to fix.

5. Originally Posted by CommonTater
Yeah you need that loop.... look at what happens next...

This is one of the problems you run into when you don't write your own code...
You end up with programs that you don't know how to fix.
No . That is a examples in book .

I think code only like this :
Code:
```#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int num[100];
int L;
int i,j,temp;
printf("\nEnter the total number of marks to be entered: ");
scanf("%d",&n);
for(L=0;L<n;L++)
{
printf("\n Enter the marks of student %d: ",L+1);
scanf("%d",&num[L]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]<num[j])
{
temp = num[i];
num[i] = num[j];
num[j]= temp;
}
}
}
for(i=0;i<n;i++)
printf("\n Number at [%d] is [%d]",i,num[i]);
}```
Why we need copies array to another array ?

6. Originally Posted by ToNy_
I think code only like this :
Looks good. Bubblesort, right?

Why we need copies array to another array ?
Perhaps so you could compare the sorted array to the unsorted one? Anyway, you are correct, you don't have to have to copies for this sort.

Actually, you don't need a temp variable either:

Code:
```              num[i] += num[j];
num[j] = num[i] - num[j];
num[i] -= num[j]```

7. Originally Posted by MK27
Looks good. Bubblesort, right?
Actually, you don't need a temp variable either:

Code:
```              num[i] += num[j];
num[j] = num[i] - num[j];
num[i] -= num[j]```
I have known that. Is that assign swap two variables?
Like this :
B = A-B;
A = A-(-B)
B = A +B

8. Originally Posted by ToNy_
I have known that. Is that assign swap two variables?
Like this :
B = A-B;
A = A-(-B)
B = A +B
Looks like the same thing only different, yeah. I'm actually not sure of the real value; I think doing the arithmetic makes it more expensive processor wise than using the temp variable, which is silly just to save one int in memory. I could be wrong about that tho. Either way it is not a big deal.

I just like it because I am easily impressed by math tricks

9. Originally Posted by MK27
Looks like the same thing only different, yeah. I'm actually not sure of the real value; I think doing the arithmetic makes it more expensive processor wise than using the temp variable, which is silly just to save one int in memory. I could be wrong about that tho. Either way it is not a big deal.

I just like it because I am easily impressed by math tricks
You may be right... since it's possible optimizations will hold the temp value in a CPU register but if you're changing the value of variables, it has to write them out and read them back in, to preserve the memory data.

10. Thank a lots Mk27 vs CommomTater . Today i have learned swap two variables and Bubbles sorting algorithms .