Thread: sort

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    sort

    This sorting program currently sort into ascending order

    How can I change it to be able to both sort in ascending or descending [/code]depending on how the sort() function is invoked


    Code:
    #include <stdio.h>
    
    void sort(int u[], int n);
    int main(void)
    {
        int u[]={333,111,444,555,999};
        int n=sizeof(u)/sizeof(int);
    	int k;
    
        sort(u,n);
    
    	for (k=0;k<n;k++)
    		printf("%d\n", u[k]);
    	 
       return 0;
    }
    
    void sort(int u[], int n)
    {
    	int i,j,tmp;
    	
    	for (i=1;i<n;i++)
    		for (j=i;j>0;j--)
    		{
    			if (u[j]> u[j-1])
    			{
    				tmp=u[j];
    				u[j]=u[j-1];
    				u[j-1]=tmp;
    			}
    			else break;
    		}
    		return;
    }

  2. #2
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Simple...
    Code:
    if (u[j] > u[j-1])
    (assuming the code you posted does work)

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    Code:
    #include <stdio.h>
    
    void sortdescending(int u[], int n);
    void sortascending(int u[], int n);
    int main(void)
    {
    int u[]={333,111,444,555,999};
    int n=sizeof(u)/sizeof(int);
    int k;
    int choice;
    
    printf("Press 1 to sort in ascending order\n");
    printf("Press 2 to sort in descending order\n");
    scanf("%d", &choice);
    
    if (choice == 1)
    	{
    		sortascending(u,n);
    	}
    
    	else
    		{
    			sortdescending(u,n);
    		}
    
    		for (k=0;k<n;k++)
    		printf("%d\n", u[k]);
    
    return 0;
    }
    
    void sortdescending(int u[], int n)
    {
    int i,j,tmp;
    
    for (i=1;i<n;i++)
    for (j=i;j>0;j--)
    {
    if (u[j]> u[j-1])
    {
    tmp=u[j];
    u[j]=u[j-1];
    u[j-1]=tmp;
    }
    else break;
    }
    return;
    }
    
    void sortascending(int u[], int n)
    {
    int i,j,tmp;
    
    for (i=1;i<n;i++)
    for (j=i;j>0;j--)
    {
    if (u[j]< u[j-1])
    {
    tmp=u[j];
    u[j]=u[j-1];
    u[j-1]=tmp;
    }
    else break;
    }
    return;
    }
    or something of the like.

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    yea... all you do is reverse the logic

    if x is greater than x+1; swap 'em -> ascending
    if x is less than x+1 swap 'em -> descending

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    Sorry, but i can't really figure it out,
    Code:
    int n=sizeof(u)/sizeof(int);
    Is it really necessary to be using sizeof?
    Isn't it just getting the size of the array?

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    just use a function pointer as a parameter and make 2 comparison funcs. When you want to sort pass the correct comp func in to the sort by pointer. simple.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Thank you all for your help

    Thank you Salem...I think you answered my question...This is what I was looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM