Thread: Sorting Array Descending - need help!

  1. #1
    Registered User
    Join Date
    Oct 2006

    Exclamation Sorting Array Descending - need help!

    This this my coding, i want to convert it into Descending and output like :

    How many marks do you have [1 to 20] ? : 4
    Enter mark 1 : 50
    Enter mark 2 : 55
    Enter mark 3 : 45
    Enter mark 4 : 60

    The sorted array in ascending order:

    The sorted array in descending order:

    The incomplete coding are below, can sumone help me finish it..

    #include <stdio.h>
    #include <conio.h>
    #define k 20
    int main()
      int a[k], n, p, j, i, small_j, smallest, biggest, big_j;
      printf("\nHow many marks do you have [1 to 20] ? : ");
      if(scanf("%d", &n) == 1 && n>0 && n<=k)
    	for(j=1; j<n+1; j++) {
    	printf("Enter mark %d :  ",j);
    	scanf("%d", &a[j]);
    	for(p=0; p<n; p++)
    		smallest = a[p];
    		small_j = p;
    		for(j=p; j<n+1; j++)
    			if(a[j] < smallest)
    				smallest = a[j];
    				small_j = j;
    			a[small_j] = a[p];
    			a[p] = smallest;
    	printf("\nThe sorted array in ascending order:\n");
    	for(j=0; j<n; j++) printf("%d\n", a[j]);
    return 0;
    Last edited by mshazny; 10-16-2006 at 04:10 AM.

  2. #2
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    You can sort an array using the qsort C-style algorithm.

    The list container provides sort and splice capabilities if you want to take a more C++ oriented approach.

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    if you must implement your own sorting algorithm and not just utilize a library, look up different types such as bubble, insertion, merge, quick sort algorithms.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > for(j=1; j<n+1; j++)
    Your sort and print function correctly start at index 0 in the array.

    j = 0 ; j < n
    is the best thing for looping over your array.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    You can also use C++'s sort() - a comparison between qsort() and sort() at

    Salem: One can optimize the loop some more by using "j != n" instead of "j < n", which works as long as j and n are integers. I've also tried starting at n and decrementing to 0, as in

    int j=n;
    while (j--) {
    which reduces the test to comparison with 0, although the difference, if any, is probably microscopic.

    Edit: Sorry Salem, after reading your post more closely I saw you were concerned with the issue of getting the index right, not the efficiency (which shouldn't be an issue here).
    Edit: Changed "j" to "j and n".
    Last edited by robatino; 10-16-2006 at 11:45 AM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Two of your loops go one past the end and cause buffer overruns.
    The loop that prints out the results at the end is correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  2. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  3. Sorting an array
    By OnionKnight in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2005, 03:31 AM
  4. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM