Thread: Simple Sorting Algorithm

  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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > if((strcmp(x[j].doctor,x[j+1].doctor) < 0)
    You should be comparing the i element with the j element

    > temp=*(x+j);
    Try a much more readable
    temp = x[ j ];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by Salem View Post
    > if((strcmp(x[j].doctor,x[j+1].doctor) < 0)
    You should be comparing the i element with the j element

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

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by dwks View Post
    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. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by m0ntana View Post
    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. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dwks View Post
    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.

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