Thread: function to sort array

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    function to sort array

    Hi,
    I have written code to sort array numbers in increasing order

    Code:
    #include <stdio.h>
    
    int main()
    {
       int i = 0;
       int j = 0;
       int temp;
       
       int array[5] = { 50, 40, 30, 20, 10}; 
       
       for ( i = 0; i < 5; i++)
       {
    	    for ( j = i + 1; j < 5; j++)
    		{
    			temp = array[i];
    			array[i] = array[j];
    			array[j] = temp;   
    		}
       
       }
       
       for ( i = 0; i < 5; i++)
       {
    	   printf(" %d ", array[i]);
       }
       
       
     
        return 0;
    
    }



    I am now trying to make a function to sort array in increasing order

    Code:
    #include <stdio.h>
    
    void foo ( int *ptr)
    {
    	// How to sort array in increasing order in function
    	
    }
    
    
    int main()
    {
       int i = 0;
       int j = 0;
       int temp;
       
       int array[5] = { 50, 40, 30, 20, 10}; 
       
       foo(&array[0]);
       
       // print array after calling function
       for ( i = 0; i < 5; i++)
       {
    	   printf(" %d ", array[i]);
       }
       
     
        return 0;
    
    }


    I am looking help to make function so how to make function that sort array in increasing order
    Last edited by Kittu20; 01-20-2023 at 08:29 AM.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    Before you do that, you should first test your main program. Try mixing up the values in the array you already have -- 20, 40, 50, 30, 10; then 40, 10, 30, 20, 50; etc. This will make sure that your algorithm doesn't have any subtle bugs in it, before you start making changed.

    (PS: your algorithm has bugs in it.)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How does this differ from your previous question?
    swapping array value
    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.

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    i was sorting the array in main function and now i want to create a function which can sort the array increasing order

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Again, already showed you how to copy code from main to inside a function.
    function for largest value
    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.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Oh, it is very simple:
    Code:
    // Generic selection sort function.
    // Works like qsort().
    void selection_sort( void *ptr, 
                         size_t elems, size_t elem_size,
                         int (*cmp)(const void *, const void *) )
    {
      // YOUR code here
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sort array by function to get Max number.help plz
    By ILight in forum C Programming
    Replies: 11
    Last Post: 03-20-2015, 07:39 PM
  2. Replies: 4
    Last Post: 11-14-2012, 09:14 PM
  3. Replies: 1
    Last Post: 07-30-2011, 12:49 PM
  4. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  5. Replies: 4
    Last Post: 12-06-2009, 12:27 PM

Tags for this Thread