Thread: Simple Sorting Algorithm

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    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;
    
    int menu();
    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;
    
    	
    		choice=menu();
    
    		switch (choice){
    			if(choice==0){return 0;}
    		case 1:
    			doctor(patient, temp);
    			break;
    		}
    		return 0;
    }
    
    int menu(){
    	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++;
    	}
    }
    Last edited by m0ntana; 04-14-2007 at 07:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge Sort the best sorting algorithm?
    By sehr alt in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 06-20-2007, 02:19 PM
  2. simple hashing algorithm??
    By iori in forum C Programming
    Replies: 7
    Last Post: 04-14-2003, 05:18 PM
  3. I need help with sorting a simple structure please
    By AlmostBeginner in forum C Programming
    Replies: 2
    Last Post: 04-11-2003, 03:01 AM
  4. a simple algorithm and questions
    By ustuzou in forum C++ Programming
    Replies: 0
    Last Post: 02-18-2002, 11:12 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM