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..