# Simple Sorting Algorithm

This is a discussion on Simple Sorting Algorithm within the C Programming forums, part of the General Programming Boards category; Hi People, In my project, I've been trying to do sorting information in ascending or decsending order.. User Enters these ...

1. ## Simple Sorting Algorithm

Hi People,

In my project, I've been trying to do sorting information in ascending or decsending order..

User Enters these strings:
Angelina
Barbara
Terrel
Jamel

and it orders strings as below ascending order:
Terrel
Jamel
Barbara
Angelina
_________________________
I coded and what's wrong is, while ordering in ascending it gives as a first output some ascii chars instead of entered data(ascii?-jamel-barbara-angeline)...And I couldnt solve where I did something wrong, but I guess while program doing bubble sorting, it calculates somethings wrong, here is the code

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

struct hospital{
char doctor[30];} hospital;

int size=0;

void doctor(struct hospital* x, struct hospital temp);
struct hospital temp;

int main(){

struct hospital* patient;

int i=0;
int choice;
printf("Please give the number of data records:\n");
scanf("&#37;d",&size);

patient=malloc(size*sizeof(hospital));

printf("Please give %d patient records in the form:\n",size);
while(i!=size){

printf("Enter the doctor's name:\n");
scanf("%s",patient[i].doctor);
i++;
}

printf("The Patient Information entered is shown below:\n");
i=0;

switch (choice){
if(choice==0){return 0;}
case 1:
doctor(patient, temp);
break;
}
return 0;
}

int choice2;

printf("\nPlease give a Sorting Code according to menu below to Exit Press \"0\":\n");
printf("1-Surname ascending\n");
scanf("%d",&choice2);
return choice2;
}

void doctor(struct hospital* x, struct hospital temp){ //here trying to do kind of bubble sort to sort in ascending order.
int i=0;
int j=0;
for(i=0 ; i<size+1 ; i++){
for(j=0 ; j<size ; j++){
if((strcmp(x[j].doctor,x[j+1].doctor) < 0)){
temp=*(x+j);
*(x+j) = *(x+j+1);
*(x+j+1) = temp;
}
}
}
i=0;

i=0;
while( i != size ){
printf("%10s\n", x[i].doctor);
i++;
}
}```

2. > if((strcmp(x[j].doctor,x[j+1].doctor) < 0)
You should be comparing the i element with the j element

> temp=*(x+j);
temp = x[ j ];

3. Originally Posted by Salem
> if((strcmp(x[j].doctor,x[j+1].doctor) < 0)
You should be comparing the i element with the j element

> temp=*(x+j);
temp = x[ j ];
Thanks Salem, I tried as you said and it works fine definetly...

4. Code:
```		switch (choice){
if(choice==0){return 0;}
case 1:
doctor(patient, temp);
break;
}```
That code will never be executed, because it does not fall under a case/default.

Code:
```	i=0;

i=0;```
Once is enough.

5. Originally Posted by dwks
Code:
```		switch (choice){
if(choice==0){return 0;}
case 1:
doctor(patient, temp);
break;
}```
That code will never be executed, because it does not fall under a case/default.

Code:
```	i=0;

i=0;```
Once is enough.
For the second one, I wanted to check it twice :P, not ofcourse, as I were pasting code here I cut some pieces of it and one of the i=0 declaration left from another part of code..

Anyway For the first one, I wanted to make sure it :P...

Thanks for suggestions..

6. Originally Posted by m0ntana
Thanks Salem, I tried as you said and it works fine definetly...
Yeah that should also work. It isn't bubble-sort though, it's called simple-sort. Simple nested loops that compare every item to every other item and swap if out of order.

This code looks highly suspect though:
Code:
` 	for(i=0 ; i<size+1 ; i++){`
The +1 shouldn't be there as it would make you go off the end of the array.

7. Yeah that should also work. It isn't bubble-sort though, it's called simple-sort. Simple nested loops that compare every item to every other item and swap if out of order.
Most sorts have many names. This sort goes by "bubble sort" as its most common name.

This code looks highly suspect though:
Code:
` 	for(i=0 ; i<size+1 ; i++){`
The +1 shouldn't be there as it would make you go off the end of the array.
Actually, no. i is never actually used as an array index, so it doesn't matter. That code is one way to do the bubble sort, though very inefficient; a better way would be to use something like
Code:
```do {
did = 0;
// ...
if(swapped) did = 1;
// ...
} while(did);```

8. Originally Posted by dwks
Most sorts have many names. This sort goes by "bubble sort" as its most common name.

Actually, no. i is never actually used as an array index, so it doesn't matter. That code is one way to do the bubble sort, though very inefficient; a better way would be to use something like
Code:
```do {
did = 0;
// ...
if(swapped) did = 1;
// ...
} while(did);```
Well, he is going off the end of the array, just not exactly for the reason I stated earlier. The j loop also goes one too far, such that it is comparing item [size-1] with item[size]. And what I mentioned before is also still wrong.

Bubble sort is supposed to be stable. This isn't. It is most unfortunate that the same name can get used for this.