what's the best algorithm for selection sort?

does anyone know whats wrong with this code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
void swap (int a, int b){
    int temp;
    temp = a;
    a = b;
    b = temp;
}

void Selection_Sort (int A[], int size){
    int i,j;

    for ( i = 0; i < size; i++){
        for ( j = i+1; j < size; j++){
            if (A[i] > A[j]){
                swap(A[j],A[i]);
            }
        }
    }
}


int main()
{
    int n,i;
    int A[100];

    printf("Enter the number of elements to be sorted: ");
    scanf("%d",&n);
    for(i=0;i<n;++i)
      {
       printf("%d. Enter element: ",i+1);
       scanf("%d",&A[i]);
    }
    Selection_Sort(A, n);

    for(i=0;i<n;++i)
      {
       printf("%d ",A[i]);
    }

}
a and b are passed by value to swap(). Make them references, or their revised values won't go back to the calling function.
Last edited on
Topic archived. No new replies allowed.