Last active
August 29, 2015 14:11
-
-
Save RDharmaTeja/63036fa58bfed4c6081e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* selection sort implementation in C */ | |
#include<stdio.h> | |
/* swapping two elements by reference */ | |
void swap(int *a,int *b) | |
{ | |
int temp; | |
temp = *a; | |
*a = *b; | |
*b = temp; | |
} | |
/* print array */ | |
void printArray(int A[], int N) | |
{ | |
int i=0; | |
for (i = 0; i < N; ++i) | |
{ | |
printf("%d ",A[i]); | |
} | |
} | |
/* selection sort */ | |
void selectionSort(int A[], int N) | |
{ | |
int i,j,minI; | |
for (i = 0; i < N-1; ++i) | |
{ | |
/* finding min index minI*/ | |
minI=i; | |
for (j = i+1; j < N; ++j) | |
{ | |
if (A[minI] > A[j]) | |
minI=j; | |
} | |
/*swapping with the min*/ | |
swap(&A[i],&A[minI]); | |
} | |
} | |
int main() | |
{ | |
int T,N; | |
int i,j; | |
int A[200]; | |
scanf("%d",&T); | |
for (i=0; i < T; ++i) | |
{ | |
scanf("%d",&N); | |
for(j=0; j < N; ++j) | |
{ | |
scanf("%d",&A[j]); | |
} | |
selectionSort(A,N); | |
printArray(A,N); | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment