Thread: Can any1 tell me wats wrong in this c program..

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    Question Can any1 tell me wats wrong in this c program..

    Code:
    #include<stdio.h>
    #include<conio.h>
    #define MAX 20
    void selsort(int [], int size); //prototype declaration..
    int A[MAX];
    void main(){
    	int n,Temp,i,j;
    	clrscr();
    	printf("\n--------------------------------------------------");
    	printf("\n\n   PROGRAM TO SORT THE NUMBERS USING SELECTION SORT  ");
    	printf("\n\n------------------------------------------------");
    	printf("\n\n ENTER THE NUMBER OF TERMS...: ");
    	scanf("%d",&n);
    	printf("\n ENTER THE ELEMENTS OF THE ARRAY...:");
    	for(i=0; i<n; i++){
    		printf("\nThe [%d]th element is: ",i);
    		scanf("%d,", &A[i]);
    	}
    	printf("THE ARRAY BEFORE SORTING:");
    	for(i=0; i<n; i++){
    		printf("%d, ",A[i]);
    	}
    	selsort(A,n);
    	printf("\nTHE ARRAY AFTER SORTING IS...:");
    	for(i=0; i<n; i++)
    		printf("%d,",A[i]);
    	printf("\n------------------------------");
    	getch();
    }
    
    void selsort(int A[], int x){
    	int i, j, k, min,temp;
    	for(i=0; i<x-1; i++){
    	min =i;
    		for(j=i+1; j<x; j++){
    			if(A[j] < A[min]){
    				min  = j;
    				temp = A[i]; A[i] = A[min]; A[min] = temp;
    			}
    		}
    		printf("\ni = %d :: ", i);
    		for(k=0; k< x; k++)
    			printf( "%d, ",A[k]);
    	}
    }
    This is a program for sorting of numbers using selection sort method.
    When ever I enter the values: 324, 2, 354, 5, 34.
    It gives me a wrongly sorted array. so pls help me, where i m overlooking the mistake.. waiting 4 a quick reply..

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't want to swap until you've actually found the minimum, hence doing a swap inside the j for loop won't work.

  3. #3
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    What's all that business with variable min? It's just confusing things.
    You need to put the smallest value from the rest of the array into the first position, then repeat for the second position, and so on. I don't see what you are trying to do with min. Once you have swapped in your code you have changed what you are testing the "rest of the array" against.

    This code works:
    Code:
    void selsort (int A[], int x)
    {
      int i, j, k, temp;
      for (i = 0; i < x - 1; i++)
      {
          for (j = i + 1; j < x; j++)
          {
              if (A[j] < A[i])
                {
                  temp = A[i];
                  A[i] = A[j];
                  A[j] = temp;
                }
          }
          printf ("\ni = %d :: ", i);
          for (k = 0; k < x; k++)
            printf ("%d, ", A[k]);
      }
    }
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  2. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  3. Calendar Program. What am I doing wrong?
    By Sektor in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2004, 11:39 PM
  4. command line program, something wrong in the argv
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-26-2001, 09:36 AM
  5. what's wrong with my newbie program??
    By insoolated in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2001, 08:49 PM